SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Time Interval
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dia1234
Starting Member

12 Posts

Posted - 07/11/2013 :  06:25:38  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 07/11/2013 :  07:16:10  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000