How does SQL Server compare the date size?

Source: Internet
Author: User

In the data room charging system, there are several such situations:

Start date and end date. I believe you are smart enough to figure out why there are two date controls! Yes, it is to find records in these two date ranges from a table!

Some people just said, it's not easy!

Suppose we define the first control as Begin, and the second control as Over! The query statement is not
Copy codeThe Code is as follows:
"Select * from recharge_info where date <'" & over. value & "' and date> '" & begin. value &"'"

What's the problem! However, the fact is not that simple. If the query statement is written like this, you cannot find it, so how can we compare the sizes of two dates in SQL Server?
Here we will borrow the CONVERT () function.
The CONVERT () function is a general function that converts a date to a new data type.
The CONVERT () function can display date/time data in different formats.
Syntax: CONVERT (data_type (length), data_to_be_converted, style)
Specifically, data_type (length) specifies the target data type (with an optional length) data_to_be_converted contains the value to be converted. style specifies the output format of the date and time.
The available style values are as follows:

For example:

Copy codeThe Code is as follows:
Print convert (varchar (19), getdate ())
Print convert (varchar (10), getdate (), 110)
Print convert (varchar (11), getdate (), 106)
Print convert (varchar (24), getdate (), 113)


The result is as follows:

If you use a query statement like date = '2017-09-22 'in SQL Server, but when you can still find the expected results, remember: This is not the date you want to record in the table that is equal to the current date, instead, the date string in the table is equal to the string '2017-09-22!

Related Article

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.