0

How can I select all that are under 20 and female from a SQL database?

The table has a datetime for the dob and strings for name, surname, and sex. That is the whole table.

I am having trouble calculating the age. I am not even sure this is possible.

I am using SQL Server LocalDB and Visual Studio 2015.

2
  • 2
    It would be best if you showed the CREATE TABLE statement and sample data. Oct 18, 2016 at 18:17
  • 1
    If you can calculate your own age, I'm almost sure you will be able to calculate anyone's, given their date of birth and knowing the today's date.
    – mustaccio
    Oct 19, 2016 at 1:40

2 Answers 2

2

This is totally possible.

SELECT *
FROM <TableName>
WHERE DOB > DATEADD(yy,-20,GETDATE())
  AND Sex = 'F'
0
2

You don't have to calculate the age, only the date that is 20 years ago from now.

ANSI SQL query:

SELECT t.*
FROM table_name AS t
WHERE t.sex = 'F'
  AND t.dob > CURRENT_DATE - INTERVAL '20 YEAR' ;

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