Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 concat colum problem

Author  Topic 

ravensensei
Starting Member

13 Posts

Posted - 2007-08-16 : 14:54:03
I have three columns I'd like to concat in a fourth column.
col1 is a varchar(10)
col2 is an int
col3 is the pk(int)


When I use this formula:
([col1] + '' + [col2] + '' + [col3])
it changes the column to an int, and it won't let me use the two Letter code in col1. And it won't let me change the type to a varchar. I've tried deleting the col and trying again, but no dice. Even ditched the table and still no luck.

any ideas?

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2007-08-16 : 15:01:56
How did you try to change the datatype? Did you use CAST or CONVERT? That should work.

e4 d5 xd5 Nf6
Go to Top of Page

ashley.sql
Constraint Violating Yak Guru

299 Posts

Posted - 2007-08-16 : 15:03:08
col1 +' '+convert(varchar(25),col2)+' '+convert(varchar(25),col3)

Ashley Rhodes
Go to Top of Page

ravensensei
Starting Member

13 Posts

Posted - 2007-08-16 : 15:10:35
that was it. You people are WAY too cool.
Thanks!
Go to Top of Page

ravensensei
Starting Member

13 Posts

Posted - 2007-08-16 : 15:13:54
Bonus round - the last column I have is an incrementing value, but I'd like to have the concatted value have the same length regardless. Is there a way to put an if statement that looks at the length of the pk and tacks on 0's?
col1 - PS
col2 - 07
col3 - 1
concat value is PS070001

Thought of using an IF, but I'm wondering if I can in SQL formula
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-08-16 : 15:47:44
Col1 + RIGHT('00' + CONVERT(VARCHAR, Col2), 2) + RIGHT('0000' + CONVERT(VARCHAR, Col3), 4)



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -