Solved

MSSQL Dates

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

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

910 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

21 Experts available now in Live!

Get 1:1 Help Now