Home Database Mysql Tutorial sql中CONVERT日期时间格式化

sql中CONVERT日期时间格式化

Jun 07, 2016 pm 05:49 PM
convert time formatting

在mysql用来对字符型的时间日期格式化我们常用CONVERT()函数来操作,下面我来给各位朋友详细介绍CONVERT()函数的使用方法,大家可参考。

格式:
CONVERT(data_type,expression[,style])

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

例子:

 代码如下 复制代码
SELECT CONVERT(varchar(30),getdate(),101) now

结果为:

now

|09/15/2001

 代码如下 复制代码

Select CONVERT(varchar(100), GETDATE(), 0)  --05 16 2006 10:57AM
Select CONVERT(varchar(100), GETDATE(), 1)  --05/16/06
Select CONVERT(varchar(100), GETDATE(), 2)  --06.05.16
Select CONVERT(varchar(100), GETDATE(), 3)  --16/05/06
Select CONVERT(varchar(100), GETDATE(), 4)  --16.05.06
Select CONVERT(varchar(100), GETDATE(), 5)  --16-05-06
Select CONVERT(varchar(100), GETDATE(), 6)  --16 05 06
Select CONVERT(varchar(100), GETDATE(), 7)  --05 16, 06
Select CONVERT(varchar(100), GETDATE(), 8)  --10:57:46
Select CONVERT(varchar(100), GETDATE(), 9)  --05 16 2006 10:57:46:827AM
Select CONVERT(varchar(100), GETDATE(), 10)  --05-16-06
Select CONVERT(varchar(100), GETDATE(), 11)  --06/05/16
Select CONVERT(varchar(100), GETDATE(), 12)  --060516
Select CONVERT(varchar(100), GETDATE(), 13)  --16 05 2006 10:57:46:937
Select CONVERT(varchar(100), GETDATE(), 14)  --10:57:46:967


style数字在转换时间时的含义如下:

Style(2位表示年份) Style(4位表示年份) 输入输出格式
0 100 mon dd yyyy hh:miAM(或PM)
1 101 mm/dd/yyyy
2 102 yyyy-mm-dd
3 103 dd/mm/yyyy
4 104 dd-mm-yyyy
5 105 dd-mm-yyyy
6 106 dd mon yyyy
7 107 mon dd,yyyy
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 yymmdd
13 113 dd mon yyyy hh:mi:ss:mmm(24小时制)
14 114 hh:mi:ss:mmm(24小时制)
20 120 yyyy-mm-dd hh:mi:ss(24小时制)
21 121 yyyy-mm-dd hh:mi:ss:mmm(24小时制)

 

 

 

 

 

 

 

 

 

 

 

 

注意了convert与cast的区别了,这里简单说明一下

convert一般用于值,小数之间转换;
cast一般用于小数转数值和字符型

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
4 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)

Python program to format time in AM-PM format Python program to format time in AM-PM format Aug 28, 2023 pm 05:29 PM

In Python, we have some built-in time functions such as strftime() and datetime.now() which can be used to find the time in AM/PM format. Time in AM/PM format is used in a variety of applications such as user interfaces, reporting and documentation, data visualization, and event scheduling. When the time is between 11:59:00 midnight and 12 noon, we say AM time. Similarly, we can say that the time between 12 o'clock and 11:59:00 midnight is PM. The abbreviations AM/PM are used to indicate the exact time. Syntax uses the following syntax in the example &miinus;strftime('%I:%M:%S%p')strft

What software is convert? What software is convert? Dec 09, 2020 am 09:33 AM

convert is not a software, but a file system modification command in Windows. Convert converts the file allocation table FAT and FAT32 volumes to the NTFS file system while leaving the existing files and folders intact. Its syntax is "convert [Volume] /fs:ntfs [/v] [/cvtarea:FileName] [/nosecurity] [/x]".

Detailed explanation of the implementation steps of converting American time to Chinese time in PHP Detailed explanation of the implementation steps of converting American time to Chinese time in PHP Mar 27, 2024 pm 06:39 PM

PHP is a commonly used programming language used to develop web applications. In the process of developing web applications, it may involve time conversion in different time zones, such as converting American time to Chinese time. This article will detail the steps on how to use PHP to convert American time to Chinese time and provide code examples. 1. Get the US time First, we need to get the US time. The time zone can be set using PHP's built-in function date_default_timezone_set

How to use convert command How to use convert command Oct 25, 2023 pm 02:55 PM

The convert command is a command line tool in the ImageMagick image processing software package, used for image format conversion, image processing, and image synthesis. Common usage: 1. Simple image format conversion: convert input.jpg output.png; 2. Adjust image size: convert input.jpg -resize 800x600 output.jpg; 3. Image cropping, etc.

How to solve Java time formatting exception (DateTimeFormatException) How to solve Java time formatting exception (DateTimeFormatException) Aug 26, 2023 pm 04:30 PM

How to solve Java time format exception (DateTimeFormatException) Introduction: Java is a widely used programming language, and format exceptions (DateTimeFormatException) are often encountered when processing dates and times. This article will explain how to resolve time formatting exceptions in Java and provide some code examples. 1. What is time formatting exception (DateTimeFormatException) in Java

How to format time into a specific string using TIME_FORMAT function in MySQL How to format time into a specific string using TIME_FORMAT function in MySQL Jul 13, 2023 pm 03:55 PM

How to use the TIME_FORMAT function in MySQL to format time into a specific string MySQL is a widely used relational database management system that provides a wealth of functions and operators to process data. In MySQL, there is a very useful function, the TIME_FORMAT function, which can format the time in a specified format and return a string. The basic syntax of the TIME_FORMAT function is as follows: TIME_FORMAT(time,f

How to use the convert conversion function How to use the convert conversion function Nov 16, 2023 pm 01:09 PM

The usage is to understand the data type and target data type to be converted, and call the corresponding conversion function to achieve the conversion.

How to deal with date and time in PHP? How to deal with date and time in PHP? May 20, 2023 pm 08:51 PM

In web development, date and time are very important factors, especially for interaction and data storage. In PHP, the functions for processing dates and times are very powerful, such as getting the current time, converting timestamps into date and time format, comparing two dates and times, and so on. In this article, we will introduce how to handle date and time in PHP. Get the current time In PHP, the function to get the current time is date(). This function takes two parameters, the first parameter is the datetime format and the second parameter is an optional timestamp. by

See all articles