search
HomeDatabaseMysql Tutorialsql convert函数使用小结

sql convert函数使用方法小结整理的比较全了,需要的朋友可以参考下。

CONVERT(data_type,expression[,style])
convert(varchar(10),字段名,转换格式)

说明:
此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,char,varchar)
相互转换的时候才用到.

语句 结果
SELECT CONVERT(varchar(100), GETDATE(), 0) 07 15 2009 4:06PM
SELECT CONVERT(varchar(100), GETDATE(), 1) 07/15/09
SELECT CONVERT(varchar(100), GETDATE(), 2) 09.07.15
SELECT CONVERT(varchar(100), GETDATE(), 3) 15/07/09
SELECT CONVERT(varchar(100), GETDATE(), 4) 15.07.09
SELECT CONVERT(varchar(100), GETDATE(), 5) 15-07-09
SELECT CONVERT(varchar(100), GETDATE(), 6) 15 07 09
SELECT CONVERT(varchar(100), GETDATE(), 7) 07 15, 09
SELECT CONVERT(varchar(100), GETDATE(), 8) 16:06:26
SELECT CONVERT(varchar(100), GETDATE(), 9) 07 15 2009 4:06:26:513PM
SELECT CONVERT(varchar(100), GETDATE(), 10) 07-15-09
SELECT CONVERT(varchar(100), GETDATE(), 11) 09/07/15
SELECT CONVERT(varchar(100), GETDATE(), 12) 090715
SELECT CONVERT(varchar(100), GETDATE(), 13) 15 07 2009 16:06:26:513
SELECT CONVERT(varchar(100), GETDATE(), 14) 16:06:26:513
SELECT CONVERT(varchar(100), GETDATE(), 20) 2009-07-15 16:06:26
SELECT CONVERT(varchar(100), GETDATE(), 21) 2009-07-15 16:06:26.513
SELECT CONVERT(varchar(100), GETDATE(), 22) 07/15/09 4:06:26 PM
SELECT CONVERT(varchar(100), GETDATE(), 23) 2009-07-15
SELECT CONVERT(varchar(100), GETDATE(), 24) 16:06:26
SELECT CONVERT(varchar(100), GETDATE(), 25) 2009-07-15 16:06:26.513
SELECT CONVERT(varchar(100), GETDATE(), 100) 07 15 2009 4:06PM
SELECT CONVERT(varchar(100), GETDATE(), 101) 07/15/2009
SELECT CONVERT(varchar(100), GETDATE(), 102) 2009.07.15
SELECT CONVERT(varchar(100), GETDATE(), 103) 15/07/2009
SELECT CONVERT(varchar(100), GETDATE(), 104) 15.07.2009
SELECT CONVERT(varchar(100), GETDATE(), 105) 15-07-2009
SELECT CONVERT(varchar(100), GETDATE(), 106) 15 07 2009
SELECT CONVERT(varchar(100), GETDATE(), 107) 07 15, 2009
SELECT CONVERT(varchar(100), GETDATE(), 108) 16:06:26
SELECT CONVERT(varchar(100), GETDATE(), 109) 07 15 2009 4:06:26:513PM
SELECT CONVERT(varchar(100), GETDATE(), 110) 07-15-2009
SELECT CONVERT(varchar(100), GETDATE(), 111) 2009/07/15
SELECT CONVERT(varchar(100), GETDATE(), 112) 20090715
SELECT CONVERT(varchar(100), GETDATE(), 113) 15 07 2009 16:06:26:513
SELECT CONVERT(varchar(100), GETDATE(), 114) 16:06:26:513
SELECT CONVERT(varchar(100), GETDATE(), 120) 2009-07-15 16:06:26
SELECT CONVERT(varchar(100), GETDATE(), 121) 2009-07-15 16:06:26.513
SELECT CONVERT(varchar(100), GETDATE(), 126) 2009-07-15T16:06:26.513
SELECT CONVERT(varchar(100), GETDATE(), 130) 23 ??? 1430 4:06:26:513PM
SELECT CONVERT(varchar(100), GETDATE(), 131) 23/07/1430 4:06:26:513PM


------------------------------------------------------------------------------------------------------------
3 | 103 英法 | dd/mm/yy
------------------------------------------------------------------------------------------------------------
4 | 104 德国 | 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:mmmmAM(或PM)
------------------------------------------------------------------------------------------------------------
10 | 110 美国 | mm-dd-yy
------------------------------------------------------------------------------------------------------------
11 | 111 日本 | yy/mm/dd
------------------------------------------------------------------------------------------------------------
12 | 112 ISO | yymmdd
------------------------------------------------------------------------------------------------------------
13 | 113 欧洲默认值 | dd mon yyyy hh:mi:ss:mmm(24小时制)
------------------------------------------------------------------------------------------------------------
14 | 114 | hh:mi:ss:mmm(24小时制)
------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------
21 | 121 | yyyy-mm-dd hh:mi:ss:mmm(24小时制)
------------------------------------------------------------------------------------------------------------

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

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

-------------------------------------------------------------------------------------------------------------------------------------------

select getdate()

例如:向日期加上2天
select dateadd(day,2,'2004-10-15') --返回:2004-10-17 00:00:00.000

select datediff(day,'2004-09-01','2004-09-18') --返回:17

SELECT DATEPART(month, '2004-10-15') --返回 10


SELECT datename(weekday, '2004-10-15') --返回:星期五

--可以与datepart对照一下

DateDiff (interval,date1,date2)

年 1753 ~ 9999
Quarter 季 1 ~ 4
Month 月1 ~ 12
Day of year 一年的日数,一年中的第几日 1-366
Day 日,1-31
Weekday 一周的日数,一周中的第几日 1-7
Week 周,一年中的第几周 0 ~ 51
Hour 时0 ~ 23
Minute 分钟0 ~ 59
Second 秒 0 ~ 59
Millisecond - 毫秒 0 ~ 999

假如你的第一天设置不一样,你可能需要调整这些例子,使它和不同的第一天设置相符合。你

下一个函数是DATEADD,增加当前日期到“1900-01-01”的月数。通过增加预定义的日期“1900-01-01”和当前日期的月数,我们可以获得这个月的第一天。另外,计算出来的日期的时间部分将会是“00:00:00.000”。


Statement
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
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools