SQL中CAST和CONVERT用在时间转换上的区别(我新手,越详细越好)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL中CAST和CONVERT用在时间转换上的区别(我新手,越详细越好)相关的知识,希望对你有一定的参考价值。
还有他们是不是用在时间的转换上比较多!不要到网上复制粘贴(谢谢合作)BS不劳而获的,没有满意答案,会关闭问题有满意答案会追加200分给你
参考技术A cast 和convert都是用来转化数据类型的,功能上是差不多的。表达式:CAST ( expression AS data_type ) ,
CONVERT (data_type[(length)], expression [, style]),
在时间转化中一般用到convert,因为它比cast多加了一个style,可以转化成不同时间的,打个简单的例子吧,比方说查找一下当前的时间,select getdate(),而我们只需要到年月日而已,这时我们就可以把它转化为select convert(varchar(100),GETDATE(),111)转化成我们需要的,而select CAST(GETDATE() as varchar(30))cast就没办法或者是转化过程比较繁琐,convert时间后面的style值不同产生的时间格式也不一样,具体可参看网站http://www.w3school.com.cn/sql/func_convert.asp本回答被提问者采纳 参考技术B 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 参考技术C
参考技术D
以上是关于SQL中CAST和CONVERT用在时间转换上的区别(我新手,越详细越好)的主要内容,如果未能解决你的问题,请参考以下文章
SQL SERVER中强制类型转换cast和convert的区别
SQL SERVER中强制类型转换cast和convert的区别
SQL SERVER中强制类型转换cast和convert的区别