SQL date_format()函数
时间:2020-02-23 14:32:25 来源:igfitidea点击:
在本文中,我们将重点关注一个有趣的主题-SQL date_format()函数。
SQL由许多内置函数组成,用于处理日期和时间值并对其进行操作。
这样的函数之一就是date_format()函数。
所以,让我们开始吧!
什么是SQL date_format()函数?
" SQL date_format()函数"使数据库管理员能够以多种形式格式化日期时间值并表示相同的格式。
在此,格式指定了可以向用户显示日期的不同形式。
因此,现在让我们在接下来的部分中关注date_format()函数的结构。
SQL date_format()函数的语法
看看下面的语法!
date_format(date, format);
date_format()函数采用两个值作为输入参数
- 日期
- 格式:我们要显示日期值的方式或者形式。
现在,让我们看一下表示日期的各种格式
| Format | Description |
|---|---|
| %a | Weekdays [Sun...Sat] |
| %b | Months [Jan..Dec] |
| %c | Months in numeric format [0..12] |
| %D | Days of the month [0th, 1st, 2nd, 3rd, etc] |
| %d | Days of the month in numeric format [00..31] |
| %e | Days of the month in numeric format [0..31] |
| %f | Microseconds |
| %H | Hour value in 24 hr-format (00..23) |
| %h | Hour value in 12 hr-format (01..12) |
| %I | Hour value (01..12) |
| %i | Minutes value in a numeric form (00..59) |
| %j | Days (001..366) |
| %k | Hour values (0..23) |
| %l | Hour values (1..12) |
| %M | Names of the month (January..December) |
| %m | Months in numeric format (00..12) |
| %p | AM/PM |
| %r | Time in a 12 hr-format |
| %S | Seconds value (00..59) |
| %s | Seconds value (00..59) |
| %T | Time in a 24-hour |
| %U | Week values (00-53), Sunday being the first day of the week |
| %u | Week values (00-53), Monday being the first day of the week |
| %V | Week values (01..53), Sunday being the first day of the week |
| %v | Week (01..53), Monday being the first day of the week |
| %W | Weekdays names |
| %w | Days of the week |
| %X | Year for the particular week, Sunday being the first day of the week |
| %x | Year for the particular week, Monday being the first day of the week |
| %Y | Year value in 4 digit numeric format |
| %y | Year value in 2 digit numeric format |
| %% | “%” character |
date_format()函数的示例
在下面的示例中,我们对使用SQL NOW()函数获得的当前日期值进行了格式化,如下所示:Date-Month(以字为单位)-Year(YYYY)
Select DATE_FORMAT(NOW(),'%D-%M-%Y')
输出:
DATE_FORMAT(NOW(),'%D-%M-%Y') 29th-August-2017
现在,我们以数字格式显示了当前日期值,如下所示
Select DATE_FORMAT(NOW(),'%d-%m-%y')
输出:
DATE_FORMAT(NOW(),'%d-%m-%y') 29-08-20
在此示例中,我们格式化并显示了星期几以及日期和月份,如下所示:
Select DATE_FORMAT(NOW(),'%W:%D-%M')
输出:
DATE_FORMAT(NOW(),'%W:%D-%M') Saturday:29th-August
下面,我们以以下格式显示了当前系统的日期值
小时:分钟–月,年(YYYY)
Select DATE_FORMAT(NOW(),'%H:%i--%b,%Y')
输出:
DATE_FORMAT(NOW(),'%H:%i--%b,%Y') 17:00--Aug,2017

