I recently had to deal with something similar and thought I’d share how I approached it — I’m still learning SQL, so I used dbForge Studio for SQL Server to help me figure it out visually.
My original date looked like 'JAN-01-2025'
, and I needed to convert it into yyyymmdd
format (like 20250101
). Since that format isn’t directly supported, I ended up doing two things:
Replaced the hyphens with spaces, because style 107 (which parses dates like "Jan 01 2025") needs that.
Then I used TRY_CONVERT
to safely turn the string into a proper DATE
.
And finally, I formatted it as char(8)
using style 112 to get the yyyymmdd
.
SELECT
OriginalValue = val,
ConvertedDate = CONVERT(char(8), TRY_CONVERT(date, REPLACE(val, '-', ' '), 107), 112)
FROM (VALUES ('JAN-01-2025'), ('FEB-30-2025')) AS v(val);