Sql-Server

如何將 LTRIM 從 Oracle 移植到 SQL Server?

  • January 26, 2017

將應用程序從 Oracle 遷移到 SQL Server,我有這個偽 Oracle PL/SQL:

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

即,我使用Oracle 的LTRIM第二個參數,指定要從字元串左側修剪的字元。

不幸的是,T-SQL 版本LTRIM不允許我指定要修剪的字元。

目前,我對如何遷移它一無所知LTRIM。在閱讀了MyColumn.

這在我看來相當不雅。

我的問題:

是否有任何有意義的方法可以讓LTRIMT-SQL 獲得類似 - 的功能來傳遞要修剪的字元?

編輯1:

我需要更換-,並且 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.`

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

引用自:https://dba.stackexchange.com/questions/162227