Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Table as follows
Scheduledate               Description    Mobile     Status
2014-02-06 16:48:09.000       AFF           xxxx       Yes
2014-02-07 16:48:09.000       MFA           xxxx       Yes

From the above table i want to view Scheduledate (date from 6th and 7th date)
for that i written query as follows
SQL
select Scheduledate,Description,Mobile,Status from Shortcode_Course_SMS where Msgdelivered > '20140205' and Msgdelivered < '20140208'

When i execute the above query output as follows
Scheduledate               Description    Mobile     Status
2014-02-06 16:48:09.000       AFF           xxxx       Yes
2014-02-07 16:48:09.000       MFA           xxxx       Yes

But when i write the above query as follows
SQL
select Scheduledate,Description,Mobile,Replymsg  from Shortcode_Course_SMS where Msgdelivered = '20140206' and Msgdelivered = '20140206'

i want to view particular date for example 6th date(2014-02-06 16:48:09.000)
When i run above query output is empty no record is shown
Scheduledate               Description    Mobile     Status

SQL
select Scheduledate,Description,Mobile,Replymsg  from Shortcode_Course_SMS where Msgdelivered = '20140206' and Msgdelivered = '20140206'

from my above query what is the problem please help me.
Regards,
Narasiman P.
Posted
Updated 14-Apr-14 21:03pm
v2
Comments
Bernhard Hiller 15-Apr-14 3:25am    
You hide somethings from us. You do not show us a column "Msgdelivered" - how do you expect us to be able to help you when you do not provide the information?

1 solution

There are two reasons: first your AND condition, which potentially requires that a date has two different values at the same time (I would suggest you want OR instead)

Second because "=" needs exactly the same value on each side - and since these are DATETIME values that means they need to be the same right down to the microsecond. Since you don;t specify a time, '20140206' is midnight on the 6th Feb, which does not match 16:48:09.000 on the 6th Feb.

You can change your query to use
SQL
SELECT Scheduledate,Description,Mobile,Replymsg 
FROM Shortcode_Course_SMS 
WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, Msgdelivered)) = '20140206' 
   OR DATEADD(dd, 0, DATEDIFF(dd, 0, Msgdelivered)) = '20140207'


There is also a better way to write your first query:
SQL
FROM Shortcode_Course_SMS
WHERE Msgdelivered BETWEEN '20140205' AND '20140208'
 
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