I want to get only those rows that contain ONLY certain characters in a column.
Let's say the column name is DATA.
I want to get all rows where in DATA are ONLY (must have all three conditions!):
- Numeric characters (
1 2 3 4 5 6 7 8 9 0
) - Dash (
-
) - Comma (
,
)
For instance:
Value "10,20,20-30,30"
IS OK
Value "10,20A,20-30,30Z"
IS NOT OK
Value "30"
IS NOT OK
Value "AAAA"
IS NOT OK
Value "30-"
IS NOT OK
Value "30,"
IS NOT OK
Value "-,"
IS NOT OK
Try patindex:
select * from(
select '10,20,20-30,30' txt union
select '10,20,20-30,40' txt union
select '10,20A,20-30,30Z' txt
)x
where patindex('%[^0-9,-]%', txt)=0
For you table, try like:
select
DATA
from
YourTable
where
patindex('%[^0-9,-]%', DATA)=0
As per your new edited question, the query should be like:
select
DATA
from
YourTable
where
PATINDEX('%[^0-9,-]%', DATA)=0 and
PATINDEX('%[0-9]%', LEFT(DATA, 1))=1 and
PATINDEX('%[0-9]%', RIGHT(DATA, 1))=1 and
PATINDEX('%[,-][-,]%', DATA)=0