Solved

T-sql Code

Posted on 2009-07-02
5
229 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 15

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Params not declared?  T-SQL syntax question 3 38
Service Statictic 11 30
CROSS APPLY 4 43
all records from previous month 6 45
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 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

9 Experts available now in Live!

Get 1:1 Help Now