Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Time Interval

Author  Topic 

dia1234
Starting Member

12 Posts

Posted - 2013-07-11 : 06:25:38
I want to check a record in my table which has time ins and time outs feilds. I want to check on insertionof any new record that a new record should not be inserted on same timings and before 45 mins of timein and after 45 mins of timeout.

how do i do it sql query ?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-11 : 07:16:10
create a check constraint on the table
create a udf
something like

CREATE FUNCTION RecCnt
(
@timein datetime,
@timeout datetime
)
RETURNS int
AS
BEGIN
DECLARE @Cnt int

SELECT @Cnt=COUNT(*)
FROM YourTable
WHERE DATEDIFF(minute,@timein,timein) <= 45
OR DATEDIFF(minute,@timeout,timeout) <= 45
RETURN(@Cnt)
END

then use it like

ALTER TABLE YourTable ADD CONSTRAINT Chk_TimeOverlap CHECK (dbo.RecCnt(timein,timeout) = 0 )


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -