Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am beginner in SQL.. unable to fetch Employee details where JoinYear<=@Year and DateofLeave=null or DateofLeave=@Year..

I searched this on google but i didn't get right solution..
plz help
Posted
Updated 15-Feb-15 21:18pm
v2

First thing to look at is the actual values you are comparing: if they are dates (and it's normal to store Join dates and leave dates as DateTime rather than just a year value as almost nobody starts of finishes on Jan 1st) then equality tests don't often work as they are accurate to milliseonds!

The next thing is to look at your condition.
A AND B OR C

Is that
(A AND B) OR C
Or
A AND (B OR C)
There is an important difference in there! If you aren't exactly sure how SQL (or any other system) will treat a condition, then use the appropriate brackets to force exactly the intrepretation you need.
I'm guessing you want
A AND (B OR C)
but I could be wrong.

Finally, check the values you are passing to to check against, and the DB values you are referring to: look at what you think you should get, and then at exactly what you do get. If they don't match, there is likely to be a pattern in the delivered data which will point you in the right direction.

Sorry, but without the data you are using, we can't be specific on a solution.
 
Share this answer
 
Comments
itsathere 16-Feb-15 3:30am    
all i need is A AND (B OR C)
OriginalGriff 16-Feb-15 3:47am    
So add the brackets, and see what happens...
itsathere 16-Feb-15 3:51am    
still not working..
itsathere 16-Feb-15 3:57am    
sorry, it's working in sql but same condition not working in linq
OriginalGriff 16-Feb-15 4:06am    
So show us the Linq?

Remember that we can't see your screen, access your HDD, or read your mind.
:laugh:
Not sure if I understood your requirement correctly, but if the data is in date format you need to extract the year portion. So perhaps somethig like
SQL
SELECT ...
FROM TableName
WHERE DATEPART(year, JoinDate) <= @year
AND   COALESCE(DATEPART(year, LeaveDate), @year) = @year
 
Share this answer
 
v3
Comments
itsathere 16-Feb-15 4:20am    
u have great idea but LeaveDate will be null sometime if employee is still working..
Wendelius 16-Feb-15 4:44am    
That is why I used the COALESCE. It returns the first non null value :)
itsathere 16-Feb-15 5:51am    
thanks for your valuable comments

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