Reputation: 283
The Original SQL Statement is:
SELECT SA.[RequestStartDate] as 'Service Start Date',
SA.[RequestEndDate] as 'Service End Date',
FROM
(......)SA
WHERE......
The output date format is YYYY/MM/DD, but I want the output date format is DD/MM/YYYY. How can I modify in this statement?
Upvotes: 21
Views: 378809
Reputation: 1
It depends on the DBMS. Indeed, on Oracle SQL, you would rather write SELECT TO_CHAR(<column_date>, 'DD/MM/YYYY') AS NAME_YOUR_DATE. The solution SELECT CONVERT(VARCHAR(10), SA.[RequestStartDate], 103) does not work for this DBMS
Upvotes: 0
Reputation: 1
SELECT column1, DATE_FORMAT(ColumnName, "%d/%M/%Y") as column2, column3 FROM tableName;
you can see here
w3: https://www.w3schools.com/sql/func_mysql_date_format.asp
Upvotes: -1
Reputation: 11
I was using oracle and I had to select multiple columns and output the date column in YYYY-MM-DD format, and this worked
select <column_1>, to_char(<date_column>, 'YYYY-MM-DD') as <Alias_name> from <table_name>
Upvotes: -1
Reputation: 335
There's also another way to do this-
select TO_CHAR(SA.[RequestStartDate] , 'DD/MM/YYYY') as RequestStartDate from ... ;
Upvotes: 5
Reputation: 1008
Changed to:
SELECT FORMAT(SA.[RequestStartDate],'dd/MM/yyyy') as 'Service Start Date', SA.[RequestEndDate] as 'Service End Date', FROM (......)SA WHERE......
Have no idea which SQL engine you are using, for other SQL engine, CONVERT can be used in SELECT statement to change the format in the form you needed.
Upvotes: 14
Reputation: 5220
Try:
SELECT convert(nvarchar(10), SA.[RequestStartDate], 103) as 'Service Start Date',
convert(nvarchar(10), SA.[RequestEndDate], 103) as 'Service End Date',
FROM
(......)SA
WHERE......
Or:
SELECT format(SA.[RequestStartDate], 'dd/MM/yyyy') as 'Service Start Date',
format(SA.[RequestEndDate], 'dd/MM/yyyy') as 'Service End Date',
FROM
(......)SA
WHERE......
Upvotes: 1
Reputation: 1373
Try like this...
select CONVERT (varchar(10), getdate(), 103) AS [DD/MM/YYYY]
For more info : http://www.sql-server-helper.com/tips/date-formats.aspx
Upvotes: 30
Reputation: 429
You will want to use a CONVERT() statement.
Try the following;
SELECT CONVERT(VARCHAR(10), SA.[RequestStartDate], 103) as 'Service Start Date', CONVERT(VARCHAR(10), SA.[RequestEndDate], 103) as 'Service End Date', FROM (......) SA WHERE.....
See MSDN Cast and Convert for more information.
Upvotes: 0