SQL time-type fuzzy query

Source: Internet
Author: User
SQL statements have been learned before in school. After a long time, I cannot remember them.

SQL statements have been learned before in school. After a long time, I cannot remember them.

Today, we use the time Like '2017-06-2008 'statement to query all the data of the day. A statement error is prompted. After checking, we found that the fuzzy query can only be used for fields of the String type.
I also consulted some materials. There are three methods for fuzzy query of time:
1. Convert to String and use Like to query.
Select * from table1 where convert (varchar, date, 120) like '2017-04-2006'
2.
Select * from table1 where time between '2017-4-1 0:00:00 'and '2017-4-1 24:59:59 '";
3 datediff () function
Select * from table1 where datediff (day, time, '2017-4-1 ') = 0
The first method should be applicable to any data type;
The second method is applicable to types other than String;
The third method is a more practical and quick method customized for the date type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.