Atul Patil

Atul Patil

  • 847
  • 586
  • 71k

How to find Check current time is in break or not?

Nov 13 2019 7:07 AM
Table Name - Shift
  1. CREATE TABLE [dbo].[shift](  
  2. [shift_id] [int] IDENTITY(1,1) NOT NULL,  
  3. [shift_name] [varchar](50) NULL,  
  4. [from_time] [datetime] NULL,  
  5. [to_time] [datetime] NULL,  
  6. [added_by] [intNULL,  
  7. [modified_by] [intNULL,  
  8. [added_on] [datetime] NULL,  
  9. [modified_on] [datetime] NULL,  
  10. [is_active] [bitNULL,  
  11. [venue_id] [intNULL,  
  12. CONSTRAINT [PK__shift__7B267220887FF64E] PRIMARY KEY CLUSTERED  
  13. (  
  14. [shift_id] ASC  
  15. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  16. ON [PRIMARY]  
  17. GO  
Table Name - Rel Shift break Schedule
  1. CREATE TABLE [dbo].[rel_shift_break_schedule](  
  2. [break_id] [int] IDENTITY(1,1) NOT NULL,  
  3. [break_name] [varchar](250) NULL,  
  4. [break_start_time] [datetime] NULL,  
  5. [break_end_time] [datetime] NULL,  
  6. [venue_id] [intNULL,  
  7. [shift_id] [intNULL,  
  8. [zone_id] [intNULL,  
  9. [added_by] [intNULL,  
  10. [added_on] [datetime] NULL,  
  11. [modified_by] [intNULL,  
  12. [modified_on] [datetime] NULL,  
  13. [is_active] [bitNULL,  
  14. PRIMARY KEY CLUSTERED  
  15. (  
  16. [break_id] ASC  
  17. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  18. ON [PRIMARY]  
  19. GO  
Question - Each shift have multiple breaks. So, how to check current time is in break or not. Result is return True or False. Following is my running query, its working perfect, but i dont want to use WHILE LOOP because of permormance impact. So please suggest me a better option over WHILE LOOP.
  1. DECLARE @shift_id INT = 65  
  2. DROP TABLE #shiftListCnt  
  3. CREATE TABLE #shiftListCnt(  
  4. ID INT IDENTITY(1, 1),  
  5. shift_id INT,  
  6. break_start_time DATETIME,  
  7. break_end_time DATETIME  
  8. )  
  9. INSERT INTO #shiftListCnt  
  10. SELECT shift_id, break_start_time,break_end_time  
  11. FROM rel_shift_break_schedule WHERE shift_id = @shift_id AND is_active = 1  
  12. DECLARE  
  13. @check_time_hr_int AS FLOAT,  
  14. @boundry_from_time_hr_int AS FLOAT,  
  15. @boundry_to_time_hr_int AS FLOAT,  
  16. @result AS INT,  
  17. @check_time VARCHAR (10);  
  18. DECLARE @inirow INT = 1;  
  19. DECLARE @NumberofRowint INT;  
  20. DECLARE @venue_id INT;  
  21. DECLARE @boundry_from_time VARCHAR (10),  
  22. @boundry_to_time VARCHAR (10);  
  23. SELECT @venue_id = venue_id FROM rel_shift_break_schedule WHERE shift_id = 65  
  24. SET @NumberofRowint = (select COUNT(*) from #shiftListCnt)  
  25. SET @check_time = CAST (dbo.fnGetAirportDate(@venue_id) AS TIME-- VENUE TIME  
  26. SELECT @check_time venue_time, @NumberofRowint  
  27. While @inirow <= @NumberofRowint  
  28. BEGIN  
  29. SET @boundry_from_time = CAST((SELECT break_start_time FROM #shiftListCnt WHERE ID=@inirow) AS TIME)  
  30. SET @boundry_to_time = CAST((SELECT break_end_time FROM #shiftListCnt WHERE ID=@inirow) AS TIME)  
  31. SET @check_time_hr_int = CONVERT (DECIMAL (10, 2), CONVERT (FLOAT, DATEDIFF(MINUTE, 0, @check_time))) / 60;  
  32. -- IF (@check_time_hr_int < 1)  
  33. -- BEGIN  
  34. -- -- SET @check_time_hr_int += 24;  
  35. -- END  
  36. SET @boundry_from_time_hr_int = CONVERT (DECIMAL (10, 2), CONVERT (FLOAT, DATEDIFF(MINUTE, 0, @boundry_from_time))) / 60;  
  37. SET @boundry_to_time_hr_int = CONVERT (DECIMAL (10, 2), CONVERT (FLOAT, DATEDIFF(MINUTE, 0, @boundry_to_time))) / 60;  
  38. IF (@boundry_from_time_hr_int > @boundry_to_time_hr_int)  
  39. BEGIN  
  40. SET @boundry_to_time_hr_int += 24;  
  41. IF (@check_time_hr_int <= 12 AND @boundry_from_time_hr_int > 12)  
  42. BEGIN  
  43. SET @check_time_hr_int += 24;  
  44. END  
  45. END  
  46. IF (@check_time_hr_int >= @boundry_from_time_hr_int  
  47. AND @check_time_hr_int <= @boundry_to_time_hr_int)  
  48. BEGIN  
  49. SET @result = 1;  
  50. END  
  51. ELSE  
  52. BEGIN  
  53. SET @result = 0;  
  54. END  
  55. SET @inirow = @inirow + 1;  
  56. END  
  57. SELECT @result status  
  58. --RETURN @result;  

Answers (2)