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
 General SQL Server Forums
 New to SQL Server Programming
 concatening strings in SQL Server

Author  Topic 

Vignesh
Starting Member

20 Posts

Posted - 2006-05-09 : 11:17:56
hello all
i am trying to concatenate strings in SQL Server by using '+' operator. but the problem is i have a code like this 1201 and another code like 0003 and when i use + operator. it removes the leading zeroes. i dont want to remove the leading zeros. how do i do this

Thanks in advance

Vignesh

mfemenel
Professor Frink

1421 Posts

Posted - 2006-05-09 : 12:15:56
cast your columns as varchar, then concatenate them. If you have a column that's storing these values it would look like this: cast(columnA as varchar(20)) + cast(columnb as varchar(20)). This will treat your columns as character data and allow the concatenation without dropping the leading 0's.

Mike
"oh, that monkey is going to pay"
Go to Top of Page

Vignesh
Starting Member

20 Posts

Posted - 2006-05-09 : 13:30:34
hi Mike
the columns are defined as varchar only.


Vignesh
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-05-09 : 13:39:12
It works fine for me:

DECLARE @a varchar(4), @b varchar(4)

SELECT @a = '1201', @b = '0003'

SELECT @a + @b
SELECT @b + @a

Tara Kizer
aka tduggan
Go to Top of Page

Vignesh
Starting Member

20 Posts

Posted - 2006-05-09 : 15:13:43
Hi Tara
the code works fine for me also
but if i have to concatenate two columns how do i do that.

Vignesh
Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-05-09 : 15:29:17
Vignesh,

I'm not telling anything different from the others. I'm just answering to ur specific question. (Means, the answers given by the others are more than enough for u )


Create table #t (a varchar(10), b varchar(10))

Insert into #t
SELECT '1201', '0003'

Select a, b, a + b from #t

Drop table #t


Srinika
Go to Top of Page

Vignesh
Starting Member

20 Posts

Posted - 2006-05-09 : 15:39:14
thank you all
its working. sorry did a silly mistake. thanks all for your replies.
Vignesh

Vignesh
Go to Top of Page
   

- Advertisement -