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

还有他们是不是用在时间的转换上比较多!

狐的传说
浏览 752回答 2
2回答

慕沐林林

CONVERT 与 CAST 都可以 用来做 字符 与 日期 类型之间的转换。但是一般来说, 用 CONVERT 比较好, 因为有一个 用于格式化的参数。你执行一下下面这个 SQL, 就能看出区别出来。SELECT 0 AS ID , Convert(VARCHAR(30), GETDATE(), 0 ) AS result UNION ALLSELECT 100 AS ID , Convert(VARCHAR(30), GETDATE(), 100) AS result UNION ALLSELECT 1 AS ID , Convert(VARCHAR(30), GETDATE(), 1 ) AS result UNION ALLSELECT 101 AS ID , Convert(VARCHAR(30), GETDATE(), 101) AS result UNION ALLSELECT 2 AS ID , Convert(VARCHAR(30), GETDATE(), 2 ) AS result UNION ALLSELECT 102 AS ID , Convert(VARCHAR(30), GETDATE(), 102) AS result UNION ALLSELECT 2 AS ID , Convert(VARCHAR(30), GETDATE(), 2 ) AS result UNION ALLSELECT 102 AS ID , Convert(VARCHAR(30), GETDATE(), 102) AS result UNION ALLSELECT 3 AS ID , Convert(VARCHAR(30), GETDATE(), 3 ) AS result UNION ALLSELECT 103 AS ID , Convert(VARCHAR(30), GETDATE(), 103) AS result UNION ALLSELECT 4 AS ID , Convert(VARCHAR(30), GETDATE(), 4 ) AS result UNION ALLSELECT 104 AS ID , Convert(VARCHAR(30), GETDATE(), 104) AS result UNION ALLSELECT 5 AS ID , Convert(VARCHAR(30), GETDATE(), 5 ) AS result UNION ALLSELECT 105 AS ID , Convert(VARCHAR(30), GETDATE(), 105) AS result UNION ALLSELECT 6 AS ID , Convert(VARCHAR(30), GETDATE(), 6 ) AS result UNION ALLSELECT 106 AS ID , Convert(VARCHAR(30), GETDATE(), 106) AS result UNION ALLSELECT 7 AS ID , Convert(VARCHAR(30), GETDATE(), 7 ) AS result UNION ALLSELECT 107 AS ID , Convert(VARCHAR(30), GETDATE(), 107) AS result UNION ALLSELECT 8 AS ID , Convert(VARCHAR(30), GETDATE(), 8 ) AS result UNION ALLSELECT 108 AS ID , Convert(VARCHAR(30), GETDATE(), 108) AS result UNION ALLSELECT 9 AS ID , Convert(VARCHAR(30), GETDATE(), 9 ) AS result UNION ALLSELECT 109 AS ID , Convert(VARCHAR(30), GETDATE(), 109) AS result UNION ALLSELECT 10 AS ID , Convert(VARCHAR(30), GETDATE(), 10 ) AS result UNION ALLSELECT 110 AS ID , Convert(VARCHAR(30), GETDATE(), 110) AS result UNION ALLSELECT 11 AS ID , Convert(VARCHAR(30), GETDATE(), 11 ) AS result UNION ALLSELECT 111 AS ID , Convert(VARCHAR(30), GETDATE(), 111) AS result UNION ALLSELECT 12 AS ID , Convert(VARCHAR(30), GETDATE(), 12 ) AS result UNION ALLSELECT 112 AS ID , Convert(VARCHAR(30), GETDATE(), 112) AS result UNION ALLSELECT 13 AS ID , Convert(VARCHAR(30), GETDATE(), 13 ) AS result UNION ALLSELECT 113 AS ID , Convert(VARCHAR(30), GETDATE(), 113) AS result UNION ALLSELECT 14 AS ID , Convert(VARCHAR(30), GETDATE(), 14 ) AS result UNION ALLSELECT 114 AS ID , Convert(VARCHAR(30), GETDATE(), 114) AS result UNION ALLSELECT 20 AS ID , Convert(VARCHAR(30), GETDATE(), 20 ) AS result UNION ALLSELECT 120 AS ID , Convert(VARCHAR(30), GETDATE(), 120) AS result UNION ALLSELECT 21 AS ID , Convert(VARCHAR(30), GETDATE(), 21 ) AS result UNION ALLSELECT 121 AS ID , Convert(VARCHAR(30), GETDATE(), 121) AS result
打开App,查看更多内容
随时随地看视频慕课网APP