changing format date MySql

后端 未结 4 577
南旧
南旧 2020-12-11 21:04

i have date type column in MySql. by default MySql date format is YYYY/MM/DD. but actualy i prefer to use DD/MM/YYYY format.

Could i change the column date f

4条回答
  •  时光说笑
    2020-12-11 21:38

    As others have explained that it is not possible, but here's alternative solution, it requires a little tuning, but it works like date column.

    I started to think, how I could make formatting possible. I got an idea. What about making trigger for it? I mean, adding column with type char, and then updating that column using a MySQL trigger. And that worked! I made some research related to triggers, and finally come up with these queries:

    CREATE TRIGGER timestampper BEFORE INSERT ON table
    FOR EACH
    ROW SET NEW.timestamp = DATE_FORMAT(NOW(), '%d/%m/%Y');
    
    CREATE TRIGGER timestampper2 BEFORE UPDATE ON table
    FOR EACH
    ROW SET NEW.timestamp = DATE_FORMAT(NOW(), '%d/%m/%Y');
    

    You can't use TIMESTAMP or DATE as a column type, because these have their own format, and they update automatically.

    So, here's your alternative timestamp or date alternative! Hope this helped, at least I'm glad that I got this working.

提交回复
热议问题