Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
select 'D05503'+ROW_NUMBER() OVER(ORDER BY C_CODE),* from Tbl_Doc_Stock_Chem_Add_Mst where N_Type=1 and C_Code like 'sa%'


While executing this query am getting error
like below:

Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to bigint.


Numbers should come like this:

D05503
D05504
D05505
D05506
D05507
D05508
Posted
Updated 27-Feb-15 1:42am
v2

ROW_NUMBER returns a numeric value, and SQL is trying to convert the text string "D05503" to a number in order to add it. That won't work.
Instead, try this:
SQL
SELECT 'D' + RIGHT('00000' + CONVERT(VARCHAR, 5503 + ROW_NUMBER() OVER (ORDER BY C_CODE)), 5), * 
FROM bl_Doc_Stock_Chem_Add_Mst 
WHERE N_Type=1 AND C_Code LIKE 'sa%'
 
Share this answer
 
Comments
Rajesh waran 27-Feb-15 8:01am    
5+. But OP's required output is starting from D05503. So i think have to change "VARCHAR, 5503" to "VARCHAR, 5502".
OriginalGriff 27-Feb-15 8:24am    
:laugh: Easy to fix!
I copied from the OP original and modified from there - so it does what his tried to do.
Rajesh waran 27-Feb-15 8:26am    
Cool.
That's a pretty cryptic error message.
But it probably comes from doing things in the wrong order.
Analytic functions are last in the pipe.

So try using a CTE like this:
SQL
WITH CTE AS (
    SELECT  ROW_NUMBER() OVER(ORDER BY C_CODE) rn
    FROM    Tbl_Doc_Stock_Chem_Add_Mst
    WHERE   N_Type=1
        AND C_Code LIKE 'sa%'
    )
SELECT  'D05503' + cast(rn as varchar) Somenumber
FROM    CTE
 
Share this answer
 
v2
Comments
Member 11337367 27-Feb-15 7:43am    
same error getting
Msg 8114, Level 16, State 5, Line 2
Error converting data type varchar to bigint.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900