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
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

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 Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor