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

SqlSql ServerTsql

Sql Problem Overview


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

Sql Solutions


Solution 1 - Sql

Use this:

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

MSDN - LIKE (Transact-SQL)

Solution 2 - Sql

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' 

Solution 3 - Sql

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

Solution 4 - Sql

  1. You could use CLR based UDFs or do a CONTAINS query using all the digits on the search column.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionManishView Question on Stackoverflow
Solution 1 - SqlcjkView Answer on Stackoverflow
Solution 2 - SqlkevchaddersView Answer on Stackoverflow
Solution 3 - SqleksortsoView Answer on Stackoverflow
Solution 4 - SqlTrustyCoderView Answer on Stackoverflow