March 20, 2009 at 11:21 am
How do I generate a serial no. within a select query
My query is
Select CustomerId,CustomerName from CustMaster
and I want the output as
1 23232 John
2 32323 Amit
3 82321 KY
Also another question is
I have a variable as type datetime like
declare @PFDate datetime
set @PFDate = '2009-03-19 00:00:00.000'
and I need to add time to this datetime variable. Like if I would add
15 hours 30 min then the variable should become '2009-03-19 15:30:00.000'
Thanks
March 20, 2009 at 11:28 am
For the first question:
What is the specification for the Serial No? Any incremental ID? Should it contain any information? Should it be a NVE or a Code39?
For the second question:
declare @PFDate datetime
set @PFDate = '2009-03-19 00:00:00.000'
select @PFDate, @PFDate + CONVERT(DATETIME, '15:30')
Greets
Flo
March 20, 2009 at 11:51 am
My questions are the same:
what is a serial number to you? is it a random number? can it be '0000' plust the customer number?(0001,0002, etc). Why did your example have a wide range of values?
is the number unique? does it need to follow a business pattern(ie SN0000YYYY0001 so it makes "at-a-glance' sense to some people?
Lowell
March 20, 2009 at 5:59 pm
SELECT ROW_NUMBER() OVER (ORDER BY CustomerID) AS SerialNo,
CustomerId,
CustomerName
FROM dbo.CustMaster
But, I agree with the others... what are you going to use it for? The answer to that question could make a huge difference.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 20, 2009 at 8:43 pm
Thanks for the replies. I am looking for a simple serial no. count starting with 1,2,3......
I just have to display the customerid and customername along with a serial no. starting from 1 , so I was looking to generate this serial no. in the sql query only.
sample output if the query returned 10 records
SR No. CustomerId Customername
1 4444444 ABC
2 3332323 XYX
3 3232323 YTR
'
'
'
10 4343434 YTTT
March 20, 2009 at 9:02 pm
Then the query I included with my post should do it.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 21, 2009 at 12:35 am
I tried your example with sql 2005 but it gives me this error
Msg 195, Level 15, State 10, Line 1
'ROWNUMBER' is not a recognized built-in function name.
March 21, 2009 at 2:32 am
Hi
Which version of SQL Server are you using?
Greets
Flo
March 21, 2009 at 2:51 am
I am using SQL server 2005 and when I try this code for generating a running sequence no. to start from 1 it gives me the error
SELECT ROWNUMBER() OVER (ORDER BY CustomerID) AS SerialNo,
CustomerId,CustomerName FROM dbo.CustMaster
March 21, 2009 at 3:59 am
SELECT ROW_NUMBER() OVER (ORDER BY CustomerID) AS SerialNo,
CustomerId,CustomerName FROM dbo.CustMaster
March 21, 2009 at 6:27 am
that error could occur if your database compatibility level is at 80 instead of 90;
the compatibility level determines which syntax version is applied to statements hitting the database, and rownnumber() is a 90 and above functionality.
change the version and you should be all set.
znkin (3/21/2009)
I tried your example with sql 2005 but it gives me this errorMsg 195, Level 15, State 10, Line 1
'ROWNUMBER' is not a recognized built-in function name.
Lowell
March 21, 2009 at 6:56 am
I don't think it's the compatibility level...
Looks more like a typo: ROWNUMBER vs. ROW_NUMBER()
Underscores sometimes matter 😛
The RowNumber function is for Reporting Services.
March 21, 2009 at 9:36 am
Dangit... sorry folks... I posted in a hurry and left out the underscore in ROW_NUMBER(). Andrew has the correct code and I went back to my previous erroneous post and fixed that, as well.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 10, 2013 at 11:20 pm
I want devide this table in to groups like in first block display Id(1-5) and second block desplay data (5-10) and onward plz help
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply