mysql将字符串转换为表名,将字符串/ varchar转换为MySQL表之间的日期

I have a large PHP project that I've been working on for my own business.

I have a MySQL table, with about 2600 entries, that I'd like to "fix" the dates in. My current table has 3 VARCHAR columns that contain string dates. I'd like to convert these, into a duplicate table, but change them to type DATE on the way.

Once I make the conversion and everything looks good, I will copy the new table to the old database, and only allow a date picker to enter those dates on a going-forward basis.

Most of the existing string dates are on the format mm-dd-yyyy, example: 05-03-2013. Some are mm/dd/yyyy, example: 05/03/2013, and a few are of the MONTH DAY, YEAR format, example: March 26th, 2013.

What I ultimately want is them to be of the yyyy-mm-dd format, example: 2013-05-02.

I think I can do this using PHP, passing each string date through strtotime() and then date() but was wondering if MySQL can do this directly.

解决方案

Have a look at STR_TO_DATE function:

UPDATE yourtable

SET

new_column = STR_TO_DATE(varchar_date, '%m-%d-%Y')

WHERE

STR_TO_DATE(varchar_date, '%m-%d-%Y') IS NOT NULL

And then the same query, with date format as '%m/%d/%Y', and then again with '%M %D, %Y'.

If the format doesn't match, STR_TO_DATE will return NULL and rows won't be updated.

If you wish to insert your existing data to a new table, you could use something like this:

INSERT INTO new_table (ID, col1, col2, new_column)

SELECT

ID, col1, col2,

COALESCE(STR_TO_DATE(varchar_date, '%m-%d-%Y'),

STR_TO_DATE(varchar_date, '%m/%d/%Y'),

STR_TO_DATE(varchar_date, '%M %D, %Y'))

FROM oldtable

(new_column is a date column, and you can then format how you want using PHP or using DATE_FORMAT)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将MySQL字符串转换日期,可以使用MySQL内置函数`str_to_date()`。该函数的语法如下: ``` str_to_date('日期字符串','日期格式') ``` 其中,'日期字符串'是要转换字符串,'日期格式'是指定字符串的格式,用于解析日期MySQL日期格式包括:%Y(4位年份),%y(2位年份),%m(月份,01到12)等。通过将字符串日期格式传递给`str_to_date()`函数,可以将字符串转换日期类型数据。 例如,在MySQL中,要将字符串'1981-12-03'转换日期类型,可以使用以下语句: ``` SELECT str_to_date('1981-12-03', '%Y-%m-%d') AS converted_date; ``` 这将返回一个日期类型的结果,字符串'1981-12-03'被转换日期。 需要注意的是,每个数据库处理日期的机制都不同,所以在实际开发中,使用日期类型或日期字符串日期会有所不同。在MySQL中,由于存在自动类型转换,即使将日期存储为VARCHAR类型的字符串,只要格式与MySQL默认的日期格式相同,也可以进行日期转换。但在其他数据库或应用程序中,可能需要更具体的日期转换方式。 总结: - 使用MySQL的`str_to_date()`函数可以将字符串转换日期类型。 - 需要指定字符串日期格式,以便正确解析日期。 - 不同数据库和应用程序可能有不同的日期处理机制,需根据具体情况选择日期示方式。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [MySQLQ中的字符串日期格式的转换(精讲)](https://blog.csdn.net/qq_30347133/article/details/83590185)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值