Some common uses of dates in MS SQL Server-Continuous updates

Source: Internet
Author: User
Tags getdate

Note: The following table in 0 is actually the date 1900-1-1

1.0 Points of the day

SELECT DATEADD (Day,DATEDIFF(Day,0,GETDATE()),0)

The above DateDiff (Day,0,getdate ()) means to calculate the number of days of the day and 1900-1-1, let's remember that this is N days, and then through the DateAdd calculated 1900-1-1 plus n days to reach the date of the day, time is 0.

2. The first day of the month

SELECT DATEADD (month,DATEDIFF(month,0,GETDATE()),0)

The principle is the same, but the calculation unit is changed from day to month.

3. The last day of the month

SELECT DATEADD (month,DATEDIFF(month,0,GETDATE())+1,0 )-1

The principle is to first calculate the first day of the next month, and then subtract the day.

4. Today is the day of the week

SELECT Datename (WEEKDAY,GETDATE())  -- input: Friday 

5. Today is the first day of the week

SELECT DATEPART (DW,GETDATE())  -- The beginning of the week is Sunday, so 1 means Sunday, 7 means Saturday 

But you can change this by the following language, such as the following modification the first day of the week for Monday:

SET 1

You can also view the values of the previous datefirst by saying the following:

SELECT @ @DATEFIRST

Some common uses of dates in MS SQL Server-Continuous updates

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.