Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for two decimal digit number in string

I have two sets of records

Set 1:

-11
-12
-12 AN    
''    
-134
-125
+135

Set 2:

1.15
1.1

In Set 1 I need to check which values are either blank '' or start with a + sign and are greater than 125.

In Set 2 I need to check which values have less than two decimal places

Example output for the above sets:

''
+135
1.1
like image 739
akhrot Avatar asked Jun 12 '15 11:06

akhrot


4 Answers

In SQL-Server could be something like that:

WITH cte AS (
SELECT Col
FROM set1
WHERE Col = '' OR Col LIKE'+%' AND (CAST(REPLACE(REPLACE(Col,'+',''),'-','') AS INT) > 125)
)
SELECT * FROM cte
UNION ALL
SELECT Col
FROM set2
WHERE Col LIKE '%._'

OUTPUT:

''  -- blank
+135
1.1

SQL FIDDLE

like image 100
Stanislovas Kalašnikovas Avatar answered Oct 30 '22 14:10

Stanislovas Kalašnikovas


For the first set, you can use the like operator to check if a string starts with '+' and then cast it to numeric and compare it with 125. Using isnumeric beforehand will help avoid casting errors:

WHERE col = '' OR
      (col LIKE '+%' AND ISNUMERIC(col) AND CAST(col AS NUMERIC) > 125)

For the second set, you can use the like operator with _, the single character wildcard:

WHERE col NOT LIKE '%.__%'
like image 21
Mureinik Avatar answered Oct 30 '22 12:10

Mureinik


One way:

where f = '' or (f like '+%' and isnumeric(f) = 1 and f > 0)

where isnumeric(f) = 1 and f like '%.[0-9]'
like image 25
Alex K. Avatar answered Oct 30 '22 14:10

Alex K.


First:

WHERE v = '' OR (v NOT LIKE '%[^+0-9]%' AND v > 125)

Second:

WHERE v NOT LIKE '%[^.0-9]%' AND (v LIKE '%._' OR (v NOT LIKE '%.%' AND v LIKE '%_%'))

For decimals:

WHERE FLOOR(v*10) = v*10
like image 32
Giorgi Nakeuri Avatar answered Oct 30 '22 14:10

Giorgi Nakeuri