Get the text between two characters in sql server -
i have tried using charindex function unable it.
text :- offshore/blr/in
i want blr" out of this.
please help.
presuming asking how text between forward-slashes, using charindex
.
see following example:
declare @text varchar(100) set @text = '- offshore/blr/in' print charindex('/', @text) print charindex('/', @text, charindex('/', @text) + 1)
this output 11
, 15
, respectively, shows position of first , second forward-slash.
the second position found searching text occurs after first position.
following it's case of using substring
extract text middle of slashes:
print substring(@text, charindex('/', @text) + 1, charindex('/', @text, charindex('/', @text)+ 1) - charindex('/', @text) - 1)
this gets text between first slash (+1 position exclude slash), position of second slash minus position of first slash minus 1 (to remove first slash). basically:
print substring(@text, 11 + 1, 15 - 11 - 1)
note text not contain both slashes code fail. contains no bounds or error checking @ all, , should add this.
Comments
Post a Comment