Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Strip Month and year from date column for new column

Posted on 2011-03-22
2
Medium Priority
?
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
joeyw earned 500 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

604 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