sql server - SQL How can I return a specific position from a string? -


i have following string:

declare @request varchar(255) set @request= '**urgent** apple / iphone/ test/future resolution/approved' 

using sql query, how return word 'apple'?

i've tried not work:

select substring(@request,charindex('** ',@request),charindex(' / ',@request)) 

thank input,

lori

you adjust have correct:

 declare @request varchar(255)  set @request= '**urgent** apple / iphone/ test/future resolution/approved'    select substring(@request,charindex('** ',@request) + 3,charindex(' / ',@request)-charindex('** ',@request) -3) 

basically substring needs start , length need adjust parameters.

i feel noting here 1 might naturally try make more generic parameterizing delimiters in someway , using len(first_delim) instead of hardcoding 3 , -3. gotcha here len() ignores trailing space have in both of these delimiters. if delims varchar can instead use datalength, if nvarchar have divide result.

i think worth pointing out datalength doesn't since len works on nvarchar.

select len(n'** ')        ,len(n'** ')       ,datalength('** ')       ,datalength(n'** ')       ,datalength(n'** ')/2 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -