r/SQL • u/PrinceFlorfian • Apr 17 '26
SQL Server Help Converting Date to a Different Date Style while Querying in SSMS.
Hey everybody! I just had what I think would be a quick question. I'm new to using Sequel Server Management Studio at work. I'm currently trying to build a query from existing databases, but I'm having trouble trying to figure out how to convert a date to another date style in T-SQL. I have two dates that I need to convert from the format "yyyymmdd" to "yyyy/mm/dd'. One I already converted by joining data to our date dimension table, but I can't do the same join to convert the other. How can I convert the date to the desired format in the same query?
7
u/VladDBA SQL Server DBA Apr 17 '26 edited Apr 17 '26
you mean something like this?
The output will be 2026/04/17
The inner TRY_CONVERT converts 20260417 to an actual date (2026-04-17), the outer TRY_CONVERT converts that date to a string that looks like your desired date format.
Edited to add relevant link to MS Learn article: https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver17#date-and-time-styles
In the inner TRY_CONVERT, 112 = the input style that matches the "yyyymmdd" format of the input value
In the outer TRY_CONVERT, 111 = the output style (because you're converting from a date to a string) that matches your desired output format of "yyyy/mm/dd"
2
u/Latentius Apr 17 '26 edited Apr 18 '26
I'd turn them both into actual DATE types instead of formatted strings. Reformatting might be sufficient for your purpose, but having appropriate data types makes the data easier to work with in the long run.
Edit: Fixed phone's auto-miscorrect.
1
u/mu_SQL Apr 17 '26
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver17
-3
u/SoggyGrayDuck Apr 17 '26
This is the perfect task for AI. I don't write substring or conversions anymore. I just toss it to AI and often find new ways of doing things. And then have to research to make sure it's actually more efficient lol
2
u/[deleted] Apr 17 '26
[removed] — view removed comment