sql - Regex pattern inside REPLACE function -
select replace('abctemplate1', 'template\d+', ''); select replace('abc_xyztemplate21', 'template\d+', '');
i trying remove part template
followed n digits string. result should be
abc abc_xyz
however replace not able read regex. using sqlserver 2008. doing wrong here? suggestions?
select substring('abctemplate1', 1, charindex('template','abctemplate1')-1)
or
select substring('abc_xyztemplate21',1,patindex('%template[0-9]%','abc_xyztemplate21')-1)
more generally,
select substring(column_name,1,patindex('%template[0-9]%',column_name)-1) sometable patindex('%template[0-9]%',column_name) > 0
you can use substring
charindex
or patindex
if pattern being looked fixed.
Comments
Post a Comment