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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Sql Query
 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  
 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.03 seconds. Powered By: Snitz Forums 2000