Newbie
Newbie

Reputation: 771

SQL Server 2016: How to read different substrings from a text with special characters

I have a string with the value 'Initiator;[email protected]'. I would like to read these two fields in two separate queries. ';' would be the delimiter always.

I used the below query and it works for fetching the email. However it looks overcomplicated to me.

select SUBSTRING(NOTES (CHARINDEX(';',NOTES,1)+1),LEN(NOTES))
from DOCUMENT
where DOC_ID = '12345'

Can someone please help in simplifying it and reading both the values.

Thanks in advance.

Upvotes: 1

Views: 32

Answers (1)

Yogesh Sharma
Yogesh Sharma

Reputation: 50173

First, your query looks fine for email but it has incorrect syntax, second just use left() with charindex() to get the first part :

select left(notes, charindex(';', notes)-1), 
       substring(notes, charindex(';', notes)+1, len(notes))
from document
where doc_id = 12345;

Upvotes: 2

Related Questions