SQL-几个日期函数
Posted lifedecideshappiness
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL-几个日期函数相关的知识,希望对你有一定的参考价值。
/* 人员:LDH 功能:SQL-几个日期函数 日期:2018-7-20 */ USE TSQLFundamentals2008; GO SELECT orderid, orderdate FROM Sales.Orders;
SELECT TOP (8) orderid, orderdate FROM Sales.Orders WHERE orderdate > ‘20080301‘; SELECT TOP (8) orderid, orderdate FROM Sales.Orders WHERE orderdate > CAST(‘20080301‘ AS DATETIME);
-- 年月日时分秒 SELECT DATEPART(YEAR, GETDATE()) AS ‘年‘, DATEPART(MONTH, GETDATE()) AS ‘月‘, DATEPART(DAY, GETDATE()) AS ‘日‘, DATEPART(HOUR, GETDATE()) AS ‘时‘, DATEPART(MINUTE, GETDATE()) AS ‘分‘, DATEPART(SECOND, GETDATE()) AS ‘秒‘; -- 特殊的几个 SELECT DATEPART(DAYOFYEAR, GETDATE()) AS ‘从年初到现在的天数‘, DATEPART(WEEKDAY, GETDATE()) AS ‘一周的第几天‘, DATEPART(WEEK, GETDATE()) AS ‘今年的第几周‘;
SELECT GETDATE() AS ‘CurrentDate‘, DATEADD(MONTH, 2, GETDATE()) AS ‘FutureDate‘; SELECT GETDATE() AS ‘CurrentDate‘, DATEADD(YEAR, 2, GETDATE()) AS ‘FutureDate‘;
-- 计算个人年龄 SELECT DATEDIFF(YEAR, ‘19890125‘, ‘20180720‘) AS ‘我的年龄‘; -- 建国周年数 SELECT DATEDIFF(YEAR, CAST(‘19491001‘ AS DATETIME), GETDATE()) AS ‘建国周年数‘;
以上是关于SQL-几个日期函数的主要内容,如果未能解决你的问题,请参考以下文章