SQL Server Change Date Format(DD/MM/YYYY) in SQL SELECT Statement

f0brbegy  于 2023-05-28  发布在  其他
关注(0)|答案(7)|浏览(153)

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?

nkoocmlb

nkoocmlb1#

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

uz75evzq

uz75evzq2#

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.

wh6knrhe

wh6knrhe3#

There's also another way to do this-

select TO_CHAR(SA.[RequestStartDate] , 'DD/MM/YYYY') as RequestStartDate from ... ;
px9o7tmv

px9o7tmv4#

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......
pvcm50d1

pvcm50d15#

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.

pvcm50d1

pvcm50d16#

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>

nimxete2

nimxete27#

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

相关问题