Description of datetime and smalldatetime types in SQLSERVER

Source: Internet
Author: User
Description of datetime and smalldatetime types in SQLSERVER. For more information, see what statements are used.

Description of datetime and smalldatetime types in SQL SERVER. For more information, see when to use the statements.

Datetime and smalldatetime
Represents the date and time data types of the date and the time within the day.
Microsoft SQL Server uses two 4-byte Integers to store values of the datetime data type. The number of days before or after the first 4-byte storage base date (January 1, 1900. The base date is the system reference date. Datetime values earlier than January 1, January 1, 1753 are not allowed. The first 4 Bytes: the value of the date on January 1, January 1, 1900 is 0, the date before is negative, and the date after is positive. The other 4-byte storage is represented by the daily time 3 1/3 milliseconds after midnight.
The smalldatetime data type stores the date and the daily time, but the accuracy is lower than datetime. SQL Server stores the smalldatetime value as two 2-byte integers. The first 2-byte is stored in the number of days after January 1, January 1, 1900. The other two bytes store the minute after midnight. The date range is from January 1, January 1-20, 1900 to January 1, June 6, accurate to minutes.
The two types can be converted to float floating point numbers. The integer part is the corresponding date byte, And the decimal part is the corresponding time ratio, for example, the decimal part of datetime is the percentage of milliseconds in the whole day; the decimal part of smalldatetime is the percentage of minutes in the whole day. therefore, we can directly add or subtract these two types of variables, integers, and floating-point numbers.
Datetime
From the date and time data of January 1, 3% to 3.33 seconds (equivalent to 0.00333 milliseconds or seconds ). As shown in the following table, adjust the value to an increment of. 000,. 003, or. 007 seconds.

We often need to search for data within the specified date range. For example, if we return data within, you may write as follows:
Date> = '2014-01-01 00:00:00. 000' and date <= '2014-01-01 23:59:59. 100'
According to the above adjustment rules, the actual search range of this statement is:
Date> = '2014-01-01 00:00:00. 000' and date <= '2014-01-02 00:00:00. 000'
You will see that this includes data, so the best and correct search statement is:
Date> = '2014-01-01 00:00:00. 000' and date <'2014-01-02 00:00:00. 000'
Smalldatetime
The date and time data from January 1, January 1-20, 1900 to January 1, June 6 are accurate to minutes. The smalldatetime value of 29.998 s or lower is rounded down to the nearest minute, And the smalldatetime value of 29.999 s or higher is rounded up to the nearest minute.
-- Returns time as 12: 35
Select cast ('2017-05-08 12:35:29. 2000 'AS smalldatetime)
GO
-- Returns time as 12: 36
Select cast ('2017-05-08 12:35:29. 2000 'AS smalldatetime)
GO
Assignment:
The actual time format is described above. When we assign a value to a time variable, we certainly won't assign a floating point number to the variable. More often, we assign a string to the variable, the system automatically converts the string to the time format and saves it to the database. If the character format is incorrect, an error is returned. According to different languages, there are many different string representation methods in the world. We can use sp_helplanguage to view the default time formats in different languages. For example, the time format of Simplified Chinese is ymd, you can use set dateformat to temporarily change this default value.
Time Functions
DATEADD: You can add or subtract the specified part of the time type. Although we mentioned above, you can directly add or subtract the specified part, but we can more easily use this function to specify the part, such as year, month, day, hour, minute, and second. We often construct another time based on one time, such as today and the end of this month. We should also try to use the DATEADD function to construct it, it can avoid errors such as the leap month, the end of the year, and the end of the month. I used to construct them based on the DATENAME, and I often need to consider these issues.
DATEDIFF: This function compares two time variables to the specified part. This function does not consider the level of granularity higher than the specified date, but only the lower level. Use this function whenever possible for time comparison.
For example, if an error occurs, the time difference between the two time variables is returned. If you use DATEPART (HOUR, @ T2-@ T1), errors may occur. You should use DATEDIFF (HOUR, @ T1, @ T2 ).
DATEPART: return the value of the specified part of the time variable.
DATENAME: return the value of the specified part of the time variable. Different from DATAPART, this function returns a string type.
GETDATE () returns the current time of the machine. The CURRENT_TIMESTAMP variable has the same function as this function.
GETUTCDATE () returns the current UTC (Greenwich Mean Time) Time of the machine.

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.