[数据库基础]——快速浏览日期时间转换

数据库日期和时间类型

相互转换

    time ? date

    time ? smalldatetime

    time ? datetime

    time ? datetime2

    time ? datetimeoffset

    date ? smalldatetime

    date ? datetime

    date ? datetime2

    date ? datetimeoffset

    smalldatetime ? datetime

    smalldatetime ? datetime2

    smalldatetime ? datetimeoffset

    datetime ? datetime2

    datetime ? datetimeoffset

    datetime2 ? datetimeoffset

数据库日期和时间类型:


类型


格式


范围


time

hh:mm:ss[.nnnnnnn] 00:00:00.0000000 ~ 23:59:59.9999999

date

YYYY-MM-DD 0001-01-01 ~ 9999-12-31

smalldatetime

YYYY-MM-DD hh:mm:ss 1900-01-01 ~ 079-06-06

datetime

YYYY-MM-DD hh:mm:ss[.nnn] 1753-01-01 ~ 99-12-31

datetime2

YYYY-MM-DD hh:mm:ss[.nnnnnnn] 0001-01-01 00:00:00.0000000 ~ 99-12-31 23:59:59.9999999

datetimeoffset

YYYY-MM-DD hh:mm:ss[.nnnnnnn] [+|-]hh:mm 0001-01-01 00:00:00.0000000 ~9999-12-31 23:59:59.9999999 (in UTC)

相互转换SET DATEFORMAT dmy;

time ? date


   1:  -- time -> date
   2:  DECLARE @time time(4) = ‘12:15:04.1234‘;
   3:  DECLARE @date date= @time;
   4:   
   5:  SELECT @time AS ‘@time‘, @date AS ‘@date‘;

Result:

Operand type clash: time is incompatible with date


   1:  -- date -> time
   2:  DECLARE @date date= ‘12-10-25‘;
   3:  DECLARE @time time= @date;
   4:   
   5:  SELECT @date AS ‘@date‘, @time AS ‘@time‘;

Result:

Operand type clash: time is incompatible with date

time ? smalldatetime


   1:  -- time -> smalldatetime
   2:  DECLARE @time time(4) = ‘12:15:04.1234‘;
   3:  DECLARE @smalldatetime smalldatetime= @time;
   4:   
   5:  SELECT @time AS ‘@time‘, @smalldatetime AS ‘@smalldatetime‘;

Result:

@time               @smalldatetime
12:15:04.1234    1900-01-01 12:15:00


   1:  -- samlldatetime –> time
   2:  DECLARE @smalldatetime smalldatetime = ‘13-12-1995 12:43:10‘;
   3:  DECLARE @time time(4) = @smalldatetime;
   4:   
   5:  SELECT @smalldatetime AS ‘@smalldatetime‘, @time AS ‘time‘;

Result:

@smalldatetime        @time
1995-12-13 12:43:00    12:43:00.0000

time ? datetime


   1:  -- time -> datetime
   2:  DECLARE @time time(4) = ‘12:10:05.1234‘;
   3:  DECLARE @datetime datetime = @time;
   4:   
   5:  SELECT @time AS ‘@datetime‘, @datetime AS ‘@time‘;

Result:

@time                      @datetime
12:10:05.1234            1900-01-01 12:10:05.123


   1:  -- datetime -> time
   2:  DECLARE @datetime datetime = ‘12-11-05 12:10:05.123‘;
   3:  DECLARE @time time(4) = @datetime;
   4:   
   5:  SELECT @datetime AS ‘@datetime‘, @time AS ‘@time‘;

Result:

@datetime                       @time
2005-11-12 12:10:05.123    12:10:05.1230

time ? datetime2


   1:  -- time -> datetime2
   2:  DECLARE @time time(4) = ‘12:15:04.1234‘;
   3:  DECLARE @datetime2 datetime2(3) = @time;
   4:   
   5:  SELECT @datetime2 AS ‘@datetime2‘, @time AS ‘@time‘;

Result:

@time               @datetime2
12:15:04.1234    1900-01-01 12:15:04.123


   1:  -- datetime2 -> time
   2:  DECLARE @datetime2 datetime2(4) = ‘12-10-25 12:32:10.1234‘;
   3:  DECLARE @time time(3) = @datetime2;
   4:   
   5:  SELECT @datetime2 AS ‘@datetime2‘, @time AS ‘@time(3)‘;

Result:

@datetime2                       @time(3)
2025-10-12 12:32:10.1234    12:32:10.123

time ? datetimeoffset


   1:  -- time -> datetimeoffset
   2:  DECLARE @time time(4) = ‘12:15:04.1234‘;
   3:  DECLARE @datetimeoffset datetimeoffset(3) = @time;
   4:   
   5:  SELECT @time AS ‘@time‘, @datetimeoffset AS ‘@datetimeoffset‘;

Result:

@time              @datetimeoffset
12:15:04.1234    1900-01-01 12:15:04.123 +00:00


   1:  -- datetimeoffset -> time
   2:  DECLARE @datetimeoffset datetimeoffset(4) = ‘12-10-25 12:32:10.1234 +01:0‘;
   3:  DECLARE @time time(3) = @datetimeoffset;
   4:   
   5:  SELECT @datetimeoffset AS ‘@datetimeoffset ‘, @time AS ‘@time‘;

Result:

@datetimeoffset                           @time
2025-10-12 12:32:10.1234 +01:00    12:32:10.123

date ? smalldatetime


   1:  -- date -> smalldatetime
   2:  DECLARE @date date= ‘1912-10-25‘;
   3:  DECLARE @smalldatetime smalldatetime = @date;
   4:   
   5:  SELECT @date AS ‘@date‘, @smalldatetime AS ‘@smalldatetime‘;

Result:

@date          @smalldatetime
1912-10-25    1912-10-25 00:00:00


   1:  -- smalldatetime -> date
   2:  DECLARE @smalldatetime smalldatetime = ‘13-12-1955 12:43:10‘;
   3:  DECLARE @date date = @smalldatetime
   4:   
   5:  SELECT @smalldatetime AS ‘@smalldatetime‘, @date AS ‘@date‘;

Result:

@smalldatetime        @date
1955-12-13 12:43:00    1955-12-13

date ? datetime


   1:  -- date -> datetime
   2:  DECLARE @date date= ‘12-10-25‘;
   3:  DECLARE @datetime datetime= @date;
   4:   
   5:  SELECT @date AS ‘@date‘, @datetime AS ‘@datetime‘;

Result:

@date           @datetime
2025-10-12    2025-10-12 00:00:00.000


   1:  -- datetime -> date
   2:  DECLARE @datetime datetime = ‘12-11-05 12:43:10‘;
   3:  DECLARE @date date = @datetime;
   4:   
   5:  SELECT @datetime AS ‘@datetime‘, @date AS ‘@date‘;

Result:

@datetime                        @date
2005-11-12 12:43:10.000    2005-11-12

date ? datetime2


   1:  -- date -> datetime2
   2:  DECLARE @date date = ‘25-10-2030‘
   3:  DECLARE @datetime2 datetime2(3) = @date;
   4:   
   5:  SELECT @date AS ‘@date‘, @datetime2 AS ‘@datetime2(3)‘;

Result:

@date           @datetime2(3)
2030-10-25    2030-10-25 00:00:00.000


   1:  -- datetime2 -> date
   2:  DECLARE @datetime2 datetime2(4) = ‘12-10-25 12:32:10.1234‘;
   3:  DECLARE @date date = @datetime2;
   4:   
   5:  SELECT @datetime2 AS ‘@datetime2‘, @date AS ‘@date‘;

Result:

@datetime2                        @date
2025-10-12 12:32:10.1234    2025-10-12

date ? datetimeoffset


   1:  -- date -> datetimeoffset
   2:  DECLARE @date date = ‘1912-10-25‘;
   3:  DECLARE @datetimeoffset datetimeoffset(3) = @date;
   4:   
   5:  SELECT @date AS ‘@date‘, @datetimeoffset AS ‘@datetimeoffset‘;

Result:

@date           @datetimeoffset
1912-10-25    1912-10-25 00:00:00.000 +00:00


   1:  -- datetimeoffset -> date
   2:  DECLARE @datetimeoffset datetimeoffset(4) = ‘12-10-25 12:32:10 +01:0‘;
   3:  DECLARE @date date= @datetimeoffset;
   4:   
   5:  SELECT @datetimeoffset AS ‘@datetimeoffset ‘, @date AS ‘@date‘;

Result:

@datetimeoffset                             @date
2025-10-12 12:32:10.0000 +01:00    2025-10-12

smalldatetime ? datetime

   1:  -- smalldatetime -> datetime
   2:  DECLARE @smalldatetime smalldatetime = ‘12-10-2040 12:43:10‘;
   3:  DECLARE @datetime datetime = @smalldatetime;
   4:   
   5:  SELECT @smalldatetime AS ‘@smalldatetime‘, @datetime AS ‘@datetime‘;

Result:

@smalldatetime          @datetime
2040-10-12 12:43:00    2040-10-12 12:43:00.000


   1:  -- datetime -> smalldatetime
   2:  DECLARE @datetime datetime = ‘12-11-05 12:43:10.123‘;
   3:  DECLARE @smalldatetime smalldatetime = @datetime;
   4:   
   5:  SELECT @datetime AS ‘datetime‘, @smalldatetime AS ‘@smalldatetime‘;

Result:

datetime                           @smalldatetime
2005-11-12 12:43:10.123    2005-11-12 12:43:00

smalldatetime ? datetime2


   1:  -- smalldatetime -> datetime2
   2:  DECLARE @smalldatetime smalldatetime = ‘12-06-2011 12:43:10‘;
   3:  DECLARE @datetime2 datetime2(4) = @smalldatetime;
   4:   
   5:  SELECT @smalldatetime AS ‘@smalldatetime‘, @datetime2 AS ‘@datetime2(4)‘;

Result:

@smalldatetime          @datetime2(4)
2011-06-12 12:43:00    2011-06-12 12:43:00.0000


   1:  -- datetime2 -> smalldatetime
   2:  DECLARE @datetime2 datetime2 = ‘12-10-25 12:32:10.1234567‘;
   3:  DECLARE @smalldatetime smalldatetime = @datetime2;
   4:   
   5:  SELECT @datetime2 AS ‘@datetime2‘, @smalldatetime AS ‘@smalldatetime‘;

Result:

@datetime2                             @smalldatetime
2025-10-12 12:32:10.1234567    2025-10-12 12:32:00

smalldatetime ? datetimeoffset


   1:  -- samlldatetime -> datetimeoffset
   2:  DECLARE @smalldatetime smalldatetime = ‘12-10-2013 12:43:10‘;
   3:  DECLARE @datetimeoffset datetimeoffset(4) = @smalldatetime;
   4:   
   5:  SELECT @smalldatetime AS ‘@smalldatetime‘, @datetimeoffset AS ‘@datetimeoffset(4)‘;

Result:

@smalldatetime          @datetimeoffset(4)
2013-10-12 12:43:00    2013-10-12 12:43:00.0000 +00:00


   1:  -- datetimeoffset -> smalldatetime
   2:  DECLARE @datetimeoffset datetimeoffset(3) = ‘1912-10-25 12:24:32 +10:0‘;
   3:  DECLARE @smalldatetime smalldatetime = @datetimeoffset;
   4:   
   5:  SELECT @datetimeoffset AS ‘@datetimeoffset‘, @smalldatetime AS ‘@smalldatetime‘;

Result:

@datetimeoffset                            @smalldatetime
1912-10-25 12:24:32.000 +10:00    1912-10-25 12:25:00

datetime ? datetime2


   1:  -- datetimes -> datetime2
   2:  DECLARE @datetime datetime = ‘10-10-2014 12:45:37.123‘;
   3:  DECLARE @datetime2 datetime2(4) = @datetime;
   4:   
   5:  SELECT @datetime AS ‘@datetime‘, @datetime2 AS ‘@datetime2‘;

Result:

@datetime                        @datetime2
2014-10-10 12:45:37.123    2014-10-10 12:45:37.1230


   1:  -- datetimes2 -> datetime
   2:  DECLARE @datetime2 datetime2(4) = ‘1968-10-23 12:45:37.1237‘;
   3:  DECLARE @datetime datetime = @datetime2;
   4:   
   5:  SELECT @datetime2 AS ‘@datetime2‘, @datetime AS ‘@datetime‘;

Result:

@datetime2                        @datetime
1968-10-23 12:45:37.1237    1968-10-23 12:45:37.123

datetime ? datetimeoffset


   1:  -- datetime -> datetimeoffset
   2:  DECLARE @datetime datetime = ‘12-10-25 12:32:10.123‘;
   3:  DECLARE @datetimeoffset datetimeoffset = @datetime;
   4:   
   5:  SELECT @datetime AS ‘@datetime ‘, @datetimeoffset AS ‘@datetimeoffset‘;

Result:

@datetime                        @datetimeoffset
2025-10-12 12:32:10.123    2025-10-12 12:32:10.1230000 +00:00


   1:  -- datetimeoffset -> datetime
   2:  DECLARE @datetimeoffset datetimeoffset(4) = ‘12-10-25 12:32:10.1234 +01:0‘;
   3:  DECLARE @datetime datetime = @datetimeoffset;
   4:   
   5:  SELECT @datetimeoffset AS ‘@datetimeoffset ‘, @datetime AS ‘@datetime‘;

Result:

@datetimeoffset                             @datetime
2025-10-12 12:32:10.1234 +01:00    2025-10-12 12:32:10.123

datetime2 ? datetimeoffset


   1:  -- datetime2 -> datetimeoffset
   2:  DECLARE @datetime2 datetime2(3) = ‘12-10-25 12:32:10.1234567‘;
   3:  DECLARE @datetimeoffset datetimeoffset(2) = @datetime2;
   4:   
   5:  SELECT @datetime2 AS ‘@datetime2‘, @datetimeoffset AS ‘@datetimeoffset(2)‘;

Result:

@datetime2                      @datetimeoffset(2)
2025-10-12 12:32:10.123    2025-10-12 12:32:10.12 +00:00

   1:  -- datetimeoffset -> datetime2
   2:  DECLARE @datetimeoffset datetimeoffset(4) = ‘1912-10-25 12:24:32.1234 +10:0‘;
   3:  DECLARE @datetime2 datetime2(3)= @datetimeoffset;
   4:   
   5:  SELECT @datetimeoffset AS ‘@datetimeoffset‘, @datetime2 AS ‘@datetime2‘;

Result:

@datetimeoffset                             @datetime2
1912-10-25 12:24:32.1234 +10:00    1912-10-25 12:24:32.123



在此谢谢读完这篇博客,有什么写的不对的地方请指正。
有帮助就推荐下,有感想就写下留言,不满意也写下留言,有问题及时更正。

时间: 2024-10-27 06:44:10

[数据库基础]——快速浏览日期时间转换的相关文章

在Mac中用快捷键快速插入日期时间

在Windows中有AutoHotkey这个好东东帮我快速插入日期时间,输入[mm然后敲空格就可以了.在Mac中就没有这么好的事了,虽然能够实现,但是没有AutoHotkey方便.还是要借助于AppleScript,但是并不能适用于Mac中任何文本输入的地方,比如DayOne的弹出窗口就不能用,另外还有一个限制:只能对选中的文字进行操作. 我使用的是这个文章里面的方案: http://www.sixhat.net/applescript-insert-date-and-time-into-you

ORACLE函数之日期时间转换函数

 1.          TO_CHAR 语法:TO_CHAR(X [,format]) 说明:将X按format格式转换成字符串.X是一个日期或者数字,format是一个规定了X采用何种格式转换的格式字符串 举例: SQL>SELECT TO_CHAR(sysdate,'YYYY-MM-DD hh24:mi:ss') A FROM DUAL; A ------------------- 2014-06-1815:58:43 2.            TO_DATE 语法:TO_DATE(

6、SQL基础整理(日期时间数据类型,转换函数)

日期时间数据类型 *系统常量: @@DATEFIRST(返回当前时间) DATEADD 增加时间 语法:DATEADD (datepart , number , date ) select DATEADD(YEAR,2,'2013-11-2') DATEDIFF 两个日期之间的距离 select DATEDIFF(YEAR,'2011-7-18','2014-11-2') DATENAME 返回某个时间值里面想要得到某块类型的数 select DATENAME(YEAR,'2011-7-8')

unix环境C编程之日期时间转换

1.理清概念 1.1.日历时间: 含义:国际标准时间1970年1月1日00:00:00以来经过的秒数. 数据类型:time_t.实际上是long的别名. 1.2.tm结构时间: 含义:结构化存放时间的数据结构,方便查看.ANSI C标准称使用tm结构的这种时间表示为分解时间(broken-down time) 数据类型: struct tm { int  tm_sec;       /* 秒数,范围[0,60] */ int  tm_min;       /* 分钟,范围[0,59] */ in

Python3基础知识之日期时间与字符的转换

问题:"猿类"们都知道,编程中都会涉及到日期.时间类型与字符串类型的转换.不同场景,需要将字符串格式转换为日期类型:也需要将日期类型转换为字符串格式. 目标: 学习和积累python中time和datetime 相关知识: 一.time  引用包time import time 1.获取CTS格式: 星期 月 日 时分秒 年 time.ctime()    结果:Wed May 16 15:12:57 2018 2.字符转换为tuple(time obj) date_string =

Sql 日期时间 转换

sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-02-01 08:02/*时间一般为getdate()函数或数据表里的字段*/ CONVERT(varchar(10), 时间一, 23) 结果:2007-02-01 /*varchar(10)表示日期输出的格式,如果不够长会发生截取*/ 语句及查询结果:Select CONVERT(varchar(

SQL时间戳日期时间转换

将时间戳转换为日期格式:比如降1455504268→2016-02-15 10:44:28 1 select device.register_time a,FROM_UNIXTIME(device.register_time,'%Y-%m-%d %H:%i:%s') as registerTime from tm_data_newdevice device MySQL的DATE_ADD() 函数向日期添加指定的时间间隔. 语法: DATE_ADD(date,INTERVAL expr type)

sqlserver convert 日期时间 转换格式化

Select CONVERT(varchar(100), GETDATE(), 120): 2006-05-16 10:57:49 Select CONVERT(varchar(100), GETDATE(), 23): 2006-05-16   Select CONVERT(varchar(100), GETDATE(), 112): 20060516 Select CONVERT(varchar(100), GETDATE(), 20): 2006-05-16 10:57:47 Select

Swift教程_基础技术_获取当前日期时间、日期时间格式化及转换

Swift教程_基础技术_类型转换(父子类转换:Int.Double.String转换) Swift教程_基础技术_获取当前日期时间.日期时间格式化及转换 1.获取当前日期时间 var nowDate = NSDate() var formatter = NSDateFormatter() formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" var dateString = formatter.stringFromDate(nowDate) pri