Solved

Automatic Date/Time in a SQL Server Query

Posted on 2004-10-01
6
241 Views
Last Modified: 2012-06-21
I have a query that selects data from one table and inserts it into another one. Part of the query looks for data in a date range. (Between '9/29/2004 0:1:00' And '9/29/2004 23:59:59')
How can I programatically via VB or Stored Procedure generate the time format for a given time period , say the previous day, and insert it into the query?  I will run this query at 1 AM on the next day. For example want to pull data from the 29th so I run it on the 30th.

Any help is appreciated.
0
Comment
Question by:infutech
6 Comments
 
LVL 15

Expert Comment

by:justinbillig
ID: 12201557
dateadd( "d", Now, -1 )

that will add (-1) day to today
0
 
LVL 15

Expert Comment

by:justinbillig
ID: 12201558
dateadd( "d", Now, -1 )


should be


dateadd( "d", GetDate( ), -1 )
0
 
LVL 9

Accepted Solution

by:
paelo earned 500 total points
ID: 12201596
If this is within a stored procedure, you could use:

BETWEEN CONVERT(datetime,CONVERT(varchar,DATEADD(dd,-1,GETDATE()),101)+' 00:01:00')
 AND CONVERT(datetime,CONVERT(varchar,DATEADD(dd,-1,GETDATE()),101)+' 23:59:59')


This will give you the items between 00:01 (first minute) and 11:59:59 (last minute) for the previous day.

-Paul.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 10

Expert Comment

by:imrancs
ID: 12201598
it should be

 dateadd(day,-1,getdate())


0
 

Author Comment

by:infutech
ID: 12201646
Worked like a charm. Thanks to all for responding.
0
 
LVL 15

Expert Comment

by:justinbillig
ID: 12201651
Here it is in VB


Private Function GetDateClause()
    ' Variables
    Dim dtmNow
    Dim strStartDate
    Dim strEndDate
    Dim strBetweenClause
   
    ' Get Yesterday
    dtmNow = FormatDateTime(DateAdd("d", -1, Now()), vbShortDate)
   
    ' Set start time clause
    strStartDate = CStr(dtmNow) & " 0:1:00 AM "
   
 
    ' Set our end time clause
    strEndDate = CStr(dtmNow) & " 23:59:59"
   
    ' BUild the between clause
    strBetweenClause = " BETWEEN " & strStartDate & " AND " & strEndDate


    ' Return Value
    GetDateClause = strBetweenClause

End Function
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

810 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