How to detect if a string contains atleast a number (digit) in SQL server 2005?
+6
A:
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'
kevchadders
2010-04-01 07:36:09
Why PATINDEX, rather than a simple LIKE?
gbn
2010-04-01 10:53:20
A:
- You could use CLR based UDFs or do a CONTAINS query using all the digits on the search column.
nitroxn
2010-04-01 07:42:55
A:
The simplest method is to use 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
eksortso
2010-04-01 07:49:03