[英]How to get month and year from date in SQL
I need help to get month and year from a date which is in yyyymmdd format.我需要帮助从 yyyymmdd 格式的日期中获取月份和年份。
The dates look like this, '20150102', the output should be Jan 2015.日期看起来像这样,“20150102”,输出应该是 2015 年 1 月。
I want the output as on single value, currently using datepart function Im getting output in 2 different columns for month and year我希望输出为单个值,目前使用 datepart 函数我在月和年的 2 个不同列中获取输出
This may help:这可能有帮助:
SQL Server: SQL 服务器:
SELECT FORMAT (GETDATE(), 'MMM yyyy') -- Jul 2019
SELECT FORMAT (GETDATE(), 'MMMM yyyy') -- July 2019
SELECT RIGHT(CONVERT(VARCHAR(11), GETDATE(), 106), 8) -- Jul 2019
For more details: https://www.tutorialgateway.org/sql-date-format/更多详情: https : //www.tutorialgateway.org/sql-date-format/
MySQL: MySQL:
SELECT DATE_FORMAT("20150102", "%M %Y"); -- January 2015
SELECT DATE_FORMAT("20150102", "%b %Y"); -- Jan 2015
SELECT YEAR(date) AS 'year', MONTH(date) AS 'month'
For more details: http://www.sqlines.com/mysql-to-oracle/date_format更多详情: http : //www.sqlines.com/mysql-to-oracle/date_format
MySQL 会将日期字符串识别为日期,因此您可以使用date_format()
来获取您想要的格式:
select date_format('20150102', '%b %Y')
有一个所有不同的 sql 日期格式的列表sql 日期格式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.