Check an example:
declare @example int = 20220203 -- 2 March 2022
select
@example / 10000 as [year],
datefromparts(@example / 10000, @example % 100, @example % 10000 / 100) as [date]
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi Expert,
how to extract year from yyddmm
20220202
and convert format to date like 02-0202022
Check an example:
declare @example int = 20220203 -- 2 March 2022
select
@example / 10000 as [year],
datefromparts(@example / 10000, @example % 100, @example % 10000 / 100) as [date]
Hi @Shambhu Rai
What is the type of '20220202' in the example, int, char or something else?
I looked for it in this official document and couldn't find the date format of yyddmm.
Extract the year: If it is an int type, you only need to divide by 10000 as Viorel demonstrated. If it is a char type, you can use the LEFT() function to truncate the year.
convert format to date like 02-0202022
Which date format is this, can you explain further?
Best regards,
Percy Tang
If the answer is the right solution, please click "Accept Answer". If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Convert it to varchar and then to date:
select convert(date, convert(varchar(8), 20220202))