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列,如下图:
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'
执行语句后,结果如下: