SQL Server to month/Zhou Chu

Source: Internet
Author: User
Tags datetime getdate

1. Show the first day of this month

SELECT DATEADD (Mm,datediff (Mm,0,getdate ()), 0)

Select CONVERT (Datetime,convert (varchar (8), GETDATE (),

120) + ' 01 ', 120)

2. Show the last day of this month

Select DATEADD (Day,-1,convert (Datetime,convert

(varchar (8), DATEADD (Month,1,getdate ()), 120) + ' 01 ', 120)

SELECT DateAdd (Ms,-3,dateadd (Mm,datediff (M,0,getdate ()) +1,0))

3. Last day of last month

SELECT DateAdd (Ms,-3,dateadd (Mm,datediff (Mm,0,getdate ()), 0)

4. This month's first Monday I

Select DATEADD (Wk,datediff (wk,0, DATEADD (Dd,6-datepart (

Day,getdate ()), GETDATE ()), 0)

5. The first day of the year

SELECT DATEADD (Yy,datediff (Yy,0,getdate ()), 0)

6. The last day of the year

SELECT DateAdd (Ms,-3,dateadd (Yy,datediff (Yy,0,getdate ()) +1,0))

7. Last day of last year

SELECT DateAdd (Ms,-3,dateadd (Yy,datediff (Yy,0,getdate ()), 0)

8. The first day of the quarter

SELECT DATEADD (Qq,datediff (Qq,0,getdate ()), 0)

9. Monday of this week

SELECT DATEADD (Wk,datediff (Wk,0,getdate ()), 0)

10. Check this month's records

SELECT * FROM tablename where DATEPART (mm, thedate)

=datepart (mm, GETDATE ()) and DATEPART (yy, thedate)

= DATEPART (yy, GETDATE ())

11. Check this week's records

SELECT * FROM tablename where DATEPART (wk, thedate) = DATEPART

(wk, GETDATE ()) and DATEPART (yy, thedate) = DATEPART (yy, GETDATE ())

12 Check this season Note: GETDATE () is a function to obtain the system time.

SELECT * FROM TableName where DATEPART (QQ, thedate) = DATEPART

(QQ, GETDATE ()) and DATEPART (yy, thedate) = DATEPART (yy, GETDATE ())

13. Get total days of the month:

Select DATEDIFF (Dd,getdate (), DATEADD

(mm, 1, GETDATE ()))

Select DateDiff (Day,

DATEADD (mm, DateDiff (mm, ', GETDATE ()),

DATEADD (mm, DateDiff (mm, ', GETDATE ()), ' 1900-02-01 ')

14. Get the current day of the week

Datename (Weekday, GETDATE ())

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.