SQL get time

Source: Internet
Author: User

Obtain the current system time using SQL.

Date part convert (varchar (10), getdate (), 120)

Yesterday select convert (varchar (10), getdate ()-1,120)

Tomorrow select convert (varchar (10), getdate () + 1,120)

Select * from TB where time field> = convert (varchar (10), getdate ()-7,120) in the last seven days)

Select * from TB where time field <= convert (varchar (10), getdate () + 7,120) and time field> = time field in the next seven days

For example

-- Yesterday select * From tablename where datediff (DD, datetimcol, getdate () = 1

-- Tomorrow select * From tablename where datediff (DD, getdate (), datetimcol) = 1

-- Select * From tablename where datediff (DD, datetimcol, getdate () in the last seven days <= 7

-- Select * From tablename where datediff (DD, getdate (), datetimcol) in the next seven days <= 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

Another one I wrote

Select top 1. topicid,. title,. boardid,. postusername,. lastpost,. lastposttime,. dateandtime, B. boardtype from dv_topic A, dv_board B where. boardid <> 444 and B. boardid =. boardid and (. istop = 1 or. isbest = 1) and (dateandtimeBetween (select convert (varchar (10), getdate ()-30,120) and (select convert (varchar (10), getdate (), 120 )))Order by hits DESC posts added as excellent or top posts in the forum within one month

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.