0

I'm Having a column data like below,

126-35-56-24

And I want the results be like,

select 126 as Id, 35 as Age, 56 as EmpId, 24 as Day

I just try using substring and I can Able to split the string into 126 and 35-56-24, but I can't get the result I Want.

Please help me to get this. Thanks in Advance...

2
  • Use STRING_SPLIT in SQL Server 2016 or later, otherwise you'll need a custom function or a really ugly query with substring and other stuff. Commented Sep 8, 2016 at 12:33
  • To make your life easier, see if you can split your date data before you load it into SQL Server. Commented Sep 8, 2016 at 12:34

6 Answers 6

6

You could use parsename if it's always 4 sets of data.

I simply replaced the - with a . and let parsename do the rest.

SELECT PARSENAME(Replace(Col,'-','.'), 1) AS 'Object Name' ID,    
       PARSENAME(Replace(Col,'-','.'), 2) AS 'Age',  
       PARSENAME(Replace(Col,'-','.'), 3) AS 'EmpID',  
       PARSENAME(Replace(Col,'-','.'), 4) AS 'Day'
FROM YOURTABLE
Sign up to request clarification or add additional context in comments.

Comments

2

Since no one else has mentioned it, you can simply use the PARSENAME function. It's been available in SQL Server for quite some time. It has some limitations, but it will handle the task in the example you gave.

Comments

2

Use the below Script:

DECLARE @VAR VARCHAR(50)='126-35-56-24'
SELECT [1] AS ID, [2] AS AGE,[3] AS EMPID,[4] as day
FROM  (SELECT [1],[2],[3],[4]
   FROM    
      (SELECT ID,VAL FROM Spliter(@VAR,'-')) AS B
   PIVOT (MAX(VAL) FOR ID IN ([1],[2],[3],[4])
         ) AS A
  ) AS C

And for splitting:

CREATE FUNCTION Spliter
(
@delimited nvarchar(max),
@delimiter nvarchar(100)
) RETURNS @t TABLE
(
 id int identity(1,1),
 val nvarchar(max)
)
 AS
 BEGIN
 declare @xml xml
 set @xml = N'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r>       </root>'

 insert into @t(val)
 select
 r.value('.','varchar(max)') as item
 from @xml.nodes('//root/r') as records(r)

 RETURN
 END
 GO

Comments

1

Use the below script ,which will split the strings based on the char index of '-'.

DECLARE @data varchar(50)='126-35-56-24'

SELECT 'SELECT '+ SUBSTRING(@data,1,CHARINDEX('-', @data)-1) + ' as Id, '
   +SUBSTRING(@data,CHARINDEX('-', @data)+1,CHARINDEX('-', @data,CHARINDEX('-',@data) + 1) -CHARINDEX('-', @data)-1)+ ' as Age, '
   +SUBSTRING(@data,CHARINDEX('-', @data,CHARINDEX('-',@data)+1)+1,CHARINDEX('-', @data,CHARINDEX('-', @data,CHARINDEX('-',@data)+1)+1) -CHARINDEX('-', @data,CHARINDEX('-',@data)+1)-1)+' as EmpId, '
   +SUBSTRING(@data,CHARINDEX('-', @data,CHARINDEX('-', @data,CHARINDEX('-',@data)+1)+1)+1,LEN(@data)-CHARINDEX('-', @data,CHARINDEX('-', @data,CHARINDEX('-',@data)+1)+1)+1) +' as Day'

Sample output :

enter image description here

1 Comment

Thanks you for the effort, But the data size may variant. It may like '66-35-546-4'., So I want to split by using the '-' only. Thank you..
1

Everything that Tim mentioned..,

But if you need a splitter

Declare @String varchar(50) = '126-35-56-24'

Select ID    = max(case when Key_PS=1 then Key_Value else null end)
      ,Age   = max(case when Key_PS=2 then Key_Value else null end)
      ,EmpID = max(case when Key_PS=3 then Key_Value else null end)
      ,Day   = max(case when Key_PS=4 then Key_Value else null end)
 From  [dbo].[udf-Str-Parse](@String,'-')

Returns

ID    Age   EmpID   Day
126   35    56      24

The UDF

CREATE FUNCTION [dbo].[udf-Str-Parse] (@String varchar(max),@Delimeter varchar(10))
--Usage: Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
--       Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')

Returns @ReturnTable Table (Key_PS int IDENTITY(1,1), Key_Value varchar(max))
As
Begin
   Declare @XML xml;Set @XML = Cast('<x>' + Replace(@String,@Delimeter,'</x><x>')+'</x>' as XML)
   Insert Into @ReturnTable Select ltrim(rtrim(String.value('.', 'varchar(max)'))) FROM @XML.nodes('x') as T(String)
   Return 
End

Comments

1

If you are using SQL Server 2016 you can use string_split function

declare @str varchar(20) = '126-35-56-24'

select  id = max(case when rn=1 then value else null end)
    ,age =  max(case when rn=2 then value else null end)
    ,empId =  max(case when rn=3 then value else null end)
    ,[Day] =  max(case when rn=4 then value else null end)
    from (select row_number() over(order by (select 1)) as rn, value from string_split(@str, '-')) a

2 Comments

I'm using SQL Server 2012
You can use custom split and replace string_split function.. Let me know if you do not have one..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.