-2

Why in sql server does the below return false?

SELECT IIF('12345' like '\d+','True','False') as Integer
chris85
  • 23,591
  • 7
  • 30
  • 47
Steve
  • 3
  • 1

2 Answers2

0

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($)

S3S
  • 24,483
  • 5
  • 24
  • 44
0

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]%'.

Shannon Severance
  • 17,303
  • 3
  • 44
  • 66