[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How do you access sysdate and current time in stored procedure  SQL server

Posted on 2009-04-10
12
Medium Priority
?
1,725 Views
Last Modified: 2012-06-21
I have stored procedure which runs wvery hour. However every hour I need to dynam,ixally change date/time,. I need two cases
a. Run query for previous day to concompass the whole day
select * from Table where dateTime>=(sysdate-1):0000 and dateTime=(sysdate-1):2359

b. I need a query which looks for anything for the past one hour for today

select * from Table where dateTime>=sysdate-1:currentime and dateTime<=sysdate:currentime -1  (past one hour)




0
Comment
Question by:rheasam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 24119077
a.
select * from tablename
where datefield >= cast(convert(varchar(10), dateadd(d, -1, getdate(), 101)  as datetime)
and datefield < cast(convert(varchar(10), getdate(), 101) as datetime)

b.
select * from tablename
where datefield >= cast(convert(varchar(10), dateadd(h, -1, getdate(), 101)  as datetime)

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24119083
in sql server the date+time (aka now) is available from GETDATE()


select * from Table 
  where dateTime>=dateadd(day, -1, getdate())
    and dateTime<=dateadd(hour,-1, getdate())

Open in new window

0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 24119740
If you happen to be using SQL Server 2008, you can make use of the new Date datatype to accept the dateadd(day, -1, getdate()) value from angelIII's solution for the "yesterday" portion of the problem and then match based upon your dates being between that value and that value + 1.
If you are not on SS2008, you can make use of the fact that the dates are integers and the times are fractions in a double datatype.  (See SQL_1 below)
If you want the process to check for exactly and precisely datetimes during the hour immediately preceding the execution, angelIII's solution is your answer for the second half of the problem.

SQL_1:
 
DECLARE @Yesterday AS DateTime;
DECLARE @ToDay AS INT;
DECLARE @INTTIME AS INT;
DECLARE @LastHour AS Datetime;
DECLARE @ThisHour AS Datetime;
 
 
 
SET @Yesterday = CAST(CAST(GETDATE() AS INT) AS DATETIME);
SET @ToDay = CAST(Cast(GETDATE() AS INT) AS DATETIME
 
SELECT @ThisHour = 
       CAST(CONVERT(VARCHAR(02), JustTime, 108) AS INT)FROM
(SELECT GETDATE() - CAST(INTDATE AS DATETIME) AS JustTime,
        INTDATE
FROM
(SELECT CAST(GetDate() AS INT) AS INTDATE
) X
) Y;
 
SET @LastHour = @ThisHour - 1;
 
 
SELECT * 
FROM Table 
WHERE CAST(DateTimeColmun as INT) BETWEEN @YESTERDAY AND (@YESTERDAY + 1);
 
SELECT *
FROM Table
WHERE DateTimeColmun >= (@Today + @LastHour) 
  AND DateTimeColumn <  (@Today + @ThisHour)
 

Open in new window

0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Author Comment

by:rheasam
ID: 24127529
k, thanks guys, I am checking into the solution and will award points in next couple of days.
0
 

Author Comment

by:rheasam
ID: 24129958
angellll
select * from Table
  where dateTime>=dateadd(day, -1, getdate())
    and dateTime<=dateadd(hour,-1, getdate())

does not giove me all records from last one day ie
I need records from
select * from Table
and dateTime > '04/12/09 00:00'
and dateTime < '04/13/09 09:00' (assuming 09:00 is the current time)

How can I add the 00:00 to the above query

0
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 300 total points
ID: 24130018
did you try my first post?

select * from tablename
where datefield >= cast(convert(varchar(10), dateadd(d, -1, getdate(), 101)  as datetime)
and datefield < cast(convert(varchar(10), getdate(), 101) as datetime)

0
 

Author Comment

by:rheasam
ID: 24131106
chap, that did not work. It gave me following error:
Server: Msg 174, Level 15, State 1, Line 21
The dateadd function requires 3 arguments.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24131183
there is some () misplaced..

select * from tablename
where datefield >= convert(datetime, convert(varchar(10), dateadd(d, -1, getdate()), 120), 120)
and datefield < getdate() 

Open in new window

0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24131197
WHOOPS:

select * from tablename
where datefield >= cast(convert(varchar(10), dateadd(d, -1, getdate()), 101)  as datetime)
and datefield < cast(convert(varchar(10), getdate(), 101) as datetime)
0
 

Author Comment

by:rheasam
ID: 24132024
amgelll your qyery seems to be working ok, how can modify this for just looking back for past one hour.

Chap, your query is returning only rows for past one day, not for today
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24132146
>>Chap, your query is returning only rows for past one day, not for today
Sorry, i  thought that is what you wanted.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1200 total points
ID: 24133284
back 1 hour:
select * from tablename
where datefield >= dateadd(hour, -1, getdate()) 
and datefield < getdate() 

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question