0

IsNumeric allows below special characters

`SELECT  ISNUMERIC('10.50') as '10.50'
 ISNUMERIC('10,50') as '10,50'
 ,ISNUMERIC('-') as '-'
 ,ISNUMERIC('+') as '+'
 ,ISNUMERIC('$') as '$'
 ,ISNUMERIC('.') as '.'
 ,ISNUMERIC(',') as ','
 ,ISNUMERIC('\') as '\'`

So, I wanted to use RegEx https:// regexr.com/4gjhm to validate my numeric which must allow numbers and dot only.

Tried Regex ^[0-9]+[.]{0,1}[0-9]*$ with the help of PATINDEX, but didn't get the expected results

 `DECLARE @Amount As NVARCHAR(50) = '150';
  SELECT CASE 
            WHEN @Amount LIKE '%^[0-9]+[.]{0,1}[0-9]*$%'
            THEN 1
            ELSE 0
            END AS IsNumericResult`

Valid Ex: 10, 10.01, .20 Invalid Ex: 10.50.20, 10..20, etc

Is this possible to achieve through LIKE or PATINDEX in SQL Server 2017?

7
  • %^ ...$% seems a bit contradictory to me. Commented Jun 28, 2019 at 7:10
  • 2
    ISNUMERIC() won't help there as you already state, instead you can use TRY_CAST() / TRY_CONVERT(). Without the needs to Regex. Commented Jun 28, 2019 at 7:16
  • how about remove . and digits and then see inf there's anything left in the string... Commented Jun 28, 2019 at 7:35
  • What is the end goal here? Why are you validating that the string is of that pattern? Is it to detect if it will cast to numeric without error or some other reason? Commented Jun 28, 2019 at 7:58
  • The problem is, SQL Server does not support regular expressions. Whereas you can match single characters with a range [0-9] or set [01234] you can't use + or * to match an instictinct count of them. Ie something like [0-9]+ will match 1+ but not 123. So you might be best off with @Sami answer and add some additional tests to exclude for instance +123 or -1.5 Commented Jun 28, 2019 at 8:12

1 Answer 1

3

You can use TRY_CAST() or TRY_CONVERT() without the need to Regex expression

SELECT CASE WHEN TRY_CAST(@Amount AS INT) IS NULL THEN 0 ELSE 1 END IsNumericResult

You can change INT there to numeric or decimal as needed.

eg:

SELECT V Value,
       CASE WHEN TRY_CAST(V AS DECIMAL(10,2)) IS NULL
            THEN 0
            ELSE 1
       END IsNumericResult
FROM
(VALUES ('10.50'), ('+'), ('$'), ('-'), ('11')) T(V);

Here is a db<>fiddle to see how it's working.

Sign up to request clarification or add additional context in comments.

5 Comments

This will also allow +10.5 and -11 which is not valid according to the op original question.
Try ` ('+0'), ('$0'), ('-0') ` the only difference with ISNUMERIC is ('$0')
@derpirscher 1st the OP doesn't state that in his Q, 2nd -11 isn't a number??? From where did that comes to your mind? There is no negative/positive numbers?
For the specific example strings they gave this has the desired effect. rextester.com/VJX9909
@Sami citing from the op question: "validate my numeric which must allow numbers and dot only" Yes -11 is a number, but OP does not want to allow it, at least as I understand his question (and his inital regex which does not contain a negative sign)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.