CAST和CONVERT



<div class="blogpost-body" id="cnblogs_post_body"><p>  CAST和CONVERT都经常被使用。特别提取出来作为一篇文章,方便查找。</p>
<p>  CAST、CONVERT都可以执行数据类型转换。在大部分情况下,两者执行同样的功能,<span style="color: rgb(255, 0, 0);">不同的是CONVERT还提供一些特别的日期格式转换,而CAST没有这个功能。</span></p>
<p><span style="color: rgb(255, 0, 0);">  <span style="color: rgb(0, 0, 0);">既然CONVERT包括了CAST的所有功能,而且CONVERT还能进行日期转换,那么为什么需要使用CAST呢?实际上,这是为了ANSI/ISO兼容。CAST是ANSI兼容的,而CONVERT则不是。<br></span></span></p>
<p><span style="color: rgb(0, 0, 0);">  语法:</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgb(255, 0, 255);">  CAST</span> (expression <span style="color: rgb(0, 0, 255);">AS</span> data_type <span style="color: rgb(255, 0, 0);">[</span><span style="color: rgb(255, 0, 0);"> (length ) </span><span style="color: rgb(255, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">)

  </span><span style="color: rgb(255, 0, 255);">CONVERT</span> (data_type <span style="color: rgb(255, 0, 0);">[</span><span style="color: rgb(255, 0, 0);"> ( length ) </span><span style="color: rgb(255, 0, 0);">]</span> , expression <span style="color: rgb(255, 0, 0);">[</span><span style="color: rgb(255, 0, 0);"> , style </span><span style="color: rgb(255, 0, 0);">]</span>)</pre>
</div>
<p>&nbsp;</p>
<p>  示例:</p>
<div class="cnblogs_code">
<pre><span style="color: rgb(0, 0, 255);">  SELECT</span> <span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">AB</span><span style="color: rgb(255, 0, 0);">'</span> <span style="color: rgb(128, 128, 128);">+</span> <span style="color: rgb(128, 0, 0); font-weight: bold;">1</span>    <span style="color: rgb(0, 128, 0);">--此语句报错,在将 varchar 值 'AB' 转换成数据类型 int 时失败。</span>

  <span style="color: rgb(0, 0, 255);">SELECT</span> <span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">AB</span><span style="color: rgb(255, 0, 0);">'</span> <span style="color: rgb(128, 128, 128);">+</span> <span style="color: rgb(255, 0, 255);">CAST</span>(<span style="color: rgb(128, 0, 0); font-weight: bold;">1</span> <span style="color: rgb(0, 0, 255);">AS</span> <span style="color: rgb(0, 0, 255);">varchar</span>)    <span style="color: rgb(0, 128, 0);">--输出 AB1</span>

  <span style="color: rgb(0, 0, 255);">SELECT</span> <span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">AB</span><span style="color: rgb(255, 0, 0);">'</span> <span style="color: rgb(128, 128, 128);">+</span> <span style="color: rgb(255, 0, 255);">CONVERT</span>(<span style="color: rgb(0, 0, 255);">varchar</span>,<span style="color: rgb(128, 0, 0); font-weight: bold;">1</span>)    <span style="color: rgb(0, 128, 0);">--输出 AB1</span></pre>
</div>
<p>  CAST和CONVERT都能转换时间:</p>
<div class="cnblogs_code">
<pre><span style="color: rgb(0, 0, 255);">  SELECT</span> <span style="color: rgb(255, 0, 255);">CONVERT</span>(<span style="color: rgb(0, 0, 255);">DateTime</span>,<span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">2011-07-11</span><span style="color: rgb(255, 0, 0);">'</span>)    <span style="color: rgb(0, 128, 0);">--输出 2011-07-11 00:00:00.000</span>

  <span style="color: rgb(0, 0, 255);">SELECT</span> <span style="color: rgb(255, 0, 255);">CAST</span>(<span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">2011-07-11</span><span style="color: rgb(255, 0, 0);">'</span> <span style="color: rgb(0, 0, 255);">AS</span> <span style="color: rgb(0, 0, 255);">DateTime</span>)    <span style="color: rgb(0, 128, 0);">--输出 2011-07-11 00:00:00.000</span></pre>
</div>
<p>  但是时间转字符串,CAST没有CONVERT这么多花样:</p>
<div class="cnblogs_code">
<pre><span style="color: rgb(0, 0, 255);">  SELECT</span> <span style="color: rgb(255, 0, 255);">CONVERT</span>(<span style="color: rgb(0, 0, 255);">varchar</span>,<span style="color: rgb(255, 0, 255);">GETDATE</span>(),<span style="color: rgb(128, 0, 0); font-weight: bold;">5</span>)    <span style="color: rgb(0, 128, 0);">--输出 01-07-13</span>
  <span style="color: rgb(0, 0, 255);">SELECT</span> <span style="color: rgb(255, 0, 255);">CONVERT</span>(<span style="color: rgb(0, 0, 255);">varchar</span>,<span style="color: rgb(255, 0, 255);">GETDATE</span>(),<span style="color: rgb(128, 0, 0); font-weight: bold;">111</span>)  <span style="color: rgb(0, 128, 0);">--输出 2013/07/01</span>
  <span style="color: rgb(0, 0, 255);">SELECT</span> <span style="color: rgb(255, 0, 255);">CONVERT</span>(<span style="color: rgb(0, 0, 255);">varchar</span>,<span style="color: rgb(255, 0, 255);">GETDATE</span>(),<span style="color: rgb(128, 0, 0); font-weight: bold;">1</span>)    <span style="color: rgb(0, 128, 0);">--输出 07/01/13</span>

  <span style="color: rgb(0, 0, 255);">SELECT</span> <span style="color: rgb(255, 0, 255);">CAST</span>(<span style="color: rgb(255, 0, 255);">GETDATE</span>() <span style="color: rgb(0, 0, 255);">AS</span> <span style="color: rgb(0, 0, 255);">varchar</span>)    <span style="color: rgb(0, 128, 0);">--输出 07 1 2013 9:56PM</span></pre>
</div>
<p>&nbsp;</p></div>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值