Date Reduction Method in MSSQL

Source: Internet
Author: User

 

Code

1 declare @ DT datetime;
2 set @ dt = getdate ();
3 declare @ Table (Caption varchar (20), value datetime );
4
5 insert into @ table values ('0', @ DT );
6 insert into @ table values ('-1', @ dt-1 );
7 insert into @ table values ('-0.1', @ dt-0.1 );
8 insert into @ table values ('-0.01', @ dt-0.01 );
9 insert into @ table values ('-0.001', @ dt-0.001 );
10 insert into @ table values ('-0.0001', @ dt-0.0001 );
11 insert into @ table values ('-0.00001', @ dt-0.00001 );
12 insert into @ table values ('-0.000001', @ dt-0.000001 );
13 insert into @ table values ('-0.0000001', @ dt-0.0000001 );
14
15 select * From @ table
16

 

 

Display result:

 

Caption value
0 2010-05-05 18:50:03. 547
-1 18:50:03. 547
-0.1 16:26:03. 547
-0.01 18:35:39. 547
-0.001 18:48:37. 147
-0.0001 18:49:54. 907
-0.00001 18:50:02. 683
-0.000001 18:50:03. 463
-0.0000001 18:50:03. 540

 

Then, how is it calculated. People who are a little focused can immediately see that when-1, the date is just less than one day, so we can understand it as follows:

Date-1 = minus 1 day.

Next, you only need to switch to understand.

Date-0.1 = today's date minus 0.1Days.

It is 0.1 days. What is 0.1 days? Suddenly realized that it was added or subtracted by minute.

1 day equals to 24 hours multiplied by 60 minutes

0.1 equals to 24 hours multiplied by 60 minutes, and then multiplied by 0.1.

This is exactly the case!

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.