Sqlserver date functions

Source: Internet
Author: User

1. Where year (A) = year (B) and month (A) = month (B) and day (A) = 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

 

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

 

3. Remove the time, minute, and second information returned by the getdate () function.

Dateadd (day, datediff (day, 0, getdate (), 0)

 

4. display the time returned by the getdate () function as the day of the week

Select datename (weekday, getdate ())
The result is related to the value of @ datefirst.

 

5. 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 results:

102 results: 2005.10.25

111 results:

101 results:

112 results: 20051027

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.