Convert finance period into Date/Time

1.2k Views Asked by At

I am trying to convert finance period into datetime e.g. 201801 = 01/04/2018

e.g.;
201801 = 01/04/2018
201802 = 01/05/2018
201803 = 01/06/2018
201804 = 01/07/2018
201805 = 01/08/2018
201806 = 01/09/2018
201807 = 01/10/2018
201808 = 01/11/2018
201809 = 01/12/2018
201810 = 01/01/2019
201811 = 01/02/2019
201812 = 01/03/2019

Formula in tableau that works 
DATE(left(str([Period]),4) + "-" + right(str([Period]),2) + "-1")

I need it in SQL. tried using convert and Cast but keep getting syntax errors.

CONVERT(date,(left(GL_master.period,4)+ '-' +(right(GL_master.period,2)+ 
'-1')),

As per summary.

1

There are 1 best solutions below

0
Caius Jard On BEST ANSWER

Where I put '201801' you should put your column name - [Period] ?

select DATEADD(month, 3, CONVERT(date, CONCAT('201801', '01'), 112)) 
                                              ^^^^^^^^
                                              [Period] ?

We pad your "date" of 201801 out with another '01' to make it like a date format 112, convert it to a date and add 3 months after it's turned into a date.

 201801 -> 20180101 -> 01/04/2018

This method works for rolling over years too

 201812 -> 20181201 -> 01/03/2019