Deepmath
asked on
Month Year concatenate
Hi,
My table contains only the Year and the MONTH. I want to get a new column StartDate in Month\Year Format. I am using derived column trans in SSIS.
I am stuck with the condition that i need to specify.
Year Month New Column StartDate
---- ------ ------------
2004 1 01\2004
2004 2 02\2004
2004 3 03\2004
My table contains only the Year and the MONTH. I want to get a new column StartDate in Month\Year Format. I am using derived column trans in SSIS.
I am stuck with the condition that i need to specify.
Year Month New Column StartDate
---- ------ ------------
2004 1 01\2004
2004 2 02\2004
2004 3 03\2004
Use T-SQL transformation in the middle to involve this query:
CAST(Year as char(4)) + RIGHT('00' + CAST(Month as varchar(2)), 2)
Above T-SQL Transformation should help you out.
CAST(Year as char(4)) + RIGHT('00' + CAST(Month as varchar(2)), 2)
Above T-SQL Transformation should help you out.
Angel Eye's is correct - http:#24738174, just has a type-o that you can easily fix by removing the extra parenthesis behind year. Please accept his, just wanted to help.
RIGHT('0' + CAST([month] as varchar(2)) + '\' + cast([year] as varchar(4)), 7)
ASKER
I just realised that i need the day too.....so the format i need is 01\01\2007 ,02\01\2008
by default the day is the First dy of the month..Thank you
by default the day is the First dy of the month..Thank you
Just amend Angel Eye's solution with a static/literal '01\'.
RIGHT('0' + CAST([month] as varchar(2)) + '\01\' + cast([year] as varchar(4)), 7)
Sorry, you need to change the 7 at the end to 10.
RIGHT('0' + CAST([month] as varchar(2)) + '\01\' + cast([year] as varchar(4)), 10)
ASKER
RIGHT('0' + CAST([month] as varchar(2)) + '\01\' + cast([year] as varchar(4)), 10).. I used this and i got the following errors
Error at Data Flow Task [Derived Column [16]]: Parsing the expression " RIGHT('0' + CAST([month] as varchar(2)) + '\01\' + cast([year] as varchar(4)), 10)" failed. The single quotation mark at line number "1", character number "16", was not expected.
Error at Data Flow Task [Derived Column [16]]: Cannot parse the expression " RIGHT('0' + CAST([month] as varchar(2)) + '\01\' + cast([year] as varchar(4)), 10)". The expression was not valid, or there is an out-of-memory error.
Error at Data Flow Task [Derived Column [16]]: The expression " RIGHT('0' + CAST([month] as varchar(2)) + '\01\' + cast([year] as varchar(4)), 10)" on "output column "ReportDate" (145)" is not valid.
See if it is the \ needing to be escaped.
RIGHT('0' + CAST([month] as varchar(2)) + '\\01\\' + cast([year] as varchar(4)), 10)
Or try with forward slash:
RIGHT('0' + CAST([month] as varchar(2)) + '/01/' + cast([year] as varchar(4)), 10)
ASKER
Nope, Still getting the same error.
ASKER
Both Year And Month are declared as Varchar.
If they are already varchar, then you can simplify to this:
RIGHT('0' + month, 2) + '/01/' + @year
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Solved!!
Open in new window