How to port LTRIM from Oracle to SQL Server?
Yeehaw.
DECLARE @MyTable TABLE ( MyColumn VARCHAR(100) );
INSERT @MyTable ( MyColumn )
VALUES ( '-----, ,,, This is ,- a test,---,' );
SELECT *,
SUBSTRING(mt.MyColumn, ca.p, LEN(mt.MyColumn))
FROM @MyTable AS mt
CROSS APPLY ( SELECT *
FROM (VALUES (PATINDEX ('%[^ ,-]%', MyColumn))
) AS x (p) ) AS ca;
Take the suffix of the string starting from the first character which is not a space, comma or hyphen:
declare @str varchar(100) = ' -----, ,,, This is ,- a test,---,'
select substring(@str,patindex('%[^ ,-]%',@str),len(@str))
Result:
This is ,- a test,---,
Please note that the hyphen, since it is a special character in regular expressions meaning 'range' (e.g. [a-z]
), must be either first ([^- ,]
) or last ([^ ,-]
).
I don't think you'll be happy with this solution compared to Oracle's ltrim
but it does what you want.
declare @Pattern varchar(32) = '-, ';
select case when MyColumn like '['+@Pattern+']%'
then right(MyColumn
, len(MyColumn)-(patindex('%[^'+@Pattern+']%',MyColumn)-1)
)
else MyColumn
end
from MyTable
rextester: http://rextester.com/IXOL62563