SQL Server 中的时间算法
DECLARE @Date DATETIME SET @Date=GETDATE() --前一天,给定日期的前一天 SELECT DATEADD(DAY,-1,@Date) AS '前一天' --后一天,给定日期的后一天 SELECT DATEADD(DAY,1,@Date) AS '后一天' GO --月初,计算给定日期所在月的第一天 --这个计算的技巧是先计
DECLARE @Date DATETIME
SET @Date=GETDATE()
--前一天,给定日期的前一天
SELECT DATEADD(DAY,-1,@Date) AS '前一天'
--后一天,给定日期的后一天
SELECT DATEADD(DAY,1,@Date) AS '后一天'
GO
--月初,计算给定日期所在月的第一天
--这个计算的技巧是先计算当前日期到“1900-01-01”的时间间隔数,然后把它加到“1900-01-01”上来获得特殊的日期,这个技巧可以用---来计算很多不同的日期。
DECLARE @Date DATETIME
SET @Date=GETDATE()
SELECT DATEADD(MONTH,DATEDIFF(MONTH,'1900-01-01',@Date),'1900-01-01') AS '所在月的第一天'
--精简算法,根据SQL Server的时间表示方式可知,'1900-01-01' 可以用0代替
SELECT DATEADD(MONTH,DATEDIFF(MONTH,0,@Date),0) AS '所在月的第一天'
--上面两种算法精确到天 时分秒均为00:00:00.000
--下面算法课以保留时分秒
--思路:用给定日期减去月第一天与给定日期差的天数
SELECT DATEADD(DAY,1-DATEPART(DAY,@Date),@Date)
GO
--月末,计算给定日期所在月的最后一天
DECLARE @Date DATETIME
SET @Date=GETDATE()
--思路:当前月的下一月1号在减1天
SELECT DATEADD(DAY,-1,DATEADD(MONTH,1+DATEDIFF(MONTH,'1900-01-01',@Date),'1900-01-01')) AS '所在月的最一天'
SELECT DATEADD(MONTH,1+DATEDIFF(MONTH,'1900-01-01',@Date),'1900-01-01')-1 AS '所在月的最一天'
--1900-01-01 用0代替
SELECT DATEADD(DAY,-1,DATEADD(MONTH,1+DATEDIFF(MONTH,0,@Date),0)) AS '所在月的最一天'
SELECT DATEADD(MONTH,1+DATEDIFF(MONTH,0,@Date),0)-1 AS '所在月的最一天'
--思路:与月初计算思路相同
SELECT DATEADD(MONTH,DATEDIFF(MONTH,'1989-12-31',@Date),'1989-12-31') AS '所在月的最一天'
--精简算法,'1989-12-31' 用-1代替
SELECT DATEADD(MONTH,DATEDIFF(MONTH,-1,@Date),-1) AS '所在月的最一天'
--保留时分秒的算法
SELECT DATEADD(DAY,-1,DATEADD(MONTH,1,DATEADD(DAY,1-DATEPART(DAY,@Date),@Date)))
GO
--其他月计算
--计算给定日期所在月的上月第一天
DECLARE @Date DATETIME
SET @Date=GETDATE()
--当前月第一天减去一个月
SELECT DATEADD(MONTH,-1,DATEADD(MONTH,DATEDIFF(MONTH,0,@Date),0)) AS '上月第一天'
--简化
SELECT DATEADD(MONTH,DATEDIFF(MONTH,0,@Date)-1,0) AS '上月第一天'
--另一种当前月第一天算法
SELECT DATEADD(MONTH,-1,DATEADD(DAY,1-DATEPART(DAY,@Date),@Date)) '上月第一天'
GO
--计算给定日期所在月的上月最后一天
DECLARE @Date DATETIME
SET @Date=GETDATE()
--当前月第一天减去一天
SELECT DATEADD(DAY,-1,DATEADD(MONTH,DATEDIFF(MONTH,0,@Date),0)) AS '上月最后一天'
--另一种当前月第一天算法
SELECT DATEADD(DAY,-1,DATEADD(DAY,1-DATEPART(DAY,@Date),@Date)) '上月最后一天'
SELECT DATEADD(DAY,1-DATEPART(DAY,@Date),@Date)-1 '上月最后一天'
--另一种算法,不能用当前月的最后一天加一个月,因为当前月可能是30天。
--例如 SELECT DATEADD(MONTH,1,'2010-06-30') --结果是2010-07-30而不是2010-07-31,
--这也是月末算法采用下月第一天减1天计算的原因
--但是如果计算月是31天择无此问题
--例如 SELECT DATEADD(MONTH,1,'2010-05-31') --结果是2010-06-30
--因此下面算法是正确的,-1 表示'1899-12-31 00:00:00.000'-- SELECT CONVERT(DATETIME,-1)
SELECT DATEADD(MONTH,DATEDIFF(MONTH,-1,@Date)-1,-1)
--另一种当前月算法
SELECT DATEADD(DAY,-1,DATEADD(DAY,1-DATEPART(DAY,@Date),@Date)) '上月最后一天'
--简化
SELECT DATEADD(DAY,0-DATEPART(DAY,@Date),@Date) '上月最后一天'
GO
--计算给定日期所在月的下月第一天
DECLARE @Date DATETIME
SET @Date=GETDATE()
--当前月第一天加一个月
SELECT DATEADD(MONTH,1,DATEADD(MONTH,DATEDIFF(MONTH,0,@Date),0)) AS '下月第一天'
--简化
SELECT DATEADD(MONTH,DATEDIFF(MONTH,0,@Date)+1,0) AS '下月第一天'
--另一种当前月第一天算法
SELECT DATEADD(MONTH,1,DATEADD(DAY,1-DATEPART(DAY,@Date),@Date)) '下月第一天'
GO
--计算给定日期所在月的下月最后一天
DECLARE @Date DATETIME
SET @Date=GETDATE()
--当前月第一天加2个月再减去1天
SELECT DATEADD(DAY,-1,DATEADD(MONTH,2,DATEADD(MONTH,DATEDIFF(MONTH,0,@Date),0))) AS '下月最后一天'
--简化
SELECT DATEADD(DAY,-1,DATEADD(MONTH,DATEDIFF(MONTH,0,@Date)+2,0)) AS '下月最后一天'
SELECT DATEADD(MONTH,DATEDIFF(MONTH,0,@Date)+2,0)-1 AS '下月最后一天'
--另一种算法
SELECT DATEADD(MONTH,DATEDIFF(MONTH,-1,@Date)+1,-1) '下月最后一天'
--另一种当前月第一天算法
SELECT DATEADD(DAY,-1,DATEADD(MONTH,2,DATEADD(DAY,1-DATEPART(DAY,@Date),@Date))) '下月最后一天'
GO
--所在星期的第一天,计算给定日期所在星期的第1天(星期日为第一天)
DECLARE @Date DATETIME
SET @Date= GETDATE()
--与SQL Server语言版本相关的算法
--思路:当前日期+星期日(每周的第1天)与当前日期的差的天数
--DATEPART(WEEKDAY,DATE)的返回值与@@DATEFIRST相关
SET DATEFIRST 7 -- 或者设置为美国英语SET LANGUAGE us_english; (星期日为第一天)
SELECT DATEADD(WEEKDAY,1-DATEPART(WEEKDAY,@Date),@Date) AS '所在星期的第一天,星期日'
--星期日,与SQL Server语言版本或@@DATEFIRST无关
--'1989-12-31' 是星期日,'1989-12-31' 再加上(当前日期与1989-12-31差的星期数)个星期
SELECT DATEADD(WEEK,DATEDIFF(WEEK,-1,@Date),-1) AS '所在星期的星期日'
--或者
SELECT DATEADD(WEEK,DATEDIFF(WEEK,6,@Date),6) AS '所在星期的星期日'
GO
--所在星期的第二天,计算给定日期所在星期的第2天(星期日为第一天)
DECLARE @Date DATETIME
SET @Date= GETDATE()
--思路:当前日期+星期一(每周的第2天)与当前日期的差的天数
--DATEPART(WEEKDAY,DATE)的返回值与@@DATEFIRST相关
SET DATEFIRST 7 -- 或者设置为美国英语SET LANGUAGE us_english; (星期日为第一天)
SELECT DATEADD(DAY,2-DATEPART(WEEKDAY,@Date),@Date) AS '所在星期的第二天,星期一'
--星期一,与SQL Server语言版本或@@DATEFIRST无关
--'1900-01-01' 是星期一,'1900-01-01' 再加上(当前日期与1900-01-01差的星期数)个星期
SELECT DATEADD(WEEK,DATEDIFF(WEEK,0,@Date),0) AS '所在星期的星期一'
GO
--上个星期第一天,计算给定日期所在星期的上一个星期日(星期日为第一天)
DECLARE @Date DATETIME
SET @Date= GETDATE()
--思路:当前日志所在星期的星期日再减1周
--DATEPART(WEEKDAY,DATE)的返回值与@@DATEFIRST相关
SET DATEFIRST 7 -- 或者设置为美国英语SET LANGUAGE us_english; (星期日为第一天)
SELECT DATEADD(WEEK,-1,DATEADD(DAY,1-DATEPART(WEEKDAY,@Date),@Date)) AS '上个星期第一天,星期日'
--一周等于7天
SELECT DATEADD(DAY,-7,DATEADD(DAY,1-DATEPART(WEEKDAY,@Date),@Date)) AS '上个星期第一天,星期日'
--简化
SELECT DATEADD(DAY,-6-DATEPART(WEEKDAY,@Date),@Date) AS '上个星期第一天,星期日'
--上个星期日,与SQL Server语言版本或@@DATEFIRST无关
SELECT DATEADD(WEEK,-1+DATEDIFF(WEEK,-1,@Date),-1) AS '上个星期日'
--或者
SELECT DATEADD(WEEK,DATEDIFF(WEEK,6,@Date),-1) AS '上个星期日'
GO
--下个星期第一天,计算给定日期所在星期的下一个星期日(星期日为第一天)
DECLARE @Date DATETIME
SET @Date= GETDATE()
--思路:当前日志所在星期的星期日再加1周
--DATEPART(WEEKDAY,DATE)的返回值与@@DATEFIRST相关
SET DATEFIRST 7 -- 或者设置为美国英语SET LANGUAGE us_english; (星期日为第一天)
SELECT DATEADD(WEEK,1,DATEADD(DAY,1-DATEPART(WEEKDAY,@Date),@Date)) AS '下个星期第一天,星期日'
--一周等于7天
SELECT DATEADD(DAY,7,DATEADD(DAY,1-DATEPART(WEEKDAY,@Date),@Date)) AS '下个星期第一天,星期日'
--简化
SELECT DATEADD(DAY,8-DATEPART(WEEKDAY,@Date),@Date) AS '下个星期第一天,星期日'
--下个星期日,与SQL Server语言版本或@@DATEFIRST无关
SELECT DATEADD(WEEK,1+DATEDIFF(WEEK,-1,@Date),-1) AS '下个星期日'
--或者
SELECT DATEADD(WEEK,DATEDIFF(WEEK,-1,@Date),6) AS '下个星期日'
GO
--判断给定日期是星期几
DECLARE @Date DATETIME
SET @Date= GETDATE()
--DATEPART(WEEKDAY,DATE)的返回值与@@DATEFIRST相关
SET DATEFIRST 7 -- 或者设置为美国英语SET LANGUAGE us_english; (星期日为第一天)
SELECT DATEPART(WEEKDAY,@Date) --返回值 1-星期日,2-星期一,3-星期二......7-星期六
--上面算法与SQL 语言版本或 @@DATEFIRST 相关
--下面算法与SQL Server语言版本或@@DATEFIRST无关
SELECT DATENAME(WEEKDAY,@Date) '星期'
GO
--年度计算
DECLARE @Date DATETIME
SET @Date=GETDATE()
--年初,计算给定日期所在年的第一天
SELECT DATEADD(YEAR,DATEDIFF(YEAR,0,@Date),0) AS '所在年的第一天'
--年末,计算给定日期所在年的最后一天
SELECT DATEADD(YEAR,DATEDIFF(YEAR,-1,@Date),-1) AS '所在年的最后一天'
--上一年年初,计算给定日期所在年的上一年的第一天
SELECT DATEADD(YEAR,DATEDIFF(YEAR,-0,@Date)-1,0) AS '所在年的上一年的第一天'
--上一年年末,计算给定日期所在年的上一年的最后一天
SELECT DATEADD(YEAR,DATEDIFF(YEAR,0,@Date),-1) AS '所在年的上一年的最后一天'
--下一年年初,计算给定日期所在年的下一年的第一天
SELECT DATEADD(YEAR,1+DATEDIFF(YEAR,0,@Date),0) AS '所在年的下一年的第一天'
--下一年年末,计算给定日期所在年的下一年的最后一天
SELECT DATEADD(YEAR,1+DATEDIFF(YEAR,-1,@Date),-1) AS '所在年的下一年的最后一天'
GO
--季度计算
DECLARE @Date DATETIME
SET @Date=GETDATE()
--季度初,计算给定日期所在季度的第一天
SELECT DATEADD(QUARTER,DATEDIFF(QUARTER,0,@Date),0) AS '当前季度的第一天'
--季度末,计算给定日期所在季度的最后一天
SELECT DATEADD(QUARTER,1+DATEDIFF(QUARTER,0,@Date),-1) AS '当前季度的最后一天'
--上个季度初
SELECT DATEADD(QUARTER,DATEDIFF(QUARTER,0,@Date)-1,0) AS '当前季度的上个季度初'
--上个季度末
SELECT DATEADD(QUARTER,DATEDIFF(QUARTER,0,@Date),-1) AS '当前季度的上个季度末'
--下个季度初
SELECT DATEADD(QUARTER,1+DATEDIFF(QUARTER,0,@Date),0) AS '当前季度的下个季度初'
--下个季度末
SELECT DATEADD(QUARTER,2+DATEDIFF(QUARTER,0,@Date),-1) AS '当前季度的下个季度末'
--计算给定日期所在月的天数
DECLARE @Date DATETIME;
SET @Date = GETDATE()
--本月度第一天与下月度第一天所差的天数
SELECT DATEDIFF(DAY,DATEADD(MONTH,DATEDIFF(MONTH,0,@Date),0),DATEADD(MONTH,1+DATEDIFF(MONTH,0,@Date),0))
--借助变量简化
SELECT @Date = DATEADD(MONTH,DATEDIFF(MONTH,0,@Date),0) --本月度第一天
SELECT DATEDIFF(DAY,@Date,DATEADD(MONTH,1,@Date))
--另一种思路:给定月最后一天的日期,,记为本月天数
SELECT DAY(DATEADD(MONTH,DATEDIFF(MONTH,-1,@Date),-1))
GO
--计算给定日期所在季度的天数
DECLARE @Date DATETIME;
SET @Date = GETDATE()
--本季度第一天与下季度第一天所差的天数
SELECT DATEDIFF(DAY,DATEADD(QUARTER,DATEDIFF(QUARTER,0,@Date),0),DATEADD(QUARTER,1+DATEDIFF(QUARTER,0,@Date),0))
--借助变量简化
SELECT @Date = DATEADD(QUARTER,DATEDIFF(QUARTER,0,@Date),0) --本季度第一天
SELECT DATEDIFF(DAY,@Date,DATEADD(QUARTER,1,@Date))
GO
--计算给定日期所在年度的天数
DECLARE @Date DATETIME;
SET @Date = GETDATE()
--本年度第一天与下年度第一天所差的天数
SELECT DATEDIFF(DAY,DATEADD(YEAR,DATEDIFF(YEAR,0,@Date),0),DATEADD(YEAR,1+DATEDIFF(YEAR,0,@Date),0))
--借助变量简化
SELECT @Date = DATEADD(YEAR,DATEDIFF(YEAR,0,@Date),0) --本年度第一天
SELECT DATEDIFF(DAY,@Date,DATEADD(YEAR,1,@Date))
GO
--判断给定日期所在年是否闰年
--根据全年总天数判断
DECLARE @Date DATETIME;
SET @Date = GETDATE()
SELECT CASE DATEDIFF(DAY,DATEADD(YEAR,DATEDIFF(YEAR,0,@Date),0),DATEADD(YEAR,1+DATEDIFF(YEAR,0,@Date),0))
WHEN 365 THEN '平年' ELSE '闰年' END
--根据二月天数判断
--给日期的上一年最后一天加2个月,即为当年2月最后一天
SELECT CASE DAY(DATEADD(MONTH,2,DATEADD(YEAR,DATEDIFF(YEAR,0,@Date),-1))) WHEN 28 THEN '平年' ELSE '闰年' END
GO
--计算给定日期是当年的第几天
DECLARE @Date DATETIME;
SET @Date = GETDATE()
SELECT DATEPART(DAYOFYEAR,@Date) [DayOfYear];
SELECT DATENAME(DAYOFYEAR,@Date) [DayOfYear];
--另一种思路:当前日期与上年最后一天差的天数
SELECT DATEDIFF(DAY,DATEADD(YEAR,DATEDIFF(YEAR,0,@Date),-1),@Date)[DayOfYear]
GO
--计算给定日期是当年的第几周
DECLARE @Date DATETIME;
SET @Date = GETDATE()
SELECT DATEPART(WEEK,@Date) [WeekOfYear]; --返回int型
SELECT DATENAME(WEEK,@Date) [WeekOfYear]; --返回varchar型
GO
--计算给定日期是当年的第几月
DECLARE @Date DATETIME;
SET @Date = GETDATE()
SELECT DATEPART(MONTH,@Date) [MonthOfYear]; --返回int型
SELECT DATENAME(MONTH,@Date) [MonthOfYear]; --返回varchar型
SELECT MONTH(@Date) [MonthOfYear];--返回int型
GO
--计算给定日期是当年的第几季度
DECLARE @Date DATETIME;
SET @Date = GETDATE()
SELECT DATEPART(QUARTER,@Date) [QuarterOfYear]; --返回int型
SELECT DATENAME(QUARTER,@Date) [QuarterOfYear]; --返回varchar型
GO
--计算给定日期是当月的第几周
DECLARE @Date DATETIME;
SET @Date = GETDATE()
--思路,给定日期是当年的第几周-给定日期所在月第一天是当年的第几周
SELECT DATEPART(WEEK,@Date)-DATEPART(WEEK,DATEADD(MONTH,DATEDIFF(MONTH,0,@Date),0))+1 [WeekOfMonth]
SELECT DATEPART(WEEK,@Date)-DATEPART(WEEK,DATEADD(DAY,1-DATEPART(DAY,@Date),@Date))+1 [WeekOfMonth]
GO
--计算给定日期所在月的第一个星期一是哪天
DECLARE @Date DATETIME;
SET @Date = GETDATE()
--思路,1900-01-01(星期一)加上(给定日志所在月的月6号与1900-01-01差的周数)个周
--为什么不选7号?如果是7号,那么7好恰好是星期日的话,第一个周一就会算到8号。
--为什么不选5号?如果5号是星期六,那么周一就跑到上月了。小于5号与这个道理一样。
SELECT DATEADD(WEEK,DATEDIFF(WEEK,0,DATEADD(DAY,6-DATEPART(DAY,@Date),@Date)),0) '所在月的第一个星期一'
SELECT DATEADD(WEEK,DATEDIFF(WEEK,0,DATEADD(DAY,6-DATEPART(DAY,@Date),@Date)),7) '所在月的第二个星期一'
SELECT DATEADD(WEEK,DATEDIFF(WEEK,0,DATEADD(DAY,6-DATEPART(DAY,@Date),@Date)),1) '所在月的第一个星期二'
SELECT DATEADD(WEEK,DATEDIFF(WEEK,0,DATEADD(DAY,6-DATEPART(DAY,@Date),@Date)),8) '所在月的第二个星期二'
GO

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

On the Douyin platform, many users are eager to obtain level certification, and the level 10 light sign shows the user's influence and recognition on Douyin. This article will delve into the price of Douyin’s level 10 light boards and the time it takes to reach this level to help users better understand the process. 1. How much does a level 10 Douyin light sign cost? The price of Douyin's 10-level light signs will vary depending on market fluctuations and supply and demand. The general price ranges from a few thousand yuan to ten thousand yuan. This price mainly includes the cost of the light sign itself and possible service fees. Users can purchase level 10 light signs through Douyin’s official channels or third-party service agencies, but they should pay attention to legal channels when purchasing to avoid false or fraudulent transactions. 2. How many days does it take to create a level 10 fan sign? Reach level 10 light sign

Written above & the author’s personal understanding: At present, in the entire autonomous driving system, the perception module plays a vital role. The autonomous vehicle driving on the road can only obtain accurate perception results through the perception module. The downstream regulation and control module in the autonomous driving system makes timely and correct judgments and behavioral decisions. Currently, cars with autonomous driving functions are usually equipped with a variety of data information sensors including surround-view camera sensors, lidar sensors, and millimeter-wave radar sensors to collect information in different modalities to achieve accurate perception tasks. The BEV perception algorithm based on pure vision is favored by the industry because of its low hardware cost and easy deployment, and its output results can be easily applied to various downstream tasks.

Common challenges faced by machine learning algorithms in C++ include memory management, multi-threading, performance optimization, and maintainability. Solutions include using smart pointers, modern threading libraries, SIMD instructions and third-party libraries, as well as following coding style guidelines and using automation tools. Practical cases show how to use the Eigen library to implement linear regression algorithms, effectively manage memory and use high-performance matrix operations.

Xiaohongshu, a platform full of life and knowledge sharing, allows more and more creators to express their opinions freely. In order to get more attention and likes on Xiaohongshu, in addition to the quality of content, the time of publishing works is also crucial. So, how to set the time for Xiaohongshu to publish works? 1. How to set the time for publishing works on Xiaohongshu? 1. Understand the active time of users. First, it is necessary to clarify the active time of Xiaohongshu users. Generally speaking, 8 pm to 10 pm and weekend afternoons are the times when user activity is high. However, this time period will also vary depending on factors such as audience group and geography. Therefore, in order to better grasp the active period of users, it is recommended to conduct a more detailed analysis of the behavioral habits of different groups. By understanding users’ lives

Players can experience the main plot of the game and collect game achievements when playing in Elden's Circle. Many players don't know how long it takes to clear Elden's Circle. The player's clearance process is 30 hours. How long does it take to clear the Elden Ring? Answer: 30 hours. 1. Although this 30-hour clearance time does not refer to a master-like speed pass, it also omits a lot of processes. 2. If you want to get a better game experience or experience the complete plot, then you will definitely need to spend more time on the duration. 3. If players collect them all, it will take about 100-120 hours. 4. If you only take the main line to brush BOSS, it will take about 50-60 hours. 5. If you want to experience it all: 150 hours of base time.

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

The bottom layer of the C++sort function uses merge sort, its complexity is O(nlogn), and provides different sorting algorithm choices, including quick sort, heap sort and stable sort.

01 Outlook Summary Currently, it is difficult to achieve an appropriate balance between detection efficiency and detection results. We have developed an enhanced YOLOv5 algorithm for target detection in high-resolution optical remote sensing images, using multi-layer feature pyramids, multi-detection head strategies and hybrid attention modules to improve the effect of the target detection network in optical remote sensing images. According to the SIMD data set, the mAP of the new algorithm is 2.2% better than YOLOv5 and 8.48% better than YOLOX, achieving a better balance between detection results and speed. 02 Background & Motivation With the rapid development of remote sensing technology, high-resolution optical remote sensing images have been used to describe many objects on the earth’s surface, including aircraft, cars, buildings, etc. Object detection in the interpretation of remote sensing images
