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 2005 Forums
 Transact-SQL (2005)
 Concatenate Fields

Author  Topic 

JezLisle
Posting Yak Master

132 Posts

Posted - 2009-12-07 : 11:18:42
How can I concatenate several fields to one string?

I want to create a string from 2 VARCHAR fields and a DateTime field

This is my attempt below, but it errored saying

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'ASV' to data type int.

JobType+''+ApptJobNo+''+CONVERT(VARCHAR(10), ApptDate,101) AS Tag


how can I get this to work?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-12-07 : 11:21:19
is JobType & AppJobNo both string ? if not convert to string first before concat


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-12-07 : 11:21:52
If one of the fields is numeric datatype then use convert to varchar.

example:
convert(varchar(255),JobType)+''+convert(varchar(255),ApptJobNo)+''+CONVERT(VARCHAR(10), ApptDate,101) AS Tag

Because concatenating is not possible with numeric data type without convert.


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

JezLisle
Posting Yak Master

132 Posts

Posted - 2009-12-07 : 11:24:51
Excellent, thanks for that :-)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-08 : 01:47:15
Also you dont need dummy single quotes but I guess it should be a single space

convert(varchar(255),JobType)+' '+convert(varchar(255),ApptJobNo)+' '+CONVERT(VARCHAR(10), ApptDate,101) AS Tag


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -