Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MSSQL Dates

Posted on 2006-07-12
7
Medium Priority
?
348 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
4 Comments
 
LVL 25

Accepted Solution

by:
Mr_Peerapol earned 1000 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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

783 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