?
Solved

computed column for finding the numerical month

Posted on 2014-12-14
3
Medium Priority
?
112 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
[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
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 2000 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

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…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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 the fundamental information of how to create a table.
Suggested Courses

743 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