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.

closed as unclear what you're asking by ypercubeᵀᴹ, Tom V, MDCCL, Philᵀᴹ, mustaccio Oct 19 '16 at 1:41

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    It would be best if you showed the CREATE TABLE statement and sample data. – ypercubeᵀᴹ Oct 18 '16 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 '16 at 1:40
2

This is totally possible.

SELECT *
FROM <TableName>
WHERE DOB > DATEADD(yy,-20,GETDATE())
  AND Sex = 'F'
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.