Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of Concatenate Function

Status
Not open for further replies.

VIJSQL01

Programmer
Oct 30, 2010
74
0
0
US
hi,

I am struggling to add a prefix to one of the column value in my table. The scenario goes like this.
I have values in a column, which are 4 as well as 5 digits. The values which have 4 digits should be added with '0' as prefix.
I already tried out following way, but it turned out be wrong. Please help me with this. Also i am surprised with the error it thrown.
The error which i am getting is that -- 'Concat is not a recognised built in function'..PLEASE HELP.

SQL:
[highlight #EF2929]
Code:
select case when LEN([FIPS Code])=4 THEN CONCATENATE('0',[FIPS Code])
ELSE END AS [FIPS Code]
From RT_County_Master
[/highlight]

Error:

[highlight #EF2929]
Code:
Msg 195, Level 15, State 10, Line 1
'CONCATENATE' is not a recognized built-in function name.
[/highlight]

 
Code:
select RIGHT('00000'+ CAST([FIPS Code] as varchar(5), 5) AS [FIPS Code]
From RT_County_Master

Borislav Borissov
VFP9 SP2, SQL Server
 
WOW!..thanks for a quick reply.

Well, unfortunately i am still getting a syntax error when i tried to execute the above mentioned query.

Error:
Code:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.

But, i just want to reiterate that, i just need to add only one Zero as prefix. Not really 5 zeros.

Thanks once again for your help on that. I greatly appreciate that!
~VIJSQL~

 
boris had a small typo

SQL:
select RIGHT('00000'+ CAST([FIPS Code] as varchar(5)[highlight #CC0000])[/highlight], 5) AS [FIPS Code]
From RT_County_Master
and the above right('00000' + ... ,5) is ensuring that even if your "Fips code" has less than 4 chars it will always return 5 with leading zeros


Regards

Frederico Fonseca
SysSoft Integrated Ltd

FAQ219-2884
FAQ181-2886
 
Thank you Thank you Thank you......

This is working..Great! You guys made my day.
 
Also, at the same time i would like understand how the Cast function works here. Here the Code
Code:
right('00000' + ... ,5)
how exactly it works and what is the main purpose of using the CAST function here?
Please throw some light on this, if possible.
 
Cast the field as varchar is to remove all trailing spaces and get the real length of the string.
So if you have record like this:
[tt]
'3ch '
[/tt]

you must get:
[tt]
'3ch'
[/tt]
then:
Code:
print RIGHT('00000' + CAST(TheField as varchar(5)), 5) -- '003ch'


Borislav Borissov
VFP9 SP2, SQL Server
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top