473,503 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need to find 'yesterday'

143 New Member
I'm writing a stored procedure that will gather a few attributes and put those on a report of events that happened yesterday. I'm having trouble with the date. How do I put this in a WHERE clause to select the events that occured yesterday.

Here's what I have so far:

SELECT d.ToName, d.CreationTime, df.NumPages
FROM documents d INNER JOIN docfiles df ON
d.docfileDBA = df handle
WHERE d.CreationTime >= (yesterday's date)

Thanks
Jul 6 '07 #1
4 25902
camscott
3 New Member
I'm writing a stored procedure that will gather a few attributes and put those on a report of events that happened yesterday. I'm having trouble with the date. How do I put this in a WHERE clause to select the events that occured yesterday.

Here's what I have so far:

SELECT d.ToName, d.CreationTime, df.NumPages
FROM documents d INNER JOIN docfiles df ON
d.docfileDBA = df handle
WHERE d.CreationTime >= (yesterday's date)

Thanks


WHERE d.CreationTime >= (GETDATE() - 1)

This, of course, assumes that you ALWAYS want yesterday's date...
Jul 6 '07 #2
teddarr
143 New Member
Yes, I always want yesterdays date

BUT

It can't really be that easy can it?
Jul 6 '07 #3
camscott
3 New Member
Yes, I always want yesterdays date

BUT

It can't really be that easy can it?
Yep...it really is that easy...try it...

Here is my result from the query SELECT GETDATE()-1:

2007-07-05 07:35:24.480
Jul 6 '07 #4
Infide
28 New Member
Yep...it really is that easy...try it...

Here is my result from the query SELECT GETDATE()-1:

2007-07-05 07:35:24.480
Most likely though he will want the entire date range for yesterday. GETDATE() - 1 will exclude all times from the current time to midnight.

For example, a result that had a date of 7-5-2007 7:35:23 would not be returned in your query.

Another solution would be either.
Select *
from table
where DATEPART(dd,creationdate) = datepart(dd,dateadd(dd,-1,getdate())
and datepart(mm,creationdate) = datepart(mm,dateadd(dd,-1,getdate())
and datepart(yyyy,creationdate) = datepart(yyyy,dateadd(dd,-1,getdate())

Not very pretty but it will give you all creation dates for yesterday, not just the creation dates after the random time generated by getdate().

You could also create a scalar function that returns the date to just after midnight.

For example:
Expand|Select|Wrap|Line Numbers
  1. CREATE  FUNCTION dbo.fn_DateToJustAfterMidnight(@Date DATETIME)
  2. returns datetime
  3. as
  4. begin
  5.         declare @returnDate datetime, 
  6.                 @year int, 
  7.                 @month int, 
  8.                 @day int
  9.  
  10.     set @year = DATEPART(yyyy, @Date)
  11.         set @month = DATEPART(mm, @Date)
  12.         set @day = DATEPART(dd, @Date)
  13.  
  14.     if (@Date is not null) begin
  15.        set @returnDate = cast(convert(varchar(4), @year) + '-' + convert(varchar(2), @month) + '-' + convert(varchar(2), @day) AS DATETIME)
  16.         end
  17.  
  18.     return @returnDate
  19. end
  20.  
then you would just write:

Select *
From Table
Where CreationDate <= dbo.fn_DateToJustAfterMidnight(dateadd(dd,-1,getdate()))
Jul 11 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
6335
by: MegaZone | last post by:
I'm having some issues with PHP DOMXML - in particular the get_elements_by_tagname method. Now, the PGP docs on this are, well, sparse, so maybe I'm just doing something stupid. I thought this...
7
1424
by: angelasg | last post by:
Here is sample data I'm working with: ID ShiftDate SegTime 99 5/2/2005 5/2/2005 1:00:00 PM 99 5/2/2005 5/2/2005 1:04:00 PM 99 5/2/2005 5/2/2005 1:43:00 PM 99 5/2/2005...
2
2084
by: amywolfie | last post by:
I would like to place a Find button on a form which uses the built-in Access Find facility. If no records are found, I would like to display a custom "no records found - plesae try again" message...
23
13997
by: thebjorn | last post by:
For the purpose of finding someone's age I was looking for a way to find how the difference in years between two dates, so I could do something like: age = (date.today() - born).year but that...
0
744
by: twicave | last post by:
In my case, I need find some web site supported sending MMS. It seemed that I can't use Google or Yahoo to search it. I mean, If I know a website, can I enumerate all web service belong to it?
5
2284
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or...
3
1216
by: wang frank | last post by:
Hi, I am a new user on Python and I really love it. I have a big text file with each line like: label 3 teststart 5 endtest 100 newrun 2345
11
2061
by: Helmut Jarausch | last post by:
Sorry, but I'm a complete newbee! I need find out under which userid the browser is running. Is this possible with JavaScript? Many thanks for a hint, Helmut Jarausch Lehrstuhl fuer...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.