Solved

Month Year concatenate

Posted on 2009-06-29
14
607 Views
Last Modified: 2013-11-10
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
0
Comment
Question by:Deepmath
14 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24738174
I knew how to do in t-sql:
RIGHT('0' + CAST(month as varchar(2)) + '\' + cast(year) as varchar(4)), 7)

Open in new window

0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24738179
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.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24738287
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)

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:Deepmath
ID: 24738529
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
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24738537
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)

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24738545
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)

Open in new window

0
 

Author Comment

by:Deepmath
ID: 24739004
       

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.



0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24739071
See if it is the \ needing to be escaped.
RIGHT('0' + CAST([month] as varchar(2)) + '\\01\\' + cast([year] as varchar(4)), 10)

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24739081
Or try with forward slash:
RIGHT('0' + CAST([month] as varchar(2)) + '/01/' + cast([year] as varchar(4)), 10)

Open in new window

0
 

Author Comment

by:Deepmath
ID: 24739111
Nope, Still getting the same error.
0
 

Author Comment

by:Deepmath
ID: 24739980
Both Year And Month are declared as Varchar.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24740119
If they are already varchar, then you can simplify to this:
RIGHT('0' + month, 2) + '/01/' + @year

Open in new window

0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 24740127
Sorry forgot to remove one of the @ symbols.
RIGHT('0' + month, 2) + '/01/' + year

Open in new window

0
 

Author Closing Comment

by:Deepmath
ID: 31598030
Solved!!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to export SQL to firebase ? 7 67
tools to scan a SQL server's problem 14 26
TSQL convert date to string 4 34
where to find DTS instalation package for sql 2014 64 bit 3 10
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question