sql server - Concatenate 2 x fields to make a Date -
i have 2 x fields t1.period , t1.year both have data type smallint using sql management studio 2014 how may concatenate them , return result date type?
also, t1.period has values 1 12 how may pad out 01 12 ... or changing date type sort out?
much appreciated!
sample data ...
period yr 1 2015 2 2009 12 2009 11 2010 10 2011
result ... date 01/01/2015 01/02/2009 01/12/2009 01/11/2010 01/10/2011
thanks!
looks terrible struggling lists - sorry :(
converting values old fashioned way
assuming t1.period
value represents month, consider converting values strings , converting concatenated result date via cast()
function :
select cast(cast(t1.year varchar) + '-' + cast(t1.period varchar) + '-1' date)
this build string looks {year}-{month}-1
, parsed date
, should give first date of given month/year.
using datefromparts()
function
sql server 2012 , above support datefromparts()
function allow pass in various parts (year, month, day) build corresponding date
object, easier read syntax :
select datefromparts(t1.year,t1.period,1)
additionally, if needed datetime
object, use datetimefromparts()
expected.
Comments
Post a Comment