Click here to Skip to main content
14,876,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to check if comma separated string
JavaScript
'822632,822633,822634,822635,827402,827403,827404,827405'

have all numeric values??

I tried ISNUMERIC but something feels off

SQL
select ISNUMERIC('822632,822633,822634,822635,827402,827403,827404,827405') -- 0
Select ISNUMERIC('822632,822633') --1
Select ISNUMERIC('8226326125163143146341') --1
Select ISNUMERIC('822632,822633,822') -- 1
Select ISNUMERIC('822632,822633,8243') --0
Select ISNUMERIC('822632323232233,822633') -- 0
Select ISNUMERIC('8226323,8226335612') --0



What are other options and why ISNUMERIC is not working properly

Thanks
Posted

You can use PATINDEX:

SQL
select PATINDEX('%[^0-9,]%', '822632,822633,822634,822635,827402,827403,827404,827405') -- 0
Select  PATINDEX('%[^0-9,]%', '822632,822633') --0
Select  PATINDEX('%[^0-9,]%', '8226326125163143146341') --0
Select  PATINDEX('%[^0-9,]%', '822632,822633,822') -- 0
Select  PATINDEX('%[^0-9,]%', '822632,822633,8243') --0
Select  PATINDEX('%[^0-9,]%', '822632323232233,822633') -- 0
Select  PATINDEX('%[^0-9,]%', '8226323,8226335612') --0
Select  PATINDEX('%[^0-9,]%', '822632,a822633') --8
Select  PATINDEX('%[^0-9,]%', 'a822632,a822633') --1


The pattern [0-9,] looks for any of 1 to 9 or comma, [^0-9,] looks for everything else. If it finds any other char it will return index>0, otherwise zero
   
v2
Comments
[no name] 16-Dec-15 4:39am
   
Andy Lanng +5 thankyou :)
Andy Lanng 16-Dec-15 4:41am
   
Thanks. I only recently discovered it myself ^_^
don't forget to accept the solution
[no name] 16-Dec-15 4:57am
   
for sure! Thanks for full-proof solution
You could try and replace the comma first and then do ISNUMERIC.
Something like this:
SQL
select ISNUMERIC(replace('8226323,8226335612',',','')) AmINumeric

Not sure if it will work on really really very large numbers though.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900