>  기사  >  데이터 베이스  >  SqlServer中使用CONVERT函数实现日期格式化

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

WBOY
WBOY원래의
2016-06-07 15:51:321301검색

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으로 문의하세요.