Solved

computed column for finding the numerical month

Posted on 2014-12-14
3
102 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

680 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