在 T-SQL 中删除字符串中的最后一个字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1256915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove the last character in a string in T-SQL?
提问by Daveed
How do I remove the last character in a string in T-SQL
?
如何删除字符串中的最后一个字符T-SQL
?
For example:
例如:
'TEST STRING'
to return:
返回:
'TEST STRIN'
回答by AdaTheDev
e.g.
例如
DECLARE @String VARCHAR(100)
SET @String = 'TEST STRING'
-- Chop off the end character
SET @String =
CASE @String WHEN null THEN null
ELSE (
CASE LEN(@String) WHEN 0 THEN @String
ELSE LEFT(@String, LEN(@String) - 1)
END
) END
SELECT @String
回答by Bill Hoenig
If for some reason your column logic is complex (case when ... then ... else ... end), then the above solutions causes you to have to repeat the same logic in the len() function. Duplicating the same logic becomes a mess. If this is the case then this is a solution worth noting. This example gets rid of the last unwanted comma. I finally found a use for the REVERSE function.
如果由于某种原因您的列逻辑很复杂(case when ... then ... else ... end),那么上述解决方案会导致您必须在 len() 函数中重复相同的逻辑。复制相同的逻辑会变得一团糟。如果是这种情况,那么这是一个值得注意的解决方案。这个例子去掉了最后一个不需要的逗号。我终于找到了 REVERSE 函数的用途。
select reverse(stuff(reverse('a,b,c,d,'), 1, 1, ''))
回答by Adrien
Try this:
尝试这个:
select substring('test string', 1, (len('test string') - 1))
回答by Max Grachev
If your string is empty,
如果您的字符串为空,
DECLARE @String VARCHAR(100)
SET @String = ''
SELECT LEFT(@String, LEN(@String) - 1)
then this code will cause error message 'Invalid length parameter passed to the substring function.'
那么此代码将导致错误消息“传递给子字符串函数的长度参数无效”。
You can handle it this way:
你可以这样处理:
SELECT LEFT(@String, NULLIF(LEN(@String)-1,-1))
It will always return result, and NULL in case of empty string.
它将始终返回结果,并在空字符串的情况下返回 NULL。
回答by greg121
select left('TEST STRING', len('TEST STRING')-1)
回答by David Roach
The source text/var can be null or empty:
源文本/var 可以为 null 或为空:
SELECT REVERSE(SUBSTRING(REVERSE(@a), 2, 9999))
SELECT REVERSE(SUBSTRING(REVERSE(@a), 2, 9999))
回答by Behrens
If your coloumn is text
and not varchar
, then you can use this:
如果您的专栏是text
而不是varchar
,那么您可以使用这个:
SELECT SUBSTRING(@String, 1, NULLIF(DATALENGTH(@String)-1,-1))
回答by Daryl
If you want to do this in two steps, rather than the three of REVERSE-STUFF-REVERSE, you can have your list separator be one or two spaces. Then use RTRIM to trim the trailing spaces, and REPLACE to replace the double spaces with ','
如果您想分两步执行此操作,而不是三步 REVERSE-STUFF-REVERSE,您可以将列表分隔符设置为一或两个空格。然后使用 RTRIM 修剪尾随空格,并使用 REPLACE 将双空格替换为 ','
select REPLACE(RTRIM('a b c d '),' ', ', ')
However, this is not a good idea if your original string can contain internal spaces.
但是,如果您的原始字符串可以包含内部空格,这不是一个好主意。
Not sure about performance. Each REVERSE creates a new copy of the string, but STUFF is a third faster than REPLACE.
不确定性能。每个 REVERSE 都会创建一个新的字符串副本,但 STUFF 比 REPLACE 快三分之一。
also see this
也看到这个
回答by farrukh saleem
@result = substring(@result, 1, (LEN(@result)-1))
回答by shA.t
I can suggest this -hack- ;).
我可以建议这个 -hack- ;)。
select
left(txt, abs(len(txt + ',') - 2))
from
t;