SQL 检查字符串是否包含前导字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4161157/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 08:15:23  来源:igfitidea点击:

Check if string contains leading letters

sqlsql-serversql-server-2000

提问by Musikero31

How can I check if my string contains leading letters? In C# is easy, but I am doing this in SQL. Is there a way to check this? If so, how can I remove it?

如何检查我的字符串是否包含前导字母?在 C# 中很容易,但我在 SQL 中这样做。有没有办法检查这个?如果是这样,我该如何删除它?

EX: @MyString = 'A1234'

例如:@MyString = 'A1234'

Updated string = '1234'

更新字符串 = '1234'

回答by OMG Ponies

Use:

用:

UPDATE YOUR_TABLE
   SET your_column = SUBSTRING(your_column, 2, DATALENGTH(your_column))
 WHERE your_column LIKE '[A-Za-z]%'

回答by BeemerGuy

For one leading letter, you can do:

对于一封前导信,您可以执行以下操作:

IF NOT ISNUMERIC(SUBSTRING(@MyString, 1, 1))
    SET @MyString = SUBSTRING(@MyString, 2, LEN(@MyString)) 

You can repeat that until there are no more letters.

您可以重复此操作,直到没有更多字母为止。

回答by Saif Khan

Try wrapping SUBSTRING in an IF...if you want to check

尝试将 SUBSTRING 包装在 IF 中...如果您想检查

substring(@MyString,1,1) = 'A'

or just

要不就

declare @val varchar(10)

set @val = substring(@MyString,1,1)