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)
 Declare datetime stamp
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sz1
Constraint Violating Yak Guru

United Kingdom
443 Posts

Posted - 01/16/2013 :  06:19:44  Show Profile  Reply with Quote
Hi,

I want to be able to set a date and time and use it to calculate previous stats other than todays date and from 6am backwards each day.

DECLARE @MyDate AS DATETIME

SET @MyDate = '2011-02-15 06:00:00' ---always want this to be previous day not todays date time

Can I use something like the above to build into a DateAdd function like:
=DateAdd(day, -1, @MyDate)

or if I have a startdate field can I:

where StartDate = @MyDate -1


SZ1
Learning and development is the driving force in the universe...!

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 01/16/2013 :  06:58:59  Show Profile  Reply with Quote
-- To get yesterday's date with 6 hrs
SELECT DATEADD(hh, 6, DATEADD(dd, DATEDIFF(dd, 0, GETDATE()-1), 0))

--
Chandu
Go to Top of Page

sz1
Constraint Violating Yak Guru

United Kingdom
443 Posts

Posted - 01/16/2013 :  08:31:56  Show Profile  Reply with Quote
Chandu,

Will that give me all records prior to 6am this morning? thats what I'm after, do I need the table logged date in place of the GETDATE to remove all calls after 6am this morning?

Something like this?


Select Top 6 c.INTI_CATEGORY, count(c.ID) As TotalCat,
DATEADD(hh, 6, DATEADD(dd, DATEDIFF(dd, 0, c.OCCURED_DT -1), 0)) --occured_dt is the actual system logged date
From DIM_CALL c
Where TYPE = 'Incident'
And c.OPEN_FLAG = 1 and c.ETL_CURRENT =1
--And c.Occured_DT < DATEADD(hh,10,CAST(CAST(GETDATE() AS DATE) AS DATETIME))
Group by c.INTI_CATEGORY
ORDER BY TotalCat DESC;


SZ1
Learning and development is the driving force in the universe...!

Edited by - sz1 on 01/16/2013 08:33:41
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/16/2013 :  23:47:07  Show Profile  Reply with Quote
quote:
Originally posted by bandi

-- To get yesterday's date with 6 hrs
SELECT DATEADD(hh, 6, DATEADD(dd, DATEDIFF(dd, 0, GETDATE()-1), 0))

--
Chandu


can be further simplified as

SELECT DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0)-.75


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 01/17/2013 :  03:32:35  Show Profile  Reply with Quote
quote:
Originally posted by sz1

Chandu,
Will that give me all records prior to 6am this morning? thats what I'm after, do I need the table logged date in place of the GETDATE to remove all calls after 6am this morning?


WHERE c.Occured_DT < DATEADD(hh, 6, DATEADD(dd, DATEDIFF(dd, 0, GETDATE()-1), 0)) --> gives the yesterday's records




--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/17/2013 :  03:48:33  Show Profile  Reply with Quote
to get records prior to 6 am this morning it should be

WHERE c.Occured_DT < DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0)+.25

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sz1
Constraint Violating Yak Guru

United Kingdom
443 Posts

Posted - 01/17/2013 :  05:10:58  Show Profile  Reply with Quote
Thanks chaps I will have a look at these and get back.
Thanks again

SZ1
Learning and development is the driving force in the universe...!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/17/2013 :  05:12:19  Show Profile  Reply with Quote
quote:
Originally posted by sz1

Thanks chaps I will have a look at these and get back.
Thanks again

SZ1
Learning and development is the driving force in the universe...!


welcome

make sure you read this to understand logic behind sugestions

http://visakhm.blogspot.in/2012/07/generate-datetime-values-from-integers.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sz1
Constraint Violating Yak Guru

United Kingdom
443 Posts

Posted - 01/17/2013 :  05:19:57  Show Profile  Reply with Quote
OK will do.
Thanks

SZ1
Learning and development is the driving force in the universe...!
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.06 seconds. Powered By: Snitz Forums 2000