Some experiences on using the date type in SQL Server

Source: Internet
Author: User

I,Previously, I used to compare two datetime data types, A and B, to determine whether the same year and the same month are the same method:

Where   Year () =   Year (B) And   Month () =   Month (B) And   Day () =   Day (B)

Actually, it can be implemented in a more concise way.

Where   Datediff ( Day , A, B) =   0

Similarly, the method to compare whether A and B are in the same month of the same year is:

Where   Datediff ( Month , A, B) =   0

For more information about datediff, see books online.

II,In the date function of the ms SQL Server, "0" can be used to replace the date, "0" indicates the date is.

III,Removes the time, minute, and second information returned by the getdate () function.

Dateadd ( Day , Datediff ( Day , 0 , Getdate ()), 0 )

IV,Display the time returned by the getdate () function

Select   Datename (Weekday, Getdate ())

The result is related to the value of @ datefirst.

V,The default datetime format of the Chinese version of SQL Server is yyyy-mm-dd thh: mm: Ss. Mmm. If we need different date formats such as: 2005/10/27, 2005-10-27,
You can use the following function:

Select   Convert ( Varchar ( 10 ), Time, 120 )

here, 120 can be replaced by a value between 101 and 114 (108 obtains the time ). Of course, you must change varchar (10) To varchar (12) in several cases to display it completely.
below are several common values:
120 result: 2005-10-27
102 result: 2005.10.25
111 result: 2005/10/27
101 result: 10/27/2005
112 results: 20051027

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.