![](/img/trans.png)
[英]How to change a VARCHAR value with two digit in a column (eg: 0214 month year) to a date format in MySQL?
[英]How to fetch date, month and year from column with varchar data type?
我有varchar数据类型的日期列。 如何获取日,月和年?
例如:“ 29/01/2015”是SQL数据库中的varchar类型。
如果您的值始终采用这种格式,则只需使用substring_index()
:
select substring_index(col, '/', 1) as day,
substring_index(substring_index(col, '/', 2), '/', -1) as month,
substring_index(col, '/', -1) as year
但是,您应该使用本地日期/时间格式将日期存储在数据库中。 它使对它们的操作变得更加容易。
您可以尝试以下方法:
SELECT SUBSTRING(d,1,2) AS day,
SUBSTRING(d,4,2) AS month,
SUBSTRING(d,7,4) as year
FROM t;
该查询以字符串形式返回日,月和年。
select to_char(to_date('29/01/2015','DD/MM/YYYY'), 'DD') Day,
to_char(to_date('29/01/2015','DD/MM/YYYY'), 'MM') MonthNum,
to_char(to_date('29/01/2015','DD/MM/YYYY'), 'YYYY') Year,
to_char(to_date('29/01/2015','DD/MM/YYYY'), 'MON') MonthChar,
to_char(to_date('29/01/2015','DD/MM/YYYY'), 'Dy') Weekday,
to_char(to_date('29/01/2015','DD/MM/YYYY'), 'DDD') JulianDay
from dual;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.