Solved

T-sql Code

Posted on 2009-07-02
5
220 Views
Last Modified: 2012-05-07
Hi,

I have a table which looks like dis


StudentInfo table
Studentid       Month  Year      StartDate =====> all are in varchar format

876               8         2002     08/17/2002

876               9         2002     08/17/2002

876               10        2002     08/17/2002


876               12         2004     08/17/2002

1136               8         2004     08/17/2004

1136               9         2004     08/17/2004

I need to derive a new column (STAYMONTH)  which will have the Number of months for each student since the start date

Output Expected

StudentInfotable
Studentid       Month  Year      StartDate          StAYMONTH(New Column)

876               8         2002     08/17/2002         0

876               9         2002     08/17/2002         1

876               10        2002     08/17/2002        2


876               12         2004     08/17/2002       26  (difference between 12/2004- 10/2002)

1136               8         2004     08/17/2004       0

1136               9         2004     08/17/2004       0



Here's the logic to achieve this

@EM=Month(startdate)
@EY=Year(startdate)

@Current MOnth= Month
@Current Year= Year

@A=12*(@EY-1900)
@B=12*(@CurrentYear-1900)


The NEw column StayMOnth=@B-@A +(@Current MOnth  - @EM)

I am Uisng t-sql for the first time....How do i code this.




0
Comment
Question by:Deepmath
5 Comments
 
LVL 17

Expert Comment

by:pssandhu
ID: 24763504
I have not vetted your logic but assumming it is correct, you can do something like this-
Select *, (12*(Year(GetDate())-1900))-(12*(Year(StartDate)-1900))+(MONTH(GetDate())-Month(startdate)) as StayMonth
From StudentInfoTable
P
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24763526
shouldn't student 876 be 28 months?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24763533
select abs(datediff(m, cast(month as varchar(2)) + '/1/' + cast(year as varchar(4)), dateadd(m, 0, startdate)))
from tablename
0
 
LVL 14

Accepted Solution

by:
rob_farley earned 500 total points
ID: 24769085
To add a computed column (that's what it's called), with your logic, use:

alter table StudentInfotable
add StayMonth as 12 * (Year - Year(StartDate)) + Month - Month(StartDate)

(I've ignored the "-1900" bits, as they cancel each other out)

Hope this helps,

Rob
0
 

Author Closing Comment

by:Deepmath
ID: 31599192
worked 4 me
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

17 Experts available now in Live!

Get 1:1 Help Now