简体   繁体   中英

How do I change date format in SQL SELECT?

I want to change the date format in my SQL calling, but I got the wrong output how to solve this?

SELECT s.id, format(s.TransacDate,'dddd, d MMMM, yyyy') AS formatDate, s.name
FROM sales s
JOIN user u
ON s.No = u.No
WHERE u.logId= 'abcde';

OUTPUT DATE THAT I GET FROM THE QUERY

This is how my DB store the format date 2020-10-21 09:25:10

I want to achieve the result of 21/10/2020 09:25:10

How do I fix my current query in order to achieve the result?

Seems that "format" is the MSSQL function. For MySQL, try date_format :

date_format(s.TransacDate, '%d/%m/%Y %H:%i:%s')

This will be correct answer.

SELECT s.id, format(s.TransacDate.getdate(),'dd/MM/yyyy,hh:mm:ss') AS formatDate, s.name
FROM sales s                             //  --------------------
JOIN user u                               //           ^--------------------- change here
ON s.No = u.No
WHERE u.logId= 'abcde';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM