Solved

MSSQL Dates

Posted on 2006-07-12
7
336 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

734 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