Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
-- 2/.Find all Male employees born from 1962 to 1970 and their hire date greater than 2001
----and all Female employees born between 1972 to 1975 and their hire date between 2001/2002.

select*
From[HumanResources].[Employee]
Where [Gender]='M'and year([BirthDate]) between 1962 and 1970
and year([HireDate])>2001 and
[Gender]='F'and year([BirthDate]) between 1972 and 1975
and year([HireDate])between 2001 and 2002

What I have tried:

select*
From[HumanResources].[Employee]
Where [Gender]='M'and year([BirthDate]) between 1962 and 1970
and year([HireDate])>2001 and
[Gender]='F'and year([BirthDate]) between 1972 and 1975
and year([HireDate])between 2001 and 2002
Posted
Updated 22-Nov-17 23:01pm
Comments
Laxmidhar tatwa technologies 25-Nov-17 4:06am    
the data type of BirthDate in thes case should be int

1 solution

Because AND requires both sides of the expression to be true in order for teh result to be true.
Let's simplify your query a bit:
SQL
SELECT  *  FROM [HumanResources].[Employee]
WHERE [Gender]='M' AND [Gender]='F'
Since Gender cannot be both M and F at ethe same time, no rows are returned.
I suspect you want something like this:
SQL
SELECT * FROM [HumanResources].[Employee]
WHERE 
   (    [Gender]='M' 
    AND YEAR([BirthDate]) BETWEEN 1962 AND 1970 
    AND YEAR([HireDate]) >2001
   ) OR
   (    [Gender]='F'
    AND YEAR([BirthDate]) between 1972 and 1975
    AND YEAR([HireDate])between 2001 and 2002
   )
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900