Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
select 'sa0'+'5752'+ROW_NUMBER() over(order by subarea) from stp5

iam getting error for this query..

i want to column come like
sa05752
sa05753
so on


pls help me out
Posted
Comments
Tomas Takac 29-Apr-15 3:31am    
What error do you get?
Member 11337367 29-Apr-15 3:35am    
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to bigint.


Try:
SQL
SELECT 'sa0' + '5752' + CAST(ROW_NUMBER() OVER(ORDER BY subarea) AS NVARCHAR(6)) FROM stp5
 
Share this answer
 
If I have to guess the problem is parameter mismatch. The first one is a string and ROW_NUMBER() produces an integer. Now + is used for both addition and string concatenation. And SQL server just cannot decide what are you trying to do, by default it picks addition. Hence tries to convert all the arguments to integers but of course 'sa0' cannot be converted so it fails. You need to tell it you want to do string concatenation. You do it by converting the second parameter to varchar:
SQL
select 'sa05752'+cast(ROW_NUMBER() over(order by subarea) as varchar(10)) from stp5

Not sure why you used + to concatenate the first two strings so I merged those into one.
 
Share this answer
 
v2
ROW_NUMBER()[^] function returns a value of type bigint...
As this is the last part of your expression ('sa0' + '5752' + ROW_NUMBER()), SQL try to convert all the other parts to bigint...
To avoid that you have to explicitly convert the outcome of ROW_NUMBER() to nvarchar...
SQL
select 'sa0' + '5752' + CAST(ROW_NUMBER() over(order by subarea) AS NVARCHAR) from stp5
 
Share this answer
 
v2
Comments
Member 11337367 29-Apr-15 3:42am    
Msg 243, Level 16, State 1, Line 1
Type NVARACHAR is not a defined system type.
error for ur query
Member 11337367 29-Apr-15 3:45am    
sa057524
sa057525
sa057526
sa057527
sa057528
sa057529
sa0575210
sa0575211
sa0575212

but number coming like this..it is wrong..
Kornfeld Eliyahu Peter 29-Apr-15 3:49am    
Why that's wrong?
Kornfeld Eliyahu Peter 29-Apr-15 3:48am    
It's a typo - fixed
Try this:
SQL
select 'sa0'+convert(varchar, 5752+ROW_NUMBER() over(order by subarea)) from stp5
 
Share this answer
 

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