Solved

Month Year concatenate

Posted on 2009-06-29
14
603 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 142

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

810 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