I have a database with a date/time stamp called dtstatusdatum. I would like to extract statistics from the database based on this column. What should the select clause look like. I tried:
select count(*) from mytable where dtstatusdatum like '2002-07-29%'
but get no results to the query:
(0) rows affected.
Thanks for mentioning "(0) rows affected" which tells me you're probably working with Microsoft SQL/Server. There are several ways to approach your query, but here's a simple one:
select count(*) from mytable where convert(dtstatusdatum,char(10),120) = '2002-07-29'
See the Microsoft documentation for CAST and CONVERT. Style 120 is the yyyy-mm-dd hh:mm:ss format, and converting to char(10) truncates the time off the end. Thus you can do a straight equality comparison rather than using LIKE.
For More Information
- Dozens more answers to tough SQL questions from Rudy Limeback.
- The Best SQL Web Links: tips, tutorials, scripts, and more.
- Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.