首页 >数据库 >mysql教程 >SqlServer中使用CONVERT函数实现日期格式化

SqlServer中使用CONVERT函数实现日期格式化

WBOY
WBOY原创
2016-06-07 15:51:321350浏览

CONVERT函数有3个参数: CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) 第一个参数返回数据类型,第二个参数是表达式,第三个是可选的定义风格的参数. 注意:不要应用CONVERT函数在被索引的日期列上. 看下面的实例: SELECT CONVERT ( VARCHAR

CONVERT函数有3个参数:

<span>CONVERT </span><span>( </span><span>data_type [ ( length ) ] </span><span>, </span><span>expression [ , style ] </span><span>)</span>

第一个参数返回数据类型,第二个参数是表达式,第三个是可选的定义风格的参数.

注意:不要应用CONVERT函数在被索引的日期列上.

看下面的实例:

<span>SELECT </span><span>CONVERT</span><span>(</span><span>VARCHAR</span><span>(</span>30<span>),</span><span>GETDATE</span><span>(),</span>100<span>)
</span><span>--Output (MON DD YYYY HH:MIAMPM)
--Jul 7 2009 2:19PM

</span><span>SELECT </span><span>CONVERT</span><span>(</span><span>VARCHAR</span><span>(</span>30<span>),</span><span>GETDATE</span><span>(),</span>101<span>)
</span><span>--Output (MM/DD/YYYY)
--07/07/2009

</span><span>SELECT </span><span>CONVERT</span><span>(</span><span>VARCHAR</span><span>(</span>30<span>),</span><span>GETDATE</span><span>(),</span>102<span>)
</span><span>--Output (YYYY.MM.DD)
--2009.07.07

</span><span>SELECT </span><span>CONVERT</span><span>(</span><span>VARCHAR</span><span>(</span>30<span>),</span><span>GETDATE</span><span>(),</span>103<span>)
</span><span>--Output (DD/MM/YYYY)
--06/07/2009

</span><span>SELECT </span><span>CONVERT</span><span>(</span><span>VARCHAR</span><span>(</span>30<span>),</span><span>GETDATE</span><span>(),</span>104<span>)
</span><span>--Output (DD.MM.YYYY)
--06.07.2009

</span><span>SELECT </span><span>CONVERT</span><span>(</span><span>VARCHAR</span><span>(</span>30<span>),</span><span>GETDATE</span><span>(),</span>105<span>)
</span><span>--Output (DD-MM-YYYY)
--06-07-2009

</span><span>SELECT </span><span>CONVERT</span><span>(</span><span>VARCHAR</span><span>(</span>30<span>),</span><span>GETDATE</span><span>(),</span>106<span>)
</span><span>--Output (DD MON YYYY)
--06 Jul 2009

</span><span>SELECT </span><span>CONVERT</span><span>(</span><span>VARCHAR</span><span>(</span>30<span>),</span><span>GETDATE</span><span>(),</span>107<span>)
</span><span>--Output (MON DD,YYYY)
--Jul 06, 2009


</span><span>SELECT </span><span>CONVERT</span><span>(</span><span>VARCHAR</span><span>(</span>30<span>),</span><span>GETDATE</span><span>(),</span>108<span>)
</span><span>--Output (HH24:MI:SS)
--14:24:20

</span><span>SELECT </span><span>CONVERT</span><span>(</span><span>VARCHAR</span><span>(</span>30<span>),</span><span>GETDATE</span><span>(),</span>109<span>)
</span><span>--Output (MON DD YYYY HH:MI:SS:NNN AMPM)
--Jul 7 2009 2:24:35:490PM

</span><span>SELECT </span><span>CONVERT</span><span>(</span><span>VARCHAR</span><span>(</span>30<span>),</span><span>GETDATE</span><span>(),</span>110<span>)
</span><span>--Output (MM-DD-YYYY)
-- 07-07-2009

</span><span>SELECT </span><span>CONVERT</span><span>(</span><span>VARCHAR</span><span>(</span>30<span>),</span><span>GETDATE</span><span>(),</span>111<span>)
</span><span>--Output (YYYY/MM/DD)
--2009/07/07

</span><span>SELECT </span><span>CONVERT</span><span>(</span><span>VARCHAR</span><span>(</span>30<span>),</span><span>GETDATE</span><span>(),</span>112<span>)
</span><span>--Output (YYYYMMDD)
--20090707

</span><span>SELECT </span><span>CONVERT</span><span>(</span><span>VARCHAR</span><span>(</span>30<span>),</span><span>GETDATE</span><span>(),</span>113<span>)
</span><span>--Output (MON DD YYYY HH24:MI:SS:NNN)
--07 Jul 2009 14:26:24:617

</span><span>SELECT </span><span>CONVERT</span><span>(</span><span>VARCHAR</span><span>(</span>30<span>),</span><span>GETDATE</span><span>(),</span>114<span>)
</span><span>--Output (HH24:MI:SS:NNN)
--14:26:48:953
</span>

希望这篇POST对您有用.

Author: Petter Liu  http://wintersun.cnblogs.com

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn