Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

Select all today from Datetime column

How do I select all from a table where the value in a Datetime column ("DateCreated") is today.
0
johnkainn
Asked:
johnkainn
  • 4
  • 2
1 Solution
 
Aneesh RetnakaranDatabase AdministratorCommented:
select *
from tablename
where convert(varchar,datetimecolumn,112) = convert(varchar, getdate(),112 )
0
 
waltersnowslinarnoldCommented:
Try the following sample..

SELECT  CASE DateCreated WHEN DATEPART(dd,DateCreated)=DATEPART(dd,GETDATE()) AS CreatedDate FROM tablename
0
 
igni7eCommented:
select *
from tablename
where dateadd(day,0,DateCreated) = dateadd(day,0,GETDATE())
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
igni7eCommented:
The wrong answer was accepted again.
My answer is more efficient.
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
>My answer is more efficient.
you know what the most efficient code is provided there is an index on the datecreated column , also your answer is not correct

select *
from yourtable
where datecreated >= convert(varchar, getdate(), 112 ) and datecreated <  convert(varchar, getdate()+1, 112 )
0
 
igni7eCommented:
Yes, your answer was not correct, but mine is.
0
 
igni7eCommented:
@ Author:

You accpeted an incorrect answer.

You can use his fixed code which is:
select *
from yourtable
where
datecreated >= convert(varchar, getdate(), 112 )
and
datecreated <  convert(varchar, getdate()+1, 112 )

Or you can use my more efficent code that was working when you accepted an incorrect answer:
select *
from tablename
where dateadd(day,0,DateCreated) = dateadd(day,0,GETDATE())

My method doesn't take time into account. His does, that's why he needs to check datecreated against today and today+1, he should actually use BETWEEN. But whatever!

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now