Precautions for using between and SQL Server

Source: Internet
Author: User

Today we also encountered the following problems. You should pay attention to using between in the future. It is best not to use it!

1. Problem:

Five pieces of data are as follows:
12:16:10. 000
2010-12-02 13:11:28. 000
2010-3 3 13:49:15. 000
2010-4 4 13:52:28. 000
13:52:28. 000

I used select * from table where [time] Between '2017-12-1 'and '2017-12-6' to display these five items.

If I use select * From sk_smssee where [time] Between '2017-12-1 'and '2017-12-5'
Why are four entries displayed?
12:16:10. 000
2010-12-02 13:11:28. 000
2010-3 3 13:49:15. 000
2010-4 4 13:52:28. 000

In other words, the last date is always not displayed. Why?

 

After the test code is changed to select * from table where [time]> = '2017-12-1 12:00:00 'and [time] <= '2017-12-5 12:00:00', four results are also displayed.. Since the database contains data for the period from 2010, why can't I find the data for [time] <= '2017-12-5?

2. The answer is as follows:

Using bewteen... In the ms SQL Server System and its scope is closed, that is, containing left and right boundary values.

In addition, in ms SQL System, when implicit conversion of date --> datetime is performed, the system automatically adds the time field 00:00:00.

That is, when the value is implicitly converted from the date type "" to the date type "00:00:00"

So use between for the date type... and... that is, only the partial score of the other edge value is actually included. You can use the semi-open and semi-closed intervals (>=& & <) to implement the problem you mentioned.

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.