Solved

computed column for finding the numerical month

Posted on 2014-12-14
3
95 Views
Last Modified: 2014-12-14
I have  a column named Month in my table called tblOrgOrganizing.

The values are January, February, March, etc....

I would like to add a second computed column that will give a numerical equivalent to the month as below:

January = 1
February = 2

How can I accomplish this?
0
Comment
Question by:al4629740
  • 2
3 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40499338
select datepart(MM,[YourMonthColumName] + ' 1 2000')
returns 1

e.g.
select datepart(MM,'January' + ' 1 2000')
returns 1
select datepart(MM,'February' + ' 1 2000')
returns 2

To add as a computed column via TSQL:
ALTER TABLE dbo.tblOrgOrganizing ADD MonthNumber AS  datepart(MM,[YourMonthColumName] + ' 1 2000');

Or, to add via SSMS, see this link http://msdn.microsoft.com/en-us/library/ms188300.aspx
0
 

Author Comment

by:al4629740
ID: 40499356
This statement will add the new column and identify the numerical value?

ALTER TABLE dbo.tblOrgOrganizing ADD MonthNumber AS  datepart(MM,[NumericalMonth] + ' 1 2000');
0
 
LVL 18

Accepted Solution

by:
Simon earned 500 total points
ID: 40499379
Sorry, I didn't read your post carefully enough. Your varchar column is called 'Month', so I could have named it properly in my previous example. I also assumed your schema was dbo.

It should be

ALTER TABLE tblOrgOrganizing ADD MonthNumber AS  datepart(MM,Month + ' 1 2000');

I just tested this and it works on 2008R2 (adds the column and computes the numeric value of the month).
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

863 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

28 Experts available now in Live!

Get 1:1 Help Now