惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Privacy International News Feed
T
Threatpost
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
NISL@THU
NISL@THU
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
Know Your Adversary
Know Your Adversary
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
Intezer
博客园 - Franky
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
The Hacker News
The Hacker News
K
Kaspersky official blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tailwind CSS Blog
Project Zero
Project Zero
T
Tor Project blog
B
Blog RSS Feed
Recorded Future
Recorded Future
Scott Helme
Scott Helme
美团技术团队
V
V2EX
V
Visual Studio Blog
L
Lohrmann on Cybersecurity
P
Proofpoint News Feed
D
DataBreaches.Net
The Register - Security
The Register - Security
M
MIT News - Artificial intelligence
L
LangChain Blog
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园_首页
P
Privacy & Cybersecurity Law Blog

博客园 - 小强小工

DataGrid设置中文列名 WinForm窗体生命周期 Oracle格式化函数 ORACLE时间间隔函数 浅谈数据库设计技巧(转) Oracle与SQLServer返回datatable列名大小写 oracle、sqlserver数据库排序空值null问题解决办法 .NET开发资源站点和.NET开源项目收集 C#中ToString格式大全 - 小强小工 - 博客园 oracle 事务 相关 Oracle SGA 各种排序方法的综合比较 .NET中三种数据类型转换的区别:(type), type.Parse, Convert类 从追MM谈Java的23种设计模式 WinCE概念扫盲 将WinCE5.0模拟器连接到VS2005 .net的事件与委托(转载) VB有关handles 的问题 VB中的handles在C#中怎么写?
MS SQL Server中的CONVERT日期格式化大全
小强小工 · 2009-02-11 · via 博客园 - 小强小工

Posted on 2009-02-11 15:01  小强小工  阅读(8811)  评论()    收藏  举报

ONVERT
将某种数据类型的表达式显式转换为另一种数据类型。由于某些需求经常用到取日期格式的不同.现以下可在
SQL Server中 将日期格式化.

SQL Server 支持使用科威特算法的阿拉伯样式中的数据格式。

在表中,左侧的两列表示将 datetime或 smalldatetime 转换为字符数据的style 值。给 style 值加 100,可获得包括世纪数位的四位年份 (yyyy)。

不带世纪数位 (yy)带世纪数位 (yyyy)
标准

输入/输出**
- 0 或 100 (*) 默认值 mon dd yyyy hh:miAM(或 PM)
1 101 美国 mm/dd/yyyy
2 102 ANSI yy.mm.dd
3 103 英国/法国 dd/mm/yy
4 104 德国 dd.mm.yy
5 105 意大利 dd-mm-yy
6 106 - dd mon yy
7 107 - mon dd, yy
8 108 - hh:mm:ss
- 9 或 109 (*) 默认值 + 毫秒 mon dd yyyy hh:mi:ss:mmmAM(或 PM)
10 110 美国 mm-dd-yy
11 111 日本 yy/mm/dd
12 112 ISO yymmdd
- 13 或 113 (*) 欧洲默认值 + 毫秒 dd mon yyyy hh:mm:ss:mmm(24h)
14 114 - hh:mi:ss:mmm(24h)
- 20 或 120 (*) ODBC 规范 yyyy-mm-dd hh:mm:ss[.fff]
- 21 或 121 (*) ODBC 规范(带毫秒) yyyy-mm-dd hh:mm:ss[.fff]
- 126(***) ISO8601 yyyy-mm-dd Thh:mm:ss:mmm(不含空格)
- 130* 科威特 dd mon yyyy hh:mi:ss:mmmAM
- 131* 科威特 dd/mm/yy hh:mi:ss:mmmAM

*    默认值(style 0 或 100、9 或 109、13 或 113、20 或 120、21 或 121)始终返回世纪数位 (yyyy)。
** 当转换为 datetime 时输入;当转换为字符数据时输出。
*** 专门用于 XML。对于从 datetime 或 smalldatetime 到 character 数据的转换,输出格式如表中所示。对于从 floatmoney 或 smallmoney 到 character 数据的转换,输出等同于 style 2。对于从 real 到 character 数据的转换,输出等同于 style 1。

重要  默认情况下,SQL Server 根据截止年份 2049 解释两位数字的年份。即,两位数字的年份 49 被解释为 2049,而两位数字的年份 50 被解释为 1950。许多客户端应用程序(例如那些基于 OLE 自动化对象的客户端应用程序)都使用 2030 作为截止年份。SQL Server 提供一个配置选项("两位数字的截止年份"),借以更改 SQL Server 所使用的截止年份并对日期进行一致性处理。然而最安全的办法是指定四位数字年份。

当从 smalldatetime 转换为字符数据时,包含秒或毫秒的样式将在这些位置上显示零。当从datetime 或 smalldatetime 值进行转换时,可以通过使用适当的 char 或 varchar 数据类型长度来截断不需要的日期部分。

下表显示了从 float 或 real 转换为字符数据时的 style 值。

输出
0(默认值) 最大为 6 位数。根据需要使用科学记数法。
1 始终为 8 位值。始终使用科学记数法。
2 始终为 16 位值。始终使用科学记数法。

在下表中,左列表示从 money  smallmoney 转换为字符数据时的 style 值。

输出
0(默认值) 小数点左侧每三位数字之间不以逗号分隔,小数点右侧取两位数,例如 4235.98。
1 小数点左侧每三位数字之间以逗号分隔,小数点右侧取两位数,例如 3,510.92。
2 小数点左侧每三位数字之间不以逗号分隔,小数点右侧取四位数,例如 4235.9819。

使用 CONVERT:

CONVERT (data_type[(length)], expression [, style])


select CONVERT(varchar, getdate(), 120 ) 
2004-09-12 11:06:08

select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),\'-\',\'\'),\' \',\'\'),\':\',\'\') 
20040912110608

select CONVERT(varchar(12) , getdate(), 111 ) 
2004/09/12

select CONVERT(varchar(12) , getdate(), 112 ) 
20040912

select CONVERT(varchar(12) , getdate(), 102 ) 
2004.09.12

select CONVERT(varchar(12) , getdate(), 101 ) 
09/12/2004

select CONVERT(varchar(12) , getdate(), 103 ) 
12/09/2004

select CONVERT(varchar(12) , getdate(), 104 ) 
12.09.2004

select CONVERT(varchar(12) , getdate(), 105 ) 
12-09-2004

select CONVERT(varchar(12) , getdate(), 106 ) 
12 09 2004

select CONVERT(varchar(12) , getdate(), 107 ) 
09 12, 2004

select CONVERT(varchar(12) , getdate(), 108 ) 
11:06:08

select CONVERT(varchar(12) , getdate(), 109 ) 
09 12 2004 1

select CONVERT(varchar(12) , getdate(), 110 ) 
09-12-2004

select CONVERT(varchar(12) , getdate(), 113 ) 
12 09 2004 1

select CONVERT(varchar(12) , getdate(), 114 ) 
11:06:08.177