add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
select str
, new_str = case when patindex('%-%-%', str) > 0
then stuff( stuff(str, charindex('-',str),1,'|')
,charindex('-',str,charindex('-', str)+1)
,1,'|')
else str end
from (values
('UTR-ATLAS-006-0143391-002')
, ('NO DASHES IN THIS STRING')
) test(str)
str new_str
UTR-ATLAS-006-0143391-002 UTR|ATLAS|006-0143391-002
NO DASHES IN THIS STRING NO DASHES IN THIS STRING
select str
, new_str = replace(left(str, charindex('-', str, charindex('-', str)+1)), '-', '|')
+ right(str, len(str) - charindex('-', str, charindex('-', str)+1))
from (values
('UTR-ATLAS-006-0143391-002')
, ('NO DASHES IN THIS STRING')
) test(str)
str new_str
UTR-ATLAS-006-0143391-002 UTR|ATLAS|006-0143391-002
NO DASHES IN THIS STRING NO DASHES IN THIS STRING
select str
, new_str = (select stuff(string_agg(iif(rn<=2+1,'|','-')+value,''),1,1,'')
from (select value, row_number() over (order by (select null)) rn from string_split(str, '-')) q )
from (values
('UTR-ATLAS-006-0143391-002')
, ('NO DASHES IN THIS STRING')
) test(str)
str new_str
UTR-ATLAS-006-0143391-002 UTR|ATLAS|006-0143391-002
NO DASHES IN THIS STRING NO DASHES IN THIS STRING