简体   繁体   中英

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

I have a string with the value 'Initiator;abcd@gmail.com'. 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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM