I am writing procedure in which i want to filter date using small date time from date and to date. from date can be null some times and To Date can also be null some time while comparing then how can i filter date when the from date or to date can be null.
I have tried following query but its giving me error.
SELECT RQ.int_REPS_QUES_DIFF_LEVEL,SUM(1) AS NoOFDificultyQuestion FROM
REPS_TEST_QUES_ASSIGNED RQA
INNER JOIN REPS_QUESTION RQ ON RQA.int_REPS_TEST_QUES_ASSG_QUESID=RQ.PK_REPS_QUES_ID
WHERE int_REPS_TEST_ID IN(
SELECT FK_TEST_ID FROM STUDENT_EXAM SE WHERE FK_USER_ID=56 AND SE.FK_REPS_BATCH_ID=466
and CASE
WHEN @FromDate!=NULL AND @ToDate!=NULL
THEN dat_STUD_EXAM_FINALEND >= @FromDate AND dat_STUD_EXAM_FINALEND <= @ToDate
WHEN @FromDate!=NULL AND @ToDate=NULL
THEN dat_STUD_EXAM_FINALEND >= @FromDate
WHEN @FromDate=NULL AND @ToDate!=NULL
THEN dat_STUD_EXAM_FINALEND <= @ToDate
END
)
strong textGROUP BY RQ.int_REPS_QUES_DIFF_LEVEL
i am getting error at
THEN dat_STUD_EXAM_FINALEND >= @FromDate AND dat_STUD_EXAM_FINALEND <= @ToDate
this line please tell me where i am getting wrong
Can you compare datetime in SQL? The right way to compare date only values with a DateTime column is by using <= and > condition. This will ensure that you will get rows where date starts from midnight and ends before midnight e.g. dates starting with '00:00:00.000' and ends at "59:59:59.999".
Yes, If declare DateTime variable as nullable like DateTime? then we can compare it with null. And also to be noted if we compare with null it gives us false result because DateTime cannot understand what is null value and to which component of DateTime type it is actually being compared.
Here we will see, SQL Query to compare two dates. This can be easily done using equals to(=), less than(<), and greater than(>) operators. In SQL, the date value has DATE datatype which accepts date in 'yyyy-mm-dd' format. To compare two dates, we will declare two dates and compare them using the IF-ELSE statement.
Instead of
WHEN @FromDate!=NULL AND @ToDate!=NULL
use
WHEN @FromDate IS NOT NULL AND @ToDate IS NOT NULL
IS [NOT] NULL
If something is NULL
it is undefined in T-SQL, so you cannot compare with it. Both =
and !=
yield false
if one of both (or both) values is NULL
.
Please format your queries. It is much better for reading.
SELECT RQ.int_REPS_QUES_DIFF_LEVEL,
SUM(1) AS NoOFDificultyQuestion
FROM REPS_TEST_QUES_ASSIGNED RQA
INNER JOIN REPS_QUESTION RQ
ON RQA.int_REPS_TEST_QUES_ASSG_QUESID=RQ.PK_REPS_QUES_ID
WHERE int_REPS_TEST_ID IN
(
SELECT FK_TEST_ID
FROM STUDENT_EXAM SE
WHERE FK_USER_ID=56
AND SE.FK_REPS_BATCH_ID=466
AND (
--If both dates are not NULL then also check dat_STUD_EXAM_FINALEND to be between them
(@FromDate IS NOT NULL AND @ToDate IS NOT NULL AND dat_STUD_EXAM_FINALEND >= @FromDate AND dat_STUD_EXAM_FINALEND <= @ToDate )
OR
--If @FromDate is not NULL AND @ToDate IS NULL then also check dat_STUD_EXAM_FINALEND to be greater than @FromDate
(@FromDate IS NOT NULL AND @ToDate IS NULL AND dat_STUD_EXAM_FINALEND >= @FromDate )
OR
--If @FromDate is NULL AND @ToDate IS NOT NULL then also check dat_STUD_EXAM_FINALEND to be less than @ToDate
(@FromDate IS NULL AND @ToDate IS NOT NULL AND dat_STUD_EXAM_FINALEND <= @ToDate )
--Having AND in each set makes it impossible two sets to be true at the same time.
--So If both dates are not null the first set will be evaluated.
--If @ToDate is NULL, then the first and third sets won't be evaluated as they need @ToDate to be not NULL
--If @FromDate is NULL, then the first and second sets won't be evaluated as they need @FromDate to be not NULL
)
) strongtext GROUP BY RQ.int_REPS_QUES_DIFF_LEVEL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With