Click here to Skip to main content
15,887,827 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I am building an Application that is working 24 hours daily. I have a table Name PrayerTimes. I want to show Notification to Customers That It's Prayer Time. I want to get query to return True or False. True should Start Before 5 Minutes of Prayer Time and 30 minutes after Prayer Time...

Let Us say if next prayer time is 2016-10-23 15:23:00.000 then 'True' will From 2016-10-23 15:18:00.000 To 2016-10-23 15:53:00.000 else should return 'False'

Please help me to build query or any stored procedure to return just True or False. Here is the Table of PrayerTimes

ID	PRAYER_ID	        TIME
775	5	                2016-10-23 19:20:00.000
776	1	                2016-10-24 05:04:00.000
777	2	                2016-10-24 12:05:00.000
778	3	                2016-10-24 15:23:00.000
779	4	                2016-10-24 17:50:00.000
780	5	                2016-10-24 19:20:00.000

What I have tried:

SELECT CAST(
CASE 
WHEN DATEDIFF(minute, GETDATE(), TIME) > -5 AND DATEDIFF(minute, GETDATE(), TIME) < 30 
THEN 1 
ELSE 0 
END AS bit) as TIME, GETDATE() 
FROM prayertimes

it's always 0
Posted
Updated 23-Oct-16 4:36am

Well, yes - it will be. The first time is at 19:20 today, and all the others are due tomorrow. DATEDIFF includes the Year, month, and day when it calculates it.
Try:
SQL
SELECT CAST(
CASE WHEN DATEDIFF(minute, GETDATE(), [TIME]) > -5 AND DATEDIFF(minute, GETDATE(), [TIME]) < 30 
THEN 1 
ELSE 0 
END AS bit) as TIME, GETDATE() 
FROM prayertimes
 
Share this answer
 
Not sure if I understand the requirement correctly, but if you only need to know if it's currently prayer time, why fetch all the rows from PrayerTimes table? Why not just check if the time is now.

For example the following query would return a row 5 starting 5 minutes before prayer time and ending 30 minutes after. If a row is returned, it's time if no rows are returned, it's not time
SQL
SELECT pt.[Time], 1 AS PrayerTime
FROM PrayerTimes pt
WHERE pt.[Time] BETWEEN DATEADD(MINUTE, -5, GETDATE()) AND DATEADD(MINUTE, 30, GETDATE())
 
Share this answer
 
SELECT TOP 1 CAST(
CASE
WHEN DATEDIFF(minute, GETDATE(), pTIME) >= -30 AND DATEDIFF(minute, GETDATE(), pTIME) <= 5
THEN 1
ELSE 0
END AS bit) as true_false, pTIME, getdate(), datediff(minute,getdate(),pTIME), DATEADD(MINUTE, -1, GETDATE())
FROM prayertimes
where DATEADD(MINUTE, -30, GETDATE()) <= pTIME order by pTIME asc
 
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