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

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Sql Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

1 Posts

Posted - 11/28/2012 :  07:31:16  Show Profile  Reply with Quote
Hi guys,
I need a function that will return No. of Days and No. of Nights in a given period of Date and Time.
e.g. 28/11/2012 01:00 and 30/11/2012 12:00
Consider below
From 00:00 to 06:00 should be considered as DAY and From 06:01 to 23:59 should be considered as NIGHT...
Plz reply...

Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/28/2012 :  07:35:52  Show Profile  Reply with Quote
Something like this:
SELECT DATEDIFF(dd,@startDate, @endDate)
	+ CASE WHEN CAST(@startDate AS TIME ) > '06:00' THEN -1 ELSE 0 END AS [Days],
	DATEDIFF(dd,@startDate, @endDate) 
	+ CASE WHEN CAST(@endDate AS TIME ) <= '06:00' THEN -1 ELSE 0 END AS [Nights]
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.05 seconds. Powered By: Snitz Forums 2000