Home Database Mysql Tutorial sqlserver中DATE类型的数据转化 CONVERT

sqlserver中DATE类型的数据转化 CONVERT

Jun 07, 2016 pm 03:39 PM
date sqlserver data type Convert

主要描述的是SQL Server使用convert取得datetime日期数据的实际操作流程,在实际操作中用SQL Server数据库中用convert来获取datetime日期数据,以下实例包含各种日期格式的转换。 语句及查询结果: SelectCONVERT(varchar(100),GETDATE(),0):0516200610:57AM

主要描述的是SQL Server使用convert取得datetime日期数据的实际操作流程,在实际操作中用SQL Server数据库中用convert来获取datetime日期数据,以下实例包含各种日期格式的转换。

 

语句及查询结果: 

  1. Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM  
  2. Select CONVERT(varchar(100), GETDATE(), 1): 05/16/0 

 SQL Server使用convert来取得datetime日期数据,以下实例包含各种日期格式的转换 

语句及查询结果: 

  1. Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM  
  2. Select CONVERT(varchar(100), GETDATE(), 1): 05/16/06  
  3. Select CONVERT(varchar(100), GETDATE(), 2): 06.05.16  
  4. Select CONVERT(varchar(100), GETDATE(), 3): 16/05/06  
  5. Select CONVERT(varchar(100), GETDATE(), 4): 16.05.06  
  6. Select CONVERT(varchar(100), GETDATE(), 5): 16-05-06  
  7. Select CONVERT(varchar(100), GETDATE(), 6): 16 05 06  
  8. Select CONVERT(varchar(100), GETDATE(), 7): 05 16, 06  
  9. Select CONVERT(varchar(100), GETDATE(), 8): 10:57:46  
  10. Select CONVERT(varchar(100), GETDATE(), 9): 05 16 2006 10:57:46:827AM  
  11. Select CONVERT(varchar(100), GETDATE(), 10): 05-16-06  
  12. Select CONVERT(varchar(100), GETDATE(), 11): 06/05/16  
  13. Select CONVERT(varchar(100), GETDATE(), 12): 060516  
  14. Select CONVERT(varchar(100), GETDATE(), 13): 16 05 2006 10:57:46:937  
  15. Select CONVERT(varchar(100), GETDATE(), 14): 10:57:46:967  
  16. Select CONVERT(varchar(100), GETDATE(), 20): 2006-05-16 10:57:47  
  17. Select CONVERT(varchar(100), GETDATE(), 21): 2006-05-16 10:57:47.157  
  18. Select CONVERT(varchar(100), GETDATE(), 22): 05/16/06 10:57:47 AM  
  19. Select CONVERT(varchar(100), GETDATE(), 23): 2006-05-16  
  20. Select CONVERT(varchar(100), GETDATE(), 24): 10:57:47  
  21. Select CONVERT(varchar(100), GETDATE(), 25): 2006-05-16 10:57:47.250  
  22. Select CONVERT(varchar(100), GETDATE(), 100): 05 16 2006 10:57AM  
  23. Select CONVERT(varchar(100), GETDATE(), 101): 05/16/2006  
  24. Select CONVERT(varchar(100), GETDATE(), 102): 2006.05.16  
  25. Select CONVERT(varchar(100), GETDATE(), 103): 16/05/2006  
  26. Select CONVERT(varchar(100), GETDATE(), 104): 16.05.2006  
  27. Select CONVERT(varchar(100), GETDATE(), 105): 16-05-2006  
  28. Select CONVERT(varchar(100), GETDATE(), 106): 16 05 2006  
  29. Select CONVERT(varchar(100), GETDATE(), 107): 05 16, 2006  
  30. Select CONVERT(varchar(100), GETDATE(), 108): 10:57:49  
  31. Select CONVERT(varchar(100), GETDATE(), 109): 05 16 2006 10:57:49:437AM  
  32. Select CONVERT(varchar(100), GETDATE(), 110): 05-16-2006  
  33. Select CONVERT(varchar(100), GETDATE(), 111): 2006/05/16  
  34. Select CONVERT(varchar(100), GETDATE(), 112): 20060516  
  35. Select CONVERT(varchar(100), GETDATE(), 113): 16 05 2006 10:57:49:513  
  36. Select CONVERT(varchar(100), GETDATE(), 114): 10:57:49:547  
  37. Select CONVERT(varchar(100), GETDATE(), 120): 2006-05-16 10:57:49  
  38. Select CONVERT(varchar(100), GETDATE(), 121): 2006-05-16 10:57:49.700  
  39. Select CONVERT(varchar(100), GETDATE(), 126): 2006-05-16T10:57:49.827  
  40. Select CONVERT(varchar(100), GETDATE(), 130): 18 ???? ?????? 1427 10:57:49:907AM  
  41. Select CONVERT(varchar(100), GETDATE(), 131): 18/04/1427 10:57:49:920AM 

说明:

SQL Server使用convert:

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

参数

expression

 

是任何有效的 Microsoft? SQL Server? 表达式。。

data_type

目标系统所提供的数据类型,包括 bigint 和 sql_variant。不能使用用户定义的数据类型。

length

 

nchar、nvarchar、char、varchar、binary 或 varbinary 数据类型的可选参数。

style

日期格式样式,借以将 datetime 或 smalldatetime 数据转换为字符数据(nchar、nvarchar、char、varchar、nchar 或 nvarchar 数据类型);或者字符串格式样式,借以将 float、real、money 或 smallmoney 数据转换为字符数据(nchar、nvarchar、char、varchar、nchar 或 nvarchar 数据类型)。

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* Hijri**** dd mon yyyy hh:mi:ss:mmmAM 

- 131* Hijri**** dd/mm/yy hh:mi:ss:mmmAM 

* 默认值(style 0 或 100、9 或 109、13 或 113、20 或 120、21 或 121)始终返回世纪数位 (yyyy)。

** 当转换为 datetime时输入;当转换为字符数据时输出。

 

*** 专门用于 XML。对于从 datetime或 smalldatetime 到 character 数据的转换,输出格式如表中所示。对于从 float、money 或 smallmoney 到 character 数据的转换,输出等同于 style 2。对于从 real 到 character 数据的转换,输出等同于 style 1。

 

****Hijri 是具有几种变化形式的日历系统,Microsoft? SQL Server? 2000 使用其中的科威特算法。

 

重要 默认情况下,SQL Server 根据截止年份 2049 解释两位数字的年份。即,两位数字的年份 49 被解释为 2049,而两位数字的年份 50 被解释为 1950。许多客户端应用程序(例如那些基于 OLE 自动化对象的客户端应用程序)都使用 2030 作为截止年份。

SQL Server 提供一个配置选项("两位数字的截止年份"),借以更改 SQL Server 所使用的截止年份并对日期进行一致性处理。然而最安全的办法是指定四位数字年份。

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

 

这里

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem that the object named already exists in the sqlserver database How to solve the problem that the object named already exists in the sqlserver database Apr 05, 2024 pm 09:42 PM

For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

How to import mdf file into sqlserver How to import mdf file into sqlserver Apr 08, 2024 am 11:41 AM

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

What to do if the sqlserver service cannot be started What to do if the sqlserver service cannot be started Apr 05, 2024 pm 10:00 PM

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

How to check sqlserver port number How to check sqlserver port number Apr 05, 2024 pm 09:57 PM

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

How to recover accidentally deleted database in sqlserver How to recover accidentally deleted database in sqlserver Apr 05, 2024 pm 10:39 PM

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks Apr 29, 2024 pm 06:55 PM

I cry to death. The world is madly building big models. The data on the Internet is not enough. It is not enough at all. The training model looks like "The Hunger Games", and AI researchers around the world are worrying about how to feed these data voracious eaters. This problem is particularly prominent in multi-modal tasks. At a time when nothing could be done, a start-up team from the Department of Renmin University of China used its own new model to become the first in China to make "model-generated data feed itself" a reality. Moreover, it is a two-pronged approach on the understanding side and the generation side. Both sides can generate high-quality, multi-modal new data and provide data feedback to the model itself. What is a model? Awaker 1.0, a large multi-modal model that just appeared on the Zhongguancun Forum. Who is the team? Sophon engine. Founded by Gao Yizhao, a doctoral student at Renmin University’s Hillhouse School of Artificial Intelligence.

How to delete sqlserver if the installation fails? How to delete sqlserver if the installation fails? Apr 05, 2024 pm 11:27 PM

If the SQL Server installation fails, you can clean it up by following these steps: Uninstall SQL Server Delete registry keys Delete files and folders Restart the computer

Slow Cellular Data Internet Speeds on iPhone: Fixes Slow Cellular Data Internet Speeds on iPhone: Fixes May 03, 2024 pm 09:01 PM

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

See all articles