sql - MSSQL Substring and keep the last word intact -
i have following example string:
this string large, has more 160 characters. can cut substring has 160 characters cuts of last word looks kind of stupid.
now want have round 160 characters, use substring()
select substring('this string large, has more 160 characters. can cut substring has 160 characters cuts of last word looks kind of stupid.', 0 , 160)
wich results in:
this string large, has more 160 characters. can cut substring has 160 characters cuts of last word l
now need find way finish off last word, in case word looks
any idea whats best way approach problem?
declare @s varchar(500)= 'this string large, has more 160 characters. can cut substring has 160 characters cuts of last word looks kind of stupid.' select case when charindex(' ', @s, 160) > 0 substring(@s, 0, charindex(' ', @s, 160)) else @s end
Comments
Post a Comment