Convert Date format into DD/MMM/YYYY format in SQL Server
You can swiftly transform a date to DD/MMM/YYYY
format using this SQL command:
Here, replace your_column
with your actual date column.
Stage I: Date Conversion Using "CONVERT"
Your journey to transform date format in SQL Server begins by using the CONVERT
function, which is the workhorse for date transformations. Here's your to-go command:
Note: Style 106
is no random magic number. It's the code we use to get the date string as "DD mon YYYY".
Stage II: Swapping Spaces with Slashes
You might've noticed that the result of your conversion is 07 Apr 2023
, which doesn't fit the requirement of DD/MMM/YYYY
. Time to call in REPLACE()
for a character switch:
That's more like it. Now the date is in the desired DD/MMM/YYYY
format.
Getting Date Fancy: Post SQL Server 2012 Era
If you're sailing the high seas of SQL Server 2012 or later, things get fancier with the FORMAT()
function:
P.S: Remember, FORMAT()
is an aristocrat, might be slow in handling large data.
Language and Culture Settings: The Unsuspected Villains
Our handy CONVERT
or FORMAT
could play tricks if the server's language settings have a life of their own. To ensure dates don't get personality disorders, align language settings with your expected cultural formats:
Just like ordering tea in England, now CONVERT
caters date in "DD mon YYYY" form in English.
The Legacy Combat: SQL Server 2005 era
Getting tangled in SQL Server 2005, with no FORMAT
function at your mercy? Time to get your hands dirty with CLR UDFs (common language runtime user-defined functions):
Consider this a tip from your friendly neighborhood SQL Spiderman.
Selecting the Right Wardrobe: Date Pattern
Whether you don the FORMAT()
suit or go classic with CONVERT()
, always remember to choose the correct pattern fitting the occasion: your SQL Server version.
Pay Attention to the Time
What's the time Mr. Wolf? If your data includes time components too, no worries. SQL Server can format datetime as niftily as date:
And just like that, your date has its watch set too!
Deeper Knowledge Dive
For SQL enthusiasts wanting more, check the References section. It's a mini database of its own on date formatting.
Was this article helpful?