96

I have a column in my database called "dob" of type datetime. How do I select all the rows after a specific DoB in SQL Server 2005?

0

2 Answers 2

148

Simply put:

SELECT * 
FROM TABLE_NAME
WHERE
dob > '1/21/2012'

Where 1/21/2012 is the date and you want all data, including that date.

SELECT * 
FROM TABLE_NAME
WHERE
dob BETWEEN '1/21/2012' AND '2/22/2012'

Use a between if you're selecting time between two dates

4
  • 4
    What rules apply with regard to the formatting of the date? I tested with "1/12/2012 13:14:33.000" and "1-12-2012 13:14:33.000" and both work.
    – Baz
    Commented Mar 2, 2012 at 11:50
  • 1
    The example is misleading. 12 is not the month! It is the day and it is in american format. It is in this format: 'mm/dd/yyyy' example SELECT * FROM TABLE_NAME WHERE dob > '1/13/2012. Now is clear that 13 is not month.
    – Daniel B
    Commented Sep 24, 2018 at 5:46
  • 2
    @Daniel when in doubt, it is better to specify the conversion as in WHERE dob >= Convert(datetime, '2019-12-15' )
    – helcode
    Commented Dec 28, 2019 at 2:04
  • For MySQL, you can use NOW() within your SQL statement, to get current DateTime. For more details, please refer to: dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html
    – Dainank
    Commented Jan 17 at 8:45
1

Let's say you want to get all records from a table called Table_One with a datetime column called date_value that have happened in the past six months...

CREATE TABLE (
  date_value DATETIME
) 

SELCECT *
FROM Table_One
WHERE date_value > DATEADD(month, -6, getdate());

This gives a bit more dynamic of a solution.

1
  • What is DATEADD? Commented Nov 30, 2022 at 19:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.