为了账号安全,请及时绑定邮箱和手机立即绑定

请问在SQL中CAST和CONVERT用在时间转换上的区别?求具体介绍

请问在SQL中CAST和CONVERT用在时间转换上的区别?求具体介绍

狐的传说 2021-06-23 15:15:42
还有他们是不是用在时间的转换上比较多!
查看完整描述

2 回答

?
慕沐林林

TA贡献2016条经验 获得超9个赞

CONVERT 与 CAST 都可以 用来做 字符 与 日期 类型之间的转换。
但是一般来说, 用 CONVERT 比较好, 因为有一个 用于格式化的参数。

你执行一下下面这个 SQL, 就能看出区别出来。

SELECT 0 AS ID , Convert(VARCHAR(30), GETDATE(), 0 ) AS result UNION ALL
SELECT 100 AS ID , Convert(VARCHAR(30), GETDATE(), 100) AS result UNION ALL
SELECT 1 AS ID , Convert(VARCHAR(30), GETDATE(), 1 ) AS result UNION ALL
SELECT 101 AS ID , Convert(VARCHAR(30), GETDATE(), 101) AS result UNION ALL
SELECT 2 AS ID , Convert(VARCHAR(30), GETDATE(), 2 ) AS result UNION ALL
SELECT 102 AS ID , Convert(VARCHAR(30), GETDATE(), 102) AS result UNION ALL
SELECT 2 AS ID , Convert(VARCHAR(30), GETDATE(), 2 ) AS result UNION ALL
SELECT 102 AS ID , Convert(VARCHAR(30), GETDATE(), 102) AS result UNION ALL
SELECT 3 AS ID , Convert(VARCHAR(30), GETDATE(), 3 ) AS result UNION ALL
SELECT 103 AS ID , Convert(VARCHAR(30), GETDATE(), 103) AS result UNION ALL
SELECT 4 AS ID , Convert(VARCHAR(30), GETDATE(), 4 ) AS result UNION ALL
SELECT 104 AS ID , Convert(VARCHAR(30), GETDATE(), 104) AS result UNION ALL
SELECT 5 AS ID , Convert(VARCHAR(30), GETDATE(), 5 ) AS result UNION ALL
SELECT 105 AS ID , Convert(VARCHAR(30), GETDATE(), 105) AS result UNION ALL
SELECT 6 AS ID , Convert(VARCHAR(30), GETDATE(), 6 ) AS result UNION ALL
SELECT 106 AS ID , Convert(VARCHAR(30), GETDATE(), 106) AS result UNION ALL
SELECT 7 AS ID , Convert(VARCHAR(30), GETDATE(), 7 ) AS result UNION ALL
SELECT 107 AS ID , Convert(VARCHAR(30), GETDATE(), 107) AS result UNION ALL
SELECT 8 AS ID , Convert(VARCHAR(30), GETDATE(), 8 ) AS result UNION ALL
SELECT 108 AS ID , Convert(VARCHAR(30), GETDATE(), 108) AS result UNION ALL
SELECT 9 AS ID , Convert(VARCHAR(30), GETDATE(), 9 ) AS result UNION ALL
SELECT 109 AS ID , Convert(VARCHAR(30), GETDATE(), 109) AS result UNION ALL
SELECT 10 AS ID , Convert(VARCHAR(30), GETDATE(), 10 ) AS result UNION ALL
SELECT 110 AS ID , Convert(VARCHAR(30), GETDATE(), 110) AS result UNION ALL
SELECT 11 AS ID , Convert(VARCHAR(30), GETDATE(), 11 ) AS result UNION ALL
SELECT 111 AS ID , Convert(VARCHAR(30), GETDATE(), 111) AS result UNION ALL
SELECT 12 AS ID , Convert(VARCHAR(30), GETDATE(), 12 ) AS result UNION ALL
SELECT 112 AS ID , Convert(VARCHAR(30), GETDATE(), 112) AS result UNION ALL
SELECT 13 AS ID , Convert(VARCHAR(30), GETDATE(), 13 ) AS result UNION ALL
SELECT 113 AS ID , Convert(VARCHAR(30), GETDATE(), 113) AS result UNION ALL
SELECT 14 AS ID , Convert(VARCHAR(30), GETDATE(), 14 ) AS result UNION ALL
SELECT 114 AS ID , Convert(VARCHAR(30), GETDATE(), 114) AS result UNION ALL
SELECT 20 AS ID , Convert(VARCHAR(30), GETDATE(), 20 ) AS result UNION ALL
SELECT 120 AS ID , Convert(VARCHAR(30), GETDATE(), 120) AS result UNION ALL
SELECT 21 AS ID , Convert(VARCHAR(30), GETDATE(), 21 ) AS result UNION ALL
SELECT 121 AS ID , Convert(VARCHAR(30), GETDATE(), 121) AS result



查看完整回答
反对 回复 2021-06-28
  • 2 回答
  • 0 关注
  • 752 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信