How to convert DateTime to VarChar
Quickly transform DateTime
into VarChar
using the mighty CONVERT()
function:
Or, tailor your formats with a stroke of FORMAT()
in SQL Server 2012+:
Replace our fancy YourDateTimeColumn
placeholder with your actual date-time column.
If your taste is simply date, no time:
And, if you just want to be a Scrooge and omit time:
Digging into the Convert function
Let's shovel into CONVERT()
and uncover some hidden gems:
-
Outfit your conversion with
style 23
for a clean, classic look: -
Don a monocle and peruse Microsoft's Docs for a comprehensive style guide:
-
For a minimalist result, use
NVARCHAR(11)
to skip time:
Avoiding slipping on banana peels
A few footings to beware on this path:
-
Watch your step with potential landmines known as invalid styles:
-
Don't forget that your choice of conversion can stretch or squish your data type lengths:
Precision is the key to SQL domination
Let's refine our SQL craftmanship:
Picking the right format
Don't just order random formats off the menu:
-
Feast on the globally acclaimed
ISO
standard: -
Savour the local flavours with styles
130
and131
:
Trimming time like an SQL barber
Keep just the date and shave off the time:
-
Get buzz cuts with
LEFT
trim: -
Craft precision formats with limited lengths:
Style experimentation lab
Try on different style numbers like changing rooms:
Was this article helpful?