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


split string into columns based on special character


split string into columns based on special character

Author
Message
knakka99
knakka99
SSC-Enthusiastic
SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)

Group: General Forum Members
Points: 133 Visits: 126
/*The Displayname column has to be split into 5 columns basing on the '/' .
I have noticed that the Field4 and Field5 are always null. */
/*This is how the output table should be. (also attached screenshot and sql to create the table)
But the issue is that there is no consistency with regards to the '/'*/

CREATE TABLE TEMP#(
Displayname varchar(35),
FIELD1 varchar(35),
FIELD2 varchar(35),
FIELD3 varchar(35),
FIELD4 varchar(35),
FIELD5 varchar(35)
)
INSERT INTO TEMP#
select '1071 E MAIN STREET////'as Displayname,'1071 E MAIN STREET'as FIELD1,''as FIELD2,''as FIELD3,''as FIELD4,''as FIELD5
union
select '/1/REC','','1','REC','',''
union
select 'Recovery/1/REC','Recovery','1','REC','',''
union
select '//3','','','3','',''
union
select 'Bldg 3150//327//','Bldg 3150','','327','',''
union
select 'nk','nk','','','',''
union
select ' CSS////','CSS','','','',''


SELECT * FROM TEMP#
Attachments
string break.JPG (13 views, 24.00 KB)
venus.pvr
venus.pvr
SSC Eights!
SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)

Group: General Forum Members
Points: 965 Visits: 558
Use the below code:

SELECT
ISNULL(x.y.value('Display[1]', 'VARCHAR(30)'),'') AS col1,
ISNULL(x.y.value('Display[2]', 'VARCHAR(30)'),'') AS col2,
ISNULL(x.y.value('Display[3]', 'VARCHAR(30)'),'') AS col2,
ISNULL(x.y.value('Display[4]', 'VARCHAR(30)'),'') AS col2,
ISNULL(x.y.value('Display[5]', 'VARCHAR(30)'),'') AS col2
FROM
(SELECT CAST('<aa><Display>'+REPLACE(DisplayName,'/','</Display><Display>')+'</Display></aa>' AS XML) as Display FROM TEMP# )cte
CROSS APPLY cte.Display.nodes('aa') x(y)

Praveena
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40177 Visits: 19802
You could use Jeff Moden (& the community) Splitter
Along with Cross Tabs


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Sean Lange
Sean Lange
SSC Guru
SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)

Group: General Forum Members
Points: 59712 Visits: 17947
Luis is absolutely correct. Use the delimited splitter he linked and then a cross tab.

BTW, nice job posting ddl and sample data. You are new around here but obviously understand the importance of that. It goes a long way with the people around here.

One thing is that the table you posted is a persistent table. Temp tables START with a pound sign. Your code will create a permanent table called TEMP#. To make it a temp table it should have been #TEMP. :-D

_______________________________________________________________

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)
knakka99
knakka99
SSC-Enthusiastic
SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)

Group: General Forum Members
Points: 133 Visits: 126
Thank you Praveena this did the job. :-)

Only it gave an exception if there was a "&" character in the cell, however they were only 5 records like this which I can enter manually.(but just wanted to know if there was a reason or alternative..
knakka99
knakka99
SSC-Enthusiastic
SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)

Group: General Forum Members
Points: 133 Visits: 126
Luis Cazares
I did go through the link and funtion (DelimitedSplit8k). learned for more things. Thank you
knakka99
knakka99
SSC-Enthusiastic
SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)

Group: General Forum Members
Points: 133 Visits: 126
Sean Lange
:-D thank you for correcting me (#Temp) I appreciate it.
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