-1

This is the data I have, it's separated by carriage returns.

'AM 1492       AM 1791       PM 1492       PM 1791       '

Desired results:

screenshot of data on each row

How do I get each piece of data on its own row? I've tried CharIndex to identify the character and I was thinking of using a substring, but I'm not sure how to separate each item into a row when there are multiple instances of a string like 'AM', 'AM', etc.

CREATE TABLE #T1
(
    StringInfo VARCHAR (500)
)

INSERT INTO #T1 (StringInfo)
VALUES ('AM 1492       AM 1791       PM 1492       PM 1791       ')

SELECT * 
FROM #T1 AS T
2
  • 1
    Doesn't look like it's separated by a carriage return, just a bunch of spaces. Commented Sep 1, 2022 at 19:38
  • CROSS APPLY STRING_SPLIT(REPLACE(T.StringInfo, ' ', '|'), '|') should do the trick Commented Sep 1, 2022 at 21:12

1 Answer 1

2

If it truly is a carriage return then the following should do the job:

CREATE TABLE #T1
(
    StringInfo NVARCHAR (500)
)

INSERT INTO #T1 (StringInfo)
VALUES ('AM 1492
AM 1791
PM 1492
PM 1791')

SELECT value
FROM #T1 cross apply string_split(StringInfo, Char(13)) as p

value
AM 1492
AM 1791
PM 1492
PM 1791
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, @JNevill! I appreciate you taking the time to help me! Your answer worked for me!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.