2

I want to check for a pattern in sql such that if there is anything in that expression (or table cell) other than numeric it should return 1. If that whole cell has only numeric values it should return 0

eq:

case when '200290' like [anything other than numbers]
then 1
else o
pirho
  • 11,565
  • 12
  • 43
  • 70
user708477
  • 147
  • 2
  • 4
  • 14

2 Answers2

2

In SQL Server, you can use something like (I'm not writing the whole function for you):

DECLARE @t varchar(100) = '231321321321'

SELECT CASE WHEN PATINDEX('%[^0-9]%', @t) > 0 THEN 1
            ELSE 0 END
JNK
  • 63,321
  • 15
  • 122
  • 138
  • @user708477 It's a common problem and a common solution. `ISNUMERIC()` doesn't work as expected so we have to come up with workarounds! – JNK Oct 07 '11 at 18:04
  • As of Teradata 13.10, you would need to replicate PATINDEX() functionality as a UDF. – Rob Paller Oct 10 '11 at 13:20
0
SELECT CASE CHARACTERS(OTRANSLATE(<your_string>,'a1234567890','a')) 
WHEN 0 THEN 0 ELSE 1 END;

Ex.:

     -- BTEQ  Enter your SQL request or BTEQ command:
SELECT CASE CHARACTERS(OTRANSLATE('12345','a1234567890','a')) 
WHEN 0 THEN 0 ELSE 1 END;

* Query completed. One row found. One column returned. * Total elapsed time was 1 second.

<CASE  expression>
------------------
                 0

SELECT CASE CHARACTERS(OTRANSLATE('1a23b45c','a1234567890','a')) WHEN 0 THEN 0 ELSE 1 END;

* Query completed. One row found. One column returned. * Total elapsed time was 1 second.

<CASE  expression>
------------------
                 1
Rocketq
  • 5,423
  • 23
  • 75
  • 126
CarlosAL
  • 31
  • 3