Port LTRIM from Oracle to SQL Server – How to Guide

oracleplsqlsql servert-sql

Moving an application from Oracle to SQL Server, I have this pseudo Oracle PL/SQL:

SELECT LTRIM(MyColumn, '-, ') FROM MyTable

I.e. I'm using Oracle's LTRIM with a second argument, specifying the characters to trim from the left side of the string.

Unfortunately, the T-SQL version of LTRIM doesn't allow me to specify the characters to trim.

Currently, I'm rather clueless how to migrate that LTRIM. I'm even thinking about processing the results in my hosting C# application, after I read the MyColumn.

This looks rather inelegant to me.

My question:

Is there any meaningful way of getting an LTRIM-like functionality for T-SQL to pass the characters to trim away?

Edit 1:

I need to replace -, , and from the beginning of the string.

E.g.:

   -----, ,,, This is ,- a test,---,

would result in

This is ,- a test,---,

Edit 2:

I strongly hope this isn't an XY problem.

Maybe rewriting my whole query would remove the need for LTRIM altogether, although I would rather focus on porting it as 1:1 as possible and later question the usefulness of the LTRIM.

Best Answer

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 ([^ ,-]).