Getting previous Days Data

rkrause

Registered User.
Local time
Today, 03:59
Joined
Sep 7, 2007
Messages
343
I want to be able to have a query automattically get the previous days data. i dont want to have the user enter any dates.
heres my query:
SELECT*--,MIN(formatdate)
FROM bk_vAPTData
Where formatdate >=dateadd("D",-1,getdate())

this gives me data for both 9-19-12 and 9-20-12
how can i just get my records from 9-19-12
 
if i do this: i get no results

SELECT *--,MIN(formatdate)
FROM bk_vAPTData
Where formatdate = dateadd("D", -1, getdate())
 
if i do this: i get no results

SELECT*--,MIN(formatdate)
FROM bk_vAPTData
Where formatdate =dateadd("D",-1,getdate())

Does your date field have a time element included?

Also, what is this getdate procedure?
 
No time included
im not sure what getdate() is, im pretty sure its a built on function for todays date?
 
To get today's date you use Date() function..
 
im not sure what getdate() is, im pretty sure its a built on function for todays date?
Go find out. You can use todays date by using Date(), so you may not need that function. But go to the VBA window, click in the code somewhere and then click CTRL+F to bring up the find dialog and put in getdate and select ENTIRE PROJECT from the selections available on the dialog and then click find. It should take you to it but if you have code which uses it, it may go to that instance(s) first. So, if it does, just right click on that instance of its name and select DEFINITION and it should take you right to it.
 
Also, what is this getdate procedure?

You must have had a brain cramp. GETDATE() is SQL Server's equivalent to Now(). This thread is in the SQL Server forum, but the syntax seems mixed, so perhaps it needs to be clarified where this is running?
 
Im using SSMS for my queries, im not familiar with VBA in SSMS?
if i use DATE() i get a error saying its not a recognized built-in function.
 
Sorry, I missed that you were in the SQL Server category.

So, getdate there returns a date in the yyyy-mm-dd format and you delimit it with quotes. Also, SQL Server date/time fields always include time (if you don't use them they just store as 00:00:00.000. But I think you can use:

PHP:
SELECT*--,MIN(formatdate)
FROM bk_vAPTData
Where formatdate >dateadd("D",-1,getdate()) AND <getdate()
 
You must have had a brain cramp. GETDATE() is SQL Server's equivalent to Now(). This thread is in the SQL Server forum, but the syntax seems mixed, so perhaps it needs to be clarified where this is running?
Yep, I got here using the New Posts link and sometimes I miss which category those are posted in.
 
i get this error:
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '<'.
 
Sorry, my bad (not thinking too well this morning):

PHP:
SELECT*--,MIN(formatdate)
FROM bk_vAPTData
Where formatdate >dateadd("D",-1,getdate()) AND formatdate <getdate()
 
Code:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]SELECT[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]*[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]FROM[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] bk_vAPTData[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Where[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] formatdate [/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]BETWEEN [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#ff00ff][SIZE=2][COLOR=#ff00ff]convert[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]([/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]varchar[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]([/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]10[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]),[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#ff00ff][SIZE=2][COLOR=#ff00ff]dateadd[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]([/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]D[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080],-[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]1[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080],[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#ff00ff][SIZE=2][COLOR=#ff00ff]getdate[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]()),[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]101[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]) [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]AND [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#ff00ff][SIZE=2][COLOR=#ff00ff]convert[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]([/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]varchar[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]([/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]10[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]),[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#ff00ff][SIZE=2][COLOR=#ff00ff]getdate[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080](),[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]101[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080])[/COLOR][/SIZE]
[/COLOR][/SIZE]
 
Just for completeness

Another way to combat the time issue, is to use a date column rather than a date/time column (sql 2008 and above)
 
Neither of the last 2 posts got me what i need.
Boblarson i still only get 9-20-12 data and not yesterdays 9-19-12

and SQL_hell i dont get any results from you.
 
i was able to find this and it seems to work. not sure whats different than ive already tried.

SELECT *--,MIN(formatdate)
FROM bk_vAPTData
WHERE formatdate>=DATEADD(d,DATEDIFF(d,0,GETDATE())-1,0) AND
formatdate<DATEADD(d,DATEDIFF(d,0,GETDATE())+0,0)
 
that makes no sense, mine should have worked fine. Your datediff function does nothing. Maybe fomatdate isn't a true date/time value
 
Im puzzled as well...Im not sure, it should be date/time field.
 
I am sure we can get to the bottom of this, please post the results of the following queries:

select convert(varchar(10),dateadd(D,-1,getdate()),101)

select convert(varchar(10),getdate(),101)

select top 3 formatdate
from bk_vAPTData

Are you in USA?
 

Users who are viewing this thread

Back
Top Bottom