Query for dates using the CONVERT function in SQL

Source: Internet
Author: User
Tags format datetime expression sql query
Function
Once encountered such a situation, in the Database meeting table has publishtime (datetime,8) field, used to store a session, in the deposit due to specify the time of the meeting, the format is Yyyy-mm-dd Hh:mm:ss, And we query it through YYYY-MM-DD, that is, to query all meeting information for a particular day, so that if the select * from meeting where Publishtime= @PublishTime ( Parameter @publishtime is YYYY-MM-DD format) statement query will not get the correct results, such as we want to query the December 1, 2004 meeting information, in the input of 2004-12-01 can not get the results of the query, and this query does not require the input of specific HH: Mm:ss.

At this point we need to use the CONVERT function, which converts an expression of one data type to an expression of another data type. Here we first convert the datetime type in the database to the char type, and since the input parameter is 10 bits, write

SELECT * from meeting where CONVERT (varchar (), publishtime,121) = @PublishTime, This will automatically convert the information in the database to the 10-bit character in the YYYY-MM-DD format after the database gets the parameters in the query. Returns the query results as long as the parameters are the same. and 121 of Convert is the 4-bit year that includes the century digits when converting a datetime type to a char type
These are the small difficulties I encountered in the development of the summary, if the written there is not the right place to welcome everyone to advise!


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.