如何將 LTRIM 從 Oracle 移植到 SQL Server?
將應用程序從 Oracle 遷移到 SQL Server,我有這個偽 Oracle PL/SQL:
SELECT LTRIM(MyColumn, '-, ') FROM MyTable
即,我使用Oracle 的
LTRIM
第二個參數,指定要從字元串左側修剪的字元。不幸的是,T-SQL 版本
LTRIM
不允許我指定要修剪的字元。目前,我對如何遷移它一無所知
LTRIM
。在閱讀了MyColumn
.這在我看來相當不雅。
我的問題:
是否有任何有意義的方法可以讓
LTRIM
T-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 theLTRIM
.`
`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 ([^ ,-]
).`