Solved

computed column for finding the numerical month

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

839 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