Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date conversion issue.: yyyy/MM/dd to dd/MM/yyyy

I have a date column where the date format is

2010-04-14

in SQL Server. Is there any possible way to retrieve the date format as

14/04/2010

in a select statement?

like image 892
chamara Avatar asked Sep 03 '25 14:09

chamara


2 Answers

Try this:

SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY]

Source Link

Addendum 1:

As described in my comment, you need to convert your field to a DATETIME type before the above will work. This example should work on your SQL Server:

SELECT CONVERT(VARCHAR(10), CAST('2010-04-14' AS DATETIME), 103) AS [DD/MM/YYYY]

If you get this exception:

Msg 242, Level 16, State 3, Line 1 The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

Then you need to change your date field to be in YYYY-MM-DD Format, EG: 2010-14-04

OR

Add the following line before the select statement:

SET DATEFORMAT MDY -- Input dates are in the MM/DD/YYYY format, change to DMY to handle UK dates

Example:

SET DATEFORMAT MDY;

SELECT CONVERT(VARCHAR(10), CAST('2010-04-14' AS DATETIME), 103) AS [DD/MM/YYYY]
like image 177
Stuart Blackler Avatar answered Sep 05 '25 03:09

Stuart Blackler


Another way to solve your problem:

select substring(columnName, 9, 2) + '/' + substring(columnName, 6, 2)  + '/' + substring(columnName, 1, 4)
like image 24
aF. Avatar answered Sep 05 '25 04:09

aF.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!