Heim >Datenbank >MySQL-Tutorial >SqlServer中使用CONVERT函数实现日期格式化

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

WBOY
WBOYOriginal
2016-06-07 15:51:321346Durchsuche

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:SQLServer2008 R2完全卸载方法Nächster Artikel:sqlserver 当IDENTITY