I'm trying to cut off everything after a certain character (in this case '&') and if there is no occurence of that character, get the entire string.
select left(@str,isnull(nullif(charindex('&',@str) - 1,-1),len(@str)))
This seems to be a rather needlessly complicated way of doing it. Is there a better/shorter way?
Here is a full test case to indicate what I'm doing...
declare @str as varchar(255)
set @str = 'abc&def&ghi'
select left(@str,isnull(nullif(charindex('&',@str) - 1,-1),len(@str)))
-- Returns 'abc'
set @str = 'abc'
select left(@str,isnull(nullif(charindex('&',@str) - 1,-1),len(@str)))
-- Returns 'abc'