SQL 如何检测一个字符串是否至少包含一个数字?

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

How to detect if a string contains at least a number?

sqlsql-servertsql

提问by Manish

How to detect if a string contains at least a number (digit) in SQL server 2005?

SQL Server 2005 中如何检测一个字符串是否至少包含一个数字(数字)?

回答by cjk

Use this:

用这个:

SELECT * FROM Table WHERE Column LIKE '%[0-9]%'

MSDN - LIKE (Transact-SQL)

MSDN - LIKE (Transact-SQL)

回答by kevchadders

DECLARE @str AS VARCHAR(50)
SET @str = 'PONIES!!...pon1es!!...p0n1es!!'

IF PATINDEX('%[0-9]%', @str) > 0
   PRINT 'YES, The string has numbers'
ELSE
   PRINT 'NO, The string does not have numbers' 

回答by eksortso

The simplest method is to use LIKE:

最简单的方法是使用LIKE

SELECT CASE WHEN 'FDAJLK' LIKE '%[0-9]%' THEN 'True' ELSE 'False' END;  -- False
SELECT CASE WHEN 'FDAJ1K' LIKE '%[0-9]%' THEN 'True' ELSE 'False' END;  -- True

回答by TrustyCoder

  1. You could use CLR based UDFs or do a CONTAINS query using all the digits on the search column.
  1. 您可以使用基于 CLR 的 UDF 或使用搜索列上的所有数字执行 CONTAINS 查询。