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 2012 Forums
 Transact-SQL (2012)
 Adding - in between the varchar field

Author  Topic 

SQLBoy14
Yak Posting Veteran

70 Posts

Posted - 2014-11-25 : 21:26:22
Anyone knows how do I do the sql string function:

from: 222335650115ABC (Varchar data type)
To: 222-33565-0115-ABC

Thank you

SQLBoy

MuralikrishnaVeera
Posting Yak Master

129 Posts

Posted - 2014-11-26 : 00:54:50
You can play with PATINDEX

DECLARE @var Varchar(max)='222335650115ABC'

SELECT SUBSTRING(@var,0,PATINDEX('%[a-z]%',@var))+'-'+SUBSTRING(@var,PATINDEX('%[a-z]%',@var),LEN(@var))



---------------
Murali Krishna

You live only once ..If you do it right once is enough.......
Go to Top of Page

SQLBoy14
Yak Posting Veteran

70 Posts

Posted - 2014-11-26 : 08:51:14
Hello Murali, the output is only 222335650115-ABC. What I am trying to output is 222-33565-0115-ABC. Thanks!


SQLBoy
Go to Top of Page

Muj9
Yak Posting Veteran

75 Posts

Posted - 2014-11-26 : 08:58:03
DECLARE @var Varchar(20)='222335650115ABC'

select left(@var,3)+'-'+SUBSTRING(@var,4,5)+'-'+SUBSTRING(@var,9,4)+'-'+right(@var,3)
Go to Top of Page

cvipin
Yak Posting Veteran

51 Posts

Posted - 2014-12-02 : 19:42:24
DECLARE @Data Varchar(20)='222335650115ABC'
SELECT STUFF(STUFF(STUFF(@Data, 4, 0, '-'), 10, 0, '-'), 15, 0, '-')
Go to Top of Page
   

- Advertisement -