Solved

Automatic Date/Time in a SQL Server Query

Posted on 2004-10-01
6
240 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting Teradata SQL to Oracle SQL (exadata) 3 28
Using Aggregate Functions to Count 3 32
SQL Server - Slabs 9 36
Sql Query 4 0
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now