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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -