我正在尝试编写一个函数,该函数将截断字段中的前4个字符。例如,如果字段的值是ABC_123_EFG
,则它将返回123_EFG
。我尝试过将LEFT和LEN函数结合使用,但没有取得任何成功。
以下是我认为它应该是。。。
RIGHT(code, LEN(code) - 4) AS code_concat
但它失败了这个错误
Msg 536, Level 16, State 2, Line 2
Invalid length parameter passed to the RIGHT function.
我做错了什么?这是实现这一目标的最佳方式吗?
使用substring
:
substring(code, 5, 1000)
或者,如果是varchar(max)
:
substring(code, 5, len(code))
试试这个。。
select isnull(RIGHT(code, LEN(code) - 4),'') AS code_concat
stuff是一个很好的小函数,在修改字符串时非常有用。
select stuff(code, 1, 4, '')