views:

22

answers:

4

Hi Guys,

I have a SQL database table column which has data in this format "0000-00-0000"

for ex: "1234-98-2010"

For it has data in other formats as well.

I need to pick out all the records where the format is "0000-00-0000" irrespective of the data. Its the format which i am after

Please, can some one advise me on how i can pick these records in sql server 2005

+1  A: 

Use the like operator with a pattern

Select * from TableA
Where ColumnA like '[0123456789][0123456789][0123456789][0123456789]-[0123456789][0123456789]-[0123456789][0123456789][0123456789][0123456789]'
John Hartsock
+4  A: 
;WITH T AS
(
SELECT '1234-98-2010' AS S UNION ALL SELECT 'foo'
)
SELECT S 
FROM T 
WHERE S LIKE '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]'
Martin Smith
+1. This will ensure the data has numbers only, whereas my solution left it open for any character.
p.campbell
lovely that worked like a dream. Wish i could do +10 for this. Cheers
Amit
+1  A: 

Try:

 SELECT * FROM MyTable WHERE MyColumn LIKE '____-__-____'

The _ is a wildcard for one character.

p.campbell
This would include letters and numbers. and I know the question doesn't specifically say..but I believe he only wants numbers between dashes. "1234-56-7890' not 'abcd-ef-ghij'
John Hartsock
@John: indeed; I commented the same on Martin's answer. Thanks for your insight. OP asked for **where the format is "0000-00-0000" irrespective of the data**
p.campbell
+1  A: 
SELECT * FROM sometable
WHERE c1 LIKE '____-__-____'
Brian Driscoll