如何删除 SQL Server 中某个字符之前的所有内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31663131/
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
How to remove everything before a certain character in SQL Server?
提问by Musi
I have entered data into my SQL server. I want to remove all the characters before a hyphen. There are different amounts of characters before the "-".
我已将数据输入到我的 SQL 服务器中。我想删除连字符之前的所有字符。“-”前有不同数量的字符。
For Example:
例如:
ABC-123
AB-424
ABCD-53214
I want this result:
我想要这个结果:
123
424
53214
I am new to SQL Server and really need help.
Thank You in advance.
我是 SQL Server 的新手,真的需要帮助。
先感谢您。
回答by APH
Try this:
尝试这个:
right(MyColumn, len(MyColumn) - charindex('-', MyColumn))
Charindex
finds the location of the hyphen, len
finds the length of the whole string, and right
returns the specified number of characters from the right of the string.
Charindex
查找连字符的位置,len
查找整个字符串的长度,并right
从字符串的右侧返回指定数量的字符。
回答by mohan111
may be the other way you can do it by using reverse and Char Index
可能是您可以通过使用 reverse 和 Char Index 来做到这一点的另一种方式
DECLARE @Table1 TABLE
(val varchar(10))
;
INSERT INTO @Table1
(val)
VALUES
('ABC-123'),
('AB- 424'),
('ABCD-53214')
select reverse(substring(reverse(val),0,CHARINDEX('-',reverse(val)))) from @Table1