Solved

MSSQL Dates

Posted on 2006-07-12
7
322 Views
Last Modified: 2008-03-10
I have a table with a column called DateStamp, containing the date the row was created in ASP Now() format. I want to run a SELECT query to return all rows that match todays date. The column has dates like this: '12/07/2006 23:31:11'. So itried something like this:

SELECT * FROM table WHERE DateStamp like '%12/07/2006%'

But it didn't work, what SQL should i use to get records from today. I also need SQL to get records from Yesterday, and one for the day before that etc.

Thanks Alex
0
Comment
Question by:alex_wareing
  • 3
7 Comments
 
LVL 25

Accepted Solution

by:
Mr_Peerapol earned 250 total points
ID: 17096584
SELECT * FROM your_table WHERE CONVERT(VARCHAR, DateStamp, 103) = '12/07/2006'
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 250 total points
ID: 17096589
is the datatype of the column DATETIME?
or character?

if  Datetime

then

SELECT * FROM table WHERE DateStamp between '20060712' and '20060712 23.59.59.997'

will return a single days data  (.997 since thats all the precision SQL Server Has...)


or

Declare @ED Datetime,@SD Datetime
set @SD = Convert(char(8),getdate(),112)
-- set @sd = dateadd(d,-1,@sd)    -- for yesterday
-- set @sd = dateadd(d,-2,@sd)    -- for 2 days ago...

set @ed = convert(char(8),@sd,112) + ' 23.59.59.997'



SELECT * FROM table WHERE DateStamp between @sd and @ed




  hth
0
 
LVL 25

Expert Comment

by:Mr_Peerapol
ID: 17096591
If you have an index on DateStamp (assumed its type is DATETIME) this will run faster:

SELECT * FROM your_table WHERE DateStamp >= CONVERT(DATETIME, '12/07/2006', 103) AND DateStamp < CONVERT(DATETIME, '13/07/2006', 103)
0
 
LVL 25

Expert Comment

by:Mr_Peerapol
ID: 17597855
Split points ...
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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 combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

786 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