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 2008 Forums
 Transact-SQL (2008)
 Combine two fields into one?

Author  Topic 

mikeallenbrown
Yak Posting Veteran

72 Posts

Posted - 2013-05-28 : 13:49:21
I want to automatically combine two columns into one.

MRNum - varchar(30)
ClientID - int

I created a new column (UniqueName) and tried this computed column formula:
([MRNum]+[ClientID]) ....didn't work.

So if MRnum is: 22-123 and ClientID is: 5010 I want UniqueName to automatically populate with 22-1235010

-Mike

Mike Brown

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-05-28 : 14:01:43
you'll need to explicitly convert the datatype

e.g. MRNum + Convert(varchar,ClientID)

Else it will throw conversion error

Cheers
MIK
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-28 : 14:29:43
quote:
Originally posted by MIK_2008

you'll need to explicitly convert the datatype

e.g. MRNum + Convert(varchar,ClientID)

Else it will throw conversion error

Cheers
MIK


though it may not affect in this case, always make it a point to specify a length while casting to varchar

see reason here

http://visakhm.blogspot.in/2010/02/importance-of-specifying-length-in.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

djj55
Constraint Violating Yak Guru

352 Posts

Posted - 2013-05-28 : 14:30:56
To add to what MIK said. SQL tries to convert the equation to all the same data types based on a hiearchy. INT is before VARCHAR so the need for the explicit convertion.

djj
Go to Top of Page

mikeallenbrown
Yak Posting Veteran

72 Posts

Posted - 2013-05-29 : 11:25:11
Thank you all! ....MIK's answer was spot on!

-Mike

Mike Brown
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-05-29 : 12:23:01
quote:
Originally posted by mikeallenbrown

Thank you all! ....MIK's answer was spot on!

-Mike

Mike Brown



You're welcome!

Though, I'ven't specified the length (since ClientID is INT) but as Visakh pointed it out, it should be a considering factor. :)

Cheers
MIK
Go to Top of Page
   

- Advertisement -