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)
 Update Issue

Author  Topic 

tooba
Posting Yak Master

224 Posts

Posted - 2011-03-16 : 20:38:45
Hi guys,

I have source data like this listed below:-

Code
12345
78699
75689

I want to add '-' after 2 character Example:-

Code
12-345
78-699
75-689

Help me out how i can accomplish, Thanks.

singularity
Posting Yak Master

153 Posts

Posted - 2011-03-16 : 21:36:10
select left(code,2) + '-' + right(code,3)
from yourtable
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-16 : 21:41:40
Or, use my favorite function - the one and only STUFF.
update YourTable set
YourColumn = stuff(YourColumn,3,0,'-')
Go to Top of Page

tooba
Posting Yak Master

224 Posts

Posted - 2011-03-16 : 22:26:03
Hi sunitabeck, thanks for your reply, quick question, could you please advise me what is "stuff" in your statement?
update YourTable set
YourColumn = stuff(YourColumn,3,0,'-')
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-16 : 22:29:50
stuff is a SQL function that inserts one string into another. See this MSDN page: http://msdn.microsoft.com/en-us/library/ms188043.aspx
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-03-18 : 03:47:25
If it is only for display use it in a select statement

select stuff(YourColumn,3,0,'-') from table

Madhivanan

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

- Advertisement -