How to convert getdate()
to the format dd/mm/yyyy
while keeping the datatype date
and not varchar
? This is what I tried
SELECT cast(CONVERT(varchar(12), GETDATE(), 103) as date)
but it is wrong.
How to convert getdate()
to the format dd/mm/yyyy
while keeping the datatype date
and not varchar
? This is what I tried
SELECT cast(CONVERT(varchar(12), GETDATE(), 103) as date)
but it is wrong.
The date
datatype doesn't have a concept of a format. It's just a moment in time perhaps with a timezone attached but without any particular fixed representation. When you format it, it becomes a varchar
. If you cast the varchar
back to a date
, it loses the format. This will be pretty much the same in any programming language that has a native date datatype (or date/datetime objects). It's not specific to SQL.
If you want a particular string representation of a date, then you want a varchar
, so don't try to cast it back to a date
.
This seems to be what's called an XY problem. What is wrong with CONVERT(varchar(12), GETDATE(), 103)
? Yes, it's a varchar
, but what's why are you unsatisfied with it being a varchar
?