duatree
duatree

Reputation: 413

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?

Upvotes: 1

Views: 89

Answers (2)

Greg Schmidt
Greg Schmidt

Reputation: 5099

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

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

Upvotes: 2

top talent
top talent

Reputation: 599

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';

Upvotes: 0

Related Questions