Solved

Strip Month and year from date column for new column

Posted on 2011-03-22
2
364 Views
Last Modified: 2012-05-11
Hi,
I need to simply create a new column in a select statement based off the current columns.
Derived column is called xNew
and needs to be made up of the following Date1.MM + Date1.YY
i am using the cast statement
Date1 = 2010/03/06
i need column Newx to = 0310
i am using the datepart function to but it is not giving me a leading 0 infront of the 3
(hope this makes sense)

select CAST(datepart(MONTH,Date1) as varchar(2)) + CAST(datepart(YYYY,Date1) as varchar(2)) as xNew from tmpTable



thanks
0
Comment
Question by:CraigLazar
2 Comments
 
LVL 4

Accepted Solution

by:
joeyw earned 125 total points
ID: 35189454
concatenate the zeros to your result and then use the right command to extract the lenght you want.

select RIGHT('0000' + CAST(datepart(MONTH,Date1) as varchar(2)) + CAST(datepart(YYYY,Date1) as varchar(2)),4) as xNew from tmpTable



0
 
LVL 4

Author Closing Comment

by:CraigLazar
ID: 35189775
perfect thanks man
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now