博客园不常在线,有问题联系微信,微信:17873041739


SQL Server系列:系统函数之日期和时间函数

1.current_timestamp :获取数据库系统时间戳

--获取数据库系统时间戳
select current_timestamp
go

 

 2.getdate() :获取数据库系统时间戳

--获取数据库系统时间戳
select getdate()
go

 

 3.getutcdate() :获取utc时间

--获取utc时间
select getutcdate()
go

 

 4.sysdatetime() :获取计算机的日期和时间的 datetime2(7) 值

--获取计算机的日期和时间的 datetime2(7) 值
--与 getdate比较而言,sysdatetime的秒的小数部分精度更高。
select sysdatetime()
go

 

 5.sysdatetimeoffset() :获取计算机的日期和时间的 datetime(7) 值, 时区偏移量包含在内

--获取计算机的日期和时间的 datetime(7) 值, 时区偏移量包含在内
select sysdatetimeoffset()
go

 

 6.sysutcdatetime() :获取计算机的日期和时间的 datetime2(7) 值,utc时间

--获取计算机的日期和时间的 datetime2(7) 值,utc时间
select sysutcdatetime()
go

 

 7.year() :获取指定日期的年份

--获取指定日期的年份
select year('2020-07-01')
go

 

 8.month() :获取指定日期的月份

--获取指定日期的月份
select month('2020-07-01')
go

 

 9.day() :获取指定日期的日

--获取指定日期的日
select day('2020-07-01')
go

 

 10.isdate() :判断是否是日期,不是返回0,是返回1

--判断是否是日期,不是返回0,是返回1
select isdate('dfsfs')
select isdate('2020-07-01')
go

 

 

11.switchoffset() :获取从存储的时区偏移量变为指定的新时区偏移量时得到的 datetimeoffset 值 

--获取从存储的时区偏移量变为指定的新时区偏移量时得到的 datetimeoffset 值 
select switchoffset(SYSDATETIMEOFFSET(),'-08:00')
go

 

 12.todatetimeoffset() :获取从 datetime2 表达式转换的 datetimeoffset 值

--获取从 datetime2 表达式转换的 datetimeoffset 值
select todatetimeoffset(getdate(),'-08:00')
go

 

 13.dateadd() :将指定的数值添加到日期部分后的日期

--将指定的数值添加到日期部分后的日期
select dateadd(hh,5,'2020-07-01 10:30')
go

 

 14.datediff() :获取两个日期的指定日期部分的区别

--获取两个日期的指定日期部分的区别
select datediff(day,'2020-07-01','2020-08-01')
go

 

 15.datename() :日期中指定日期部分的字符串形式

--日期中指定日期部分的字符串形式
select datename(dw,'2020-07-20')
go

16.datepart() :获取日期中指定日期部分的整数形式

--获取日期中指定日期部分的整数形式
select datepart(day,'2020-07-20')
go

 

 ps:所有的

--获取数据库系统时间戳
select current_timestamp
go
--获取数据库系统时间戳
select getdate()
go
--获取utc时间
select getutcdate()
go
--获取计算机的日期和时间的 datetime2(7) 值
--与 getdate比较而言,sysdatetime的秒的小数部分精度更高。
select sysdatetime()
go
--获取计算机的日期和时间的 datetime(7) 值, 时区偏移量包含在内
select sysdatetimeoffset()
go
--获取计算机的日期和时间的 datetime2(7) 值,utc时间
select sysutcdatetime()
go
--获取指定日期的年份
select year('2020-07-01')
go
--获取指定日期的月份
select month('2020-07-01')
go
--获取指定日期的日
select day('2020-07-01')
go
--判断是否是日期,不是返回0,是返回1
select isdate('dfsfs')
select isdate('2020-07-01')
go
--获取从存储的时区偏移量变为指定的新时区偏移量时得到的 datetimeoffset 值 
select switchoffset(SYSDATETIMEOFFSET(),'-08:00')
go
--获取从 datetime2 表达式转换的 datetimeoffset 值
select todatetimeoffset(getdate(),'-08:00')
go
--将指定的数值添加到日期部分后的日期
select dateadd(hh,5,'2020-07-01 10:30')
go
--获取两个日期的指定日期部分的区别
select datediff(day,'2020-07-01','2020-08-01')
go
--日期中指定日期部分的字符串形式
select datename(dw,'2020-07-20')
go
--获取日期中指定日期部分的整数形式
select datepart(day,'2020-07-20')
go

 

posted @ 2020-07-20 14:46  Code技术分享  Views(735)  Comments(0Edit  收藏  举报

博客园不常在线,有问题联系微信,微信:17873041739