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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 selecting dates in sql
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

silentbob
Starting Member

18 Posts

Posted - 11/23/2012 :  04:53:15  Show Profile  Reply with Quote
Hi

I have the following sql statement that selects data from yesterday between 7 and and 6 (current day)

I am trying to use it for a report in reporting services and I would like to be able to select the date. So for example they would select 1/11/2012 amd 2/11/2012 (thats in UK format) and get back data between 6am on the 1st and 7am on the 2nd.

SELECT readDate as Interval, tagname ,round(value,3) as value, amendedValue
FROM [GasNominations].[dbo].[GasData]
WHERE readDate between 
             dateadd(hour, 7, DATEADD(day, DATEDIFF(day, 0, GETDATE()), -1))   
             and dateadd(hour, 6, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))
order by tagname, readdate

madhivanan
Premature Yak Congratulator

India
22754 Posts

Posted - 11/29/2012 :  06:24:23  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Try this

declare @from_date datetime, @to_date datetime

select @from_date ='20121101', @to_date='20121102'
SELECT readDate as Interval, tagname ,round(value,3) as value, amendedValue
FROM [GasNominations].[dbo].[GasData]
WHERE readDate between 
             dateadd(hour, 7, DATEADD(day, DATEDIFF(day, 0, @from_date , -1))   
             and dateadd(hour, 6, DATEADD(day, DATEDIFF(day, 0, @to_date ), 0))
order by tagname, readdate


Madhivanan

Failing to plan is Planning to fail
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