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
 Concatenating varchar and numeric

Author  Topic 

fastmichaels
Yak Posting Veteran

71 Posts

Posted - 2008-10-30 : 10:49:48
Hi everyone,

I need to combine several fields in one, such as age, firstname, lastname, gender, street, city, poscode so it can be read in one sentace like 20 Male John Smith High st......

I have tried to concatenate the fields but I get an error message saying "Error converting data type varchar to numeric.". I asume this is because the fields are made up of varchar and numeric values.

How would I go about combining them?

Thanks everyone.

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-30 : 10:54:44
age must be numeric:

cast possible numeric fields into varchars.

for example:
cast(age as char(2))
Go to Top of Page

fastmichaels
Yak Posting Veteran

71 Posts

Posted - 2008-10-30 : 11:00:28
quote:
Originally posted by hanbingl

age must be numeric:

cast possible numeric fields into varchars.

for example:
cast(age as char(2))



How could you fit that into a SELECT statement? i.e. in this statement:

SELECT date,time,age+''+firstname+''+lastname+''.... FROM table3

Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-30 : 12:09:18
SELECT date,time,cast(age as varchar(3))+''+firstname+''+lastname+''+.... FROM table3
Go to Top of Page

fastmichaels
Yak Posting Veteran

71 Posts

Posted - 2008-10-30 : 12:47:03
quote:
Originally posted by visakh16

SELECT date,time,cast(age as varchar(3))+''+firstname+''+lastname+''+.... FROM table3



That works really well!! But the results are coming back joined up, how do you get spaces between field contents?

Thanks ever so much for your help, I'm learning so much from you, it feels like I should pay you for your services, ha ha
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2008-10-30 : 12:51:01
[code]Do you see all the +''+ ?
Make it +' '+[/code]

Webfred


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -