Solved

MSSQL Dates

Posted on 2006-07-12
7
302 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now