SQL Server method for comparing date sizes _mssql

Source: Internet
Author: User
Tags getdate

In the computer room toll system, there are several cases where:

Start date and end date, believe that smart you can certainly imagine why there are two date controls! Yes, it is from a table to find out in these two date range class records!

Some people say, it's not easy!

If we define the first control as begin, the second control is defined as a over! So the query statement is not

Copy Code code as follows:

"SELECT * from Recharge_info where date< '" & Over.value & "' and Date> '" & Begin.value & ""

What's so hard about this! However, the fact is not so simple, if the query is written like this, you are sure not to find out, so how can we in the SQL Server to compare the size of two date?
Here we're going to borrow the convert () function.
The CONVERT () function is a common function that converts dates to new data types.
The CONVERT () function can display date/time data in a different format.
Syntax: CONVERT (data_type (length), Data_to_be_converted,style)
where data_type (length) sets the target data type (with optional length) data_to_be_converted the output format that contains the value style that needs to be converted, the date time
Where you can use the style value:

For example:

Copy Code code as follows:

Print convert (varchar (), GETDATE ())
Print convert (varchar), GETDATE (), 110)
Print CONVERT (varchar (one), GETDATE (), 106)
Print convert (varchar), GETDATE (), 113)


The results are as follows:

If you use a date lookup record in SQL Server, if you use a date= ' 2013-09-22 ' query, but you can still find out what you want, remember: it's not the date that you want to record in the table. But the date in the table this string is equal to the ' 2013-09-22 ' string!

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.