tags:

views:

151

answers:

6

In SQL how do I exclude a record if there are more than 2 characters after a dash..

Example I only want to return records that match the following AA00000-0

but the table also has recoreds like AA0000-000,AA0000000-00

I need to return only records that have a single digit after the dash

A: 

LIKE '%-[0-9]'

Filler Filler Filler Filler

Remus Rusanu
A: 

Assuming SQL Server:

SELECT * FROM records WHERE id NOT LIKE '%-[0-9][0-9]%'
Anthony Faull
That would give two characters after the dash
HLGEM
Please can you elaborate. It should exclude records with 2 or more digits after a dash.
Anthony Faull
A: 

Use a like statement.

Field Like '%-[0-9]'

Anthony Pegram
A: 

What database are you using?

In MSSQL you could: where fld not like '%-__'

Alex K.
+2  A: 

As mentioned, digit matching is not consistent across databases.

For SQL Server, you can do:

MyColumn LIKE '%-[0-9]' (will get it with only a digit after the dash)

Database-agnostic:

MyColumn LIKE '%-_' (would match a digit or 1 letter after the dash, but that may be sufficient)

Jon Seigel
+1/2 for being agnostic, and +1/2 for interpreting "single digit after the dash" to possibly included single character after dash.
MJB
A: 

thanks for all the answers, the soluition I used was

Like '%-_'

The single underscore represents the first digit after the dash

Pete Kamenszky