SQL语句--截取完整日期中的 年、月、日

SQL语句–截取完整日期中的 年、月、日

截取日期:几号

 SELECT  substring(Convert(VARCHAR(30),getdate(),112),7,2)

提取月份:

select month(getdate())
----或--
SELECT  substring(Convert(VARCHAR(30),getdate(),112),5,2)

提取年份:

select year(getdate())
----或--
SELECT  substring(Convert(VARCHAR(30),getdate(),112),1,4)

这些在 where 条件里也可以写

eg:------------------------------------------
SELECT  year(t1.FDate),month(t1.FDate),substring(Convert(VARCHAR(30),t1.FDate,112),7,2)FROM Salary t1
WHERE year(t1.FDate) ='2021' and 
      month(t1.FDate) !='09' and
      substring(Convert(VARCHAR(30),t1.FDate,112),7,2)='03' 

Salary表的FDate列,如下图:
《SQL语句--截取完整日期中的 年、月、日》

SELECT  year(t1.FDate),month(t1.FDate),substring(Convert(VARCHAR(30),t1.FDate,112),7,2)FROM Salary t1
WHERE year(t1.FDate) ='2021' and 
      month(t1.FDate) !='09' and
      substring(Convert(VARCHAR(30),t1.FDate,112),7,2)='03' 

执行语句后,结果如下:
《SQL语句--截取完整日期中的 年、月、日》

    原文作者:Mimo_YY
    原文地址: https://blog.csdn.net/Mimo_YY/article/details/114286007
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞