Maison > base de données > tutoriel mysql > SqlServer中使用CONVERT函数实现日期格式化

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

WBOY
Libérer: 2016-06-07 15:51:32
original
1345 Les gens l'ont consulté

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>
Copier après la connexion

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

注意:不要应用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>
Copier après la connexion

希望这篇POST对您有用.

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

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal