Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to query the date and time of SQL SERVER

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to query the date and time of SQL SERVER". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to query the date and time of SQL SERVER" can help you solve the problem.

Commonly used date query-YYYY-- the day before the first day of the current year SELECT TRUNC (SYSDATE,'yyyy')-1 + 8 / 24 FROM DUAL---2019/12/31  08 / 00 FROM DUAL---2019/12/31 SELECT TRUNC-the first day of the current year SELECT TRUNC (SYSDATE,'yyyy') + 8 / 24 FROM DUAL---2020/01/01 SYSDATE the first day of the current year SELECT TRUNC (SYSDATE) 'MM')-1 + 8 / 24 FROM DUAL---2020/04/30 -the day before the first day of the current month SELECT TRUNC (SYSDATE,'MM') + 8 / 24 FROM DUAL---2020/05/01 -the previous day of the current day SELECT TRUNC (SYSDATE) 'DD')-1 + 8 / 24 FROM DUAL---2020/05/19  Morning 08:00:00SELECT TRUNC (SYSDATE)-1 + 8 / 24 FROM DUAL---2020/5/19  Morning 08  0000 -current Day SELECT TRUNC (SYSDATE) 'DD') + 8 / 24 FROM DUAL---2020/05/20  morning 08:00:00SELECT TRUNC (SYSDATE) + 8 / 24 FROM DUAL---2020/ 5  20  08DD' 0000kuwashi-the first day of the current week SELECT TRUNC (SYSDATE,'D') FROM DUAL---2020/05/17 the first day of the current week {is Sunday}-the current time of the HH-- Accurate to hour SELECT TRUNC (SYSDATE,'HH') FROM DUAL---2020/05/20  03SELECT TRUNC (SYSDATE,'HH') pm-one hour before the current time SELECT TRUNC (SYSDATE,'HH')-1 bank 24 FROM DUAL---2020/05/20  pm 02VV 00lylylylylylyMIMui-current time Accurate to the minute SELECT TRUNC (SYSDATE,'MI') FROM DUAL---2020/05/20  03pm 40 yyyymmdd' yyyymmdd'-one minute before the current time SELECT TRUNC (SYSDATE,'MI')-1-24-60 FROM DUAL---2020/05/20  afternoon 03Groupe 3900muri-last week SELECT TO_CHAR (TRUNC (SYSDATE,' IW')-7, 'yyyymmdd') FROM DUALSELECT TO_CHAR (TRUNC (SYSDATE,' IW')-1 'yyyymmdd') FROM DUAL--- this week on SELECT TO_CHAR (TRUNC (SYSDATE,' IW'), 'yyyymmdd') FROM DUALSELECT TO_CHAR (TRUNC (SYSDATE,' IW') + 6, 'yyyymmdd') FROM DUAL--- first and last day of January select trunc (add_months (sysdate,-1),' mm') first_day, last_day (add_months (sysdate) -1)) last_day from dual--- last day of this month SELECT TO_CHAR (last_day (add_months (sysdate, 0)), 'yyyymmdd') FROM DUAL--- first day of the year SELECT TO_CHAR (trunc (sysdate,' yyyy'),' yyyymmdd') FROM DUAL--- last year SELECT TO_CHAR (trunc (sysdate,'iyyy'), 'yyyymmdd') FROM DUAL--- first day of the month SELECT TO_CHAR (trunc (sysdate,'mm')) 'yyyymmdd') FROM DUAL--- current month value SELECT TO_CHAR (sysdate,'mm') FROM DUAL--- current week value SELECT TO_CHAR (sysdate,'ww') FROM DUAL time query method

1. Get the current time

Select getdate ()

2. Intercept the required value

Select datepart (year,getdate ()) select datepart (month,getdate) select datepart (day,getdate ()) select datepart (hour,getdate ()) select datepart (minute,getdate ()) select datepart (second,getdate ()) select datepart (week,getdate ())

3. Add or subtract the specified time interval from the date

Select dateadd (year,3,getdate ())-- get the current time, postpone select dateadd (month,3,getdate ()) for three years, get the current time, postpone select dateadd (day,3,getdate ()) for three months, get the current time, postpone select dateadd (hour,3,getdate ()) for three days, get the current time, postpone select dateadd (minute,3,getdate ()) for three hours later-- get the current time. Postpone select dateadd (second,3,getdate ()) for three minutes-- get the current time, postpone it for three seconds

4. Return the time between two dates

Select datediff (year,'2020/11/30',getdate ())-how many years is the difference between 2001-08-19 and the current time select datediff (month,'2020/11/30',getdate ())-- how many months is the difference between the 2001-08-19 and the current time select datediff (day,'2020/11/30',getdate ())-- 2001-08-19 and the current time?

5. Display the date / time in different formats

Select convert (char,getdate (), 8)-display current time: minutes: seconds "15:00:19" select convert (char,getdate (), 10)-display current month-day-year, display form "12-01-20" select convert (char,getdate (), 11)-display current year-month-day, display form "20-12-01" select convert (char,getdate (), 14)-display current time-minutes-seconds-milliseconds Display form: "14 GETDATE 58, GETDATE 06 GETDATE 340"-- 2020/12/01select convert (varchar, GETDATE)-- 20201201select convert (varchar, GETDATE, 120)-- 2020-12-01 15:09:51select convert (varchar, GETDATE, 121)-- 2020-12-01 15:10:03.717select convert (varchar, GETDATE) 11) +'+ convert (char,getdate (), 8)-- select convert (char,getdate (), 8) on "how to query the date and time of SQL SERVER" is here. Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report