SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need help to split Data


Need help to split Data

Author
Message
abhas
abhas
SSC Eights!
SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)

Group: General Forum Members
Points: 966 Visits: 664
Hi All,

I am having a column StartTime in my table which data looks like below.

StartTime
7:00 AM <br/> 12:30 PM

Now I need to split the above into two separate rows as below.

StartTime
7:00 AM
12:30 PM

How can I do this.

This is very urtgent please help me.

Thanks
Abhas.
Sowbhari
Sowbhari
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2206 Visits: 1488
This might help

DECLARE @StartTime VARCHAR(MAX),@X XML
SELECT @StartTime = '7:00 AM
12:30 PM'
SELECT @X = CONVERT(XML,'' + replace(@StartTime,'','</br>') + '</br>')
SELECT x.i.value('.','varchar(max)') AS [StartTime]
FROM @X.nodes('//br') x(i)


Sowbhari
Sowbhari
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2206 Visits: 1488
Sowbhari (12/19/2013)
This might help

DECLARE @StartTime VARCHAR(MAX),@X XML
SELECT @StartTime = '7:00 AM
12:30 PM'
SELECT @X = CONVERT(XML,'' + replace(@StartTime,'','</br>') + '</br>')
SELECT x.i.value('.','varchar(max)') AS [StartTime]
FROM @X.nodes('//br') x(i)


This is the correct SQL,somehow the tags are missing in my previous post.
DECLARE @StartTime VARCHAR(MAX),@X XML
SELECT @StartTime = '7:00 AM <br/> 12:30 PM'
SELECT @X = CONVERT(XML,'<br>' + replace(@StartTime,'<br/> ','</br><br>') + '</br>')
SELECT x.i.value('.','varchar(max)') AS [StartTime]
FROM @X.nodes('//br') x(i)
higgim
higgim
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1504 Visits: 2631
You could also create a function to do it:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[fnSplitList]
(
@sInputList VARCHAR(8000),
@sDelimiter VARCHAR(10)
)
RETURNS @List TABLE ( item VARCHAR(8000) )
BEGIN
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@sDelimiter, @sInputList, 0) <> 0
BEGIN
SELECT @sItem = RTRIM(LTRIM(SUBSTRING(@sInputList, 1,
CHARINDEX(@sDelimiter,
@sInputList, 0)
- 1))) ,
@sInputList = RTRIM(LTRIM(SUBSTRING(@sInputList,
CHARINDEX(@sDelimiter,
@sInputList, 0)
+ LEN(@sDelimiter),
LEN(@sInputList))))

IF LEN(@sItem) > 0
INSERT INTO @List
SELECT @sItem
END

IF LEN(@sInputList) > 0
INSERT INTO @List
SELECT @sInputList -- Put the last item in
RETURN
END

GO



From there you just do the following:

SELECT * FROM dbo.fnSplitList('7:00 AM delimiter 12:30 PM','delimiter')



Output :
item
7:00 AM
12:30 PM



You can then be flexible on your delimiters Smile
Mitesh Oswal
Mitesh Oswal
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1186 Visits: 653
Try with Charindex

DECLARE @StartTime VARCHAR(MAX),@X XML
SELECT @StartTime = '7:00 AM
12:30 PM'



DECLARE @tbl TABLE
(
ID INT
)

INSERT INTO @tbl
SELECT TOP 100 ROW_NUMBER() OVER(Order by s.object_id)
from sys.objects s , sys.objects si


select LTRIM(SUBSTRING(@StartTime,ID,CHARINDEX(CHAR(13),@StartTime+CHAR(13),ID))) from @tbl
where CHARINDEX(CHAR(13),CHAR(13)+@StartTime,ID)=ID



Regards,
Mitesh OSwal
+918698619998
Sean Lange
Sean Lange
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64259 Visits: 17974
Everyone on this thread should take a look at the link in my signature for splitting strings. In there you will find a number of ways to split a string including the while loop and xml methods. As you continue reading you will find a tally table set based splitter. It will blow the doors off all the above methods for performance.

Another method that is super duper fast is Dwain Camp's pattern splitter. You can find it here. http://www.sqlservercentral.com/articles/String+Manipulation/94365/

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
higgim
higgim
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1504 Visits: 2631
Cheers Sean. Will have a look Smile
abhas
abhas
SSC Eights!
SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)

Group: General Forum Members
Points: 966 Visits: 664
Hi Sowbhari,

Thanks for reply but if i am using your approach, i am getting below error.


XML parsing: line 1, character 13, end tag does not match start tag


Thanks
Abhas
abhas
abhas
SSC Eights!
SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)SSC Eights! (966 reputation)

Group: General Forum Members
Points: 966 Visits: 664
Hi,
Sorry Sowbhari,

its working.


DECLARE @StartTime VARCHAR(MAX),@X XML
SELECT @StartTime = '7:00 AM <br/> 12:30 PM'
SELECT @X = CONVERT(XML,'
' + replace(@StartTime,'<br/> ','</br>
') + '</br>')
SELECT x.i.value('.','varchar(max)') AS [StartTime]
FROM @X.nodes('//br') x(i)


Thanks.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64259 Visits: 17974
abhas (12/19/2013)
Hi,
Sorry Sowbhari,

its working.


DECLARE @StartTime VARCHAR(MAX),@X XML
SELECT @StartTime = '7:00 AM <br/> 12:30 PM'
SELECT @X = CONVERT(XML,'
' + replace(@StartTime,'<br/> ','</br>
') + '</br>')
SELECT x.i.value('.','varchar(max)') AS [StartTime]
FROM @X.nodes('//br') x(i)


Thanks.


I would again recommend you look at the articles I referenced. They will perform a LOT better in most situations. Try them out and see what works for you but the DelimitedSplit8K function is crazy fast.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search