sql 中,如何获取两个日期之前月数、周数、天数

1、获取两个日期之间的月数、周数、天数语法

   --1、获取两个日期之间的月数、周数、天数
--1.1)声明参数
declare @startDate varchar(20)
declare @endDate varchar(20)
--1.2)获取两个日期直接的月数
select DATEDIFF(MM,@startDate,@endDate)
--1.3)获取两个日期直接的月数
select DATEDIFF(WW,@startDate,@endDate)
--1.4)获取两个日期直接的月数
select DATEDIFF(DD,@startDate,@endDate)

2、测试用例

    --2、测试用例
--2.1)获取指定日期和当前日期直接的月数
select DATEDIFF(MM,'2017-08-12 11:12:23',GETDATE()) as 月数
--2.2)获取指定日期和当前日期直接的周数
select DATEDIFF(WW,'2017-08-12 11:12:23',GETDATE()) as 周数
--2.3)获取指定日期和当前日期直接的月数
select DATEDIFF(DD,'2017-08-12 11:12:23',GETDATE()) as 天数

3、测试结果

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKQAAADJCAIAAAAivQ6SAAAIuklEQVR4nO2dTWvjOACG9Z/cFjr/pWR70e8ohbI99pRFtxxKoGznkoU5hzkNtYbeNixDTmVgyLbg9LD0pj0ocRTbUhzbsvXxPojgUdwo46evJLuOQr4rZFkmhLj+/e4/hclkkmWZfGyJ6Jm/P5Orq03542vhOZo/dXX16eu/6nPk6koIsi0NufskCFEK3T7xQ3zar/xxt9uNftm9AlV+/Ev+grTcVF1IyLJb0Va2g0B2BZ0k20GOkD30WwVtsZvs78AlIDsiIDsiIDsiOpD92xbIdpy2sg2mIds1Wsk2m4Zs12gu+6BpyHYNJDsiMGZHBGbjEYHz7IiA7IiA7IiA7Ijo9eYFMCxHyAa+U1c2CADIjgjIjgjIjoi6sv/xlqGPsEMcIbvHe147A7JVwpftPpDdDX0eymZ4JpsyXlkSWiw9Wt5QPpTz+Xw+n8/2mUzuezviBfyTzbjYFCaopphlE0IM/+xQNmPFt7FarShjvR3xAv7JPpjphPIkaSWb6Gkme71e81fBX8X0KaMUsuvJVrOSpmm+PZ/P5cbHxwdjPEm0nyzOhRlcqlJ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bxcxnY5I4zUukewhZWuT/XBJRo/KIof5Gnit1rhEsh1M9sbo3oqW/PaEENKiD0eyXUx27ngne9eNt1rmEsl2LdnP16d796Kc3DyrEW+zgC2S7VyyyxH/9XBJdsluPkFDsl1LdpXst/fpaJv0FgvYItm4ghYajsru0zSSjWQHiKOykWwbOCobybaBo7clZVio1gIu3nAILAHZEQHZEQHZEQHZEQHZEQHZEQHZEQHZEQHZEQHZEQHZEQHZEQHZEfE/OgMrcorqGdkAAAAASUVORK5CYII=" alt="" />

上一篇:java性能调优---------------------JVM调优方案


下一篇:【开源java游戏框架libgdx专题】-13-开发工具-地图的使用