Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

T-sql Code

Posted on 2009-07-02
5
Medium Priority
?
269 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
[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
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 15

Accepted Solution

by:
rob_farley earned 1500 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

618 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