Home > Database > Mysql Tutorial > SqlServer中使用CONVERT函数实现日期格式化

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

WBOY
Release: 2016-06-07 15:51:32
Original
1343 people have browsed it

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>
Copy after login

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

注意:不要应用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>
Copy after login

希望这篇POST对您有用.

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template