MSSQL retrieval yesterday, this week, and this month

Source: Internet
Author: User
Note: Sunday is the first day of the week, not the last day of the week () minus one day, such as dateadd (day,-1, getDate () This week: select * fromtablewheredatediff (week, C_CALLTIME, getdat

Note: Sunday is the first day of the week, not the last day of the week () minus one day, such as dateadd ('day',-1, getDate () This week: select * from table where datediff (week, C_CALLTIME, getdat

Note: Sunday is the first day of the week, not the last day of the week () minus one day, such as dateadd ('day',-1, getDate ())

This week: select * from table where datediff (week, C_CALLTIME, getdate () = 0 -- C_CALLTIME is the Date Field

This month: Select * from table where datediff (Month, C_CALLTIME, getdate () = 0 -- C_CALLTIME is the Date Field

This season: select * from table where datediff (qq, C_CALLTIME, getdate () = 0

Select * from table where datepart (mm, C_CALLTIME)/7 = datepart (mm, getdate ()/7

Yesterday

Select convert (varchar (10), getdate ()-1,120)

Tomorrow

Select convert (varchar (10), getdate () + 1,120)

Last Seven Days

Select * from tb whereTimeField> = convert (varchar (10), getdate ()-7,120)

The next seven days

Select * from tb whereTimeField <= convert (varchar (10), getdate () + 7,120) andTimeField> =TimeField

You can use the convert and dateadd functions in combination.

Use datediff (day,TimeColumn, getdate ())

Last month

Select * from tb where month (TimeField) = month (getdate ()-1

This month

Select * from tb where month (TimeField) = month (getdate ())

Next month

Select * from tb where month (TimeField) = month (getdate () + 1

-- If you want to query partitions in a table

--Yesterday

Select * From TableName Where DateDiff (dd, DateTimCol, GetDate () = 1

-- Tomorrow

Select * From TableName Where DateDiff (dd, GetDate (), DateTimCol) = 1

-- Last seven days

Select * From TableName Where DateDiff (dd, DateTimCol, GetDate () <= 7

-- The next seven days

Select * From TableName Where DateDiff (dd, GetDate (), DateTimCol) <= 7

-- Last week

Select * From TableName Where DateDiff (wk, DateTimCol, GetDate () = 1

-- This week

Select * From TableName Where DateDiff (wk, DateTimCol, GetDate () = 0

-- Next week

Select * From TableName Where DateDiff (wk, GetDate (), DateTimCol) = 1

-- Last month

Select * From TableName Where DateDiff (mm, DateTimCol, GetDate () = 1

--This month

Select * From TableName Where DateDiff (mm, DateTimCol, GetDate () = 0

-- Next month

Select * From TableName Where DateDiff (mm, GetDate (), DateTimCol) = 1

--------------------------------------------------------

This week

Select * from tb where datediff (week,TimeField, getdate () = 0

Last week

Select * from tb where datediff (week,TimeField, getdate () = 1

Next week

Select * from tb where datediff (week,TimeField, getdate () =-1

--------------------------------------------------------

1. Now I need to get only the date part,TimeSome do not, how to write SQL?

Select convert (varchar (10), getdate (), 120)

2. Calculate the SQL statement of the following date:

Yesterday

Select convert (varchar (10), getdate ()-1,120)

Tomorrow

Select convert (varchar (10), getdate () + 1,120)

Last Seven Days

Select * from tb whereTimeField> = convert (varchar (10), getdate ()-7,120)

The next seven days

Select * from tb whereTimeField <= convert (varchar (10), getdate () + 7,120) andTimeField> =TimeField

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.