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)
 Replace or Substring?

Author  Topic 

SSSQL2008R2
Starting Member

3 Posts

Posted - 2013-01-05 : 19:08:21
Hi all,

I have a column which I am trying to prefix every entry to have a prefix of 2013. How would I go about doing this? I dont know what syntax you would use so that sql knows to start at the beginning of the string?

Thanks in advance!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-06 : 09:48:31
sounds like this

UPDATE table
SET Col = '2013' + Col

in case Col is of numeric type (int,decimal,numeric,flost etc) then use

UPDATE table
SET Col = '2013' + CAST(Col AS varchar(30))



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-01-07 : 07:59:52
Alternatively without changing the actual data, you can also append this when you SELECT data

SELECT col+'2013' FROM table WHERE ...

Madhivanan

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

SSSQL2008R2
Starting Member

3 Posts

Posted - 2013-01-07 : 22:42:48
Hi guys,

I managed to get this in the end, I added column for 2013 then added the column for 2013 to the one i was trying to concat with.

Thanks for the replys!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-07 : 23:28:56
quote:
Originally posted by SSSQL2008R2

Hi guys,

I managed to get this in the end, I added column for 2013 then added the column for 2013 to the one i was trying to concat with.

Thanks for the replys!


why do you need a separate column for this? you could just concat it on the fly as shown.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -