Why in sql server does the below return false?
SELECT IIF('12345' like '\d+','True','False') as Integer
Why in sql server does the below return false?
SELECT IIF('12345' like '\d+','True','False') as Integer
REGEX is limited in SQL Server... you can use a few different methods. Here's one.
select case when patindex('%[^0-9]%','1234566') = 0 then 'TRUE' else 'FALSE' end
You can also look at ISNUMERIC()
but read this function carefully before using... it returns false positives. E.g. SELECT ISNUMERIC($)
LIKE
does not do regular expressions. It is a very simple pattern matching. SQL Server is a little more powerful then the standard LIKE.
'12345' not like '%[^0123456789]%'
will match all strings that do not contain a non-numeric character. To make sure there is at least one digit and len('123456') > 0
.
Edit: I forgot about character ranges which is more obviously correct, '12345' not like '%[^0-9]%'
.