SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 CREATE MONTHS ROUNDING UP
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jfm
Posting Yak Master

134 Posts

Posted - 05/21/2012 :  07:24:52  Show Profile  Reply with Quote
Hi there,

I have to different columns inside my Table_1. Both columns are dates.

what I need is to see the difference of months between dates, rounding up the solution.

Imagine:

Class_Column Exam_Column

2012-05-28 2012-01-28


Answer in a NEW_Column = 4 (months)

I did try the structure from W3Schools site, but still not working.

Any clue?

Thanks

khtan
In (Som, Ni, Yak)

Singapore
16745 Posts

Posted - 05/21/2012 :  07:30:18  Show Profile  Reply with Quote
datediff(month, Exam_Column, Class_Column)



KH
Time is always against us

Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 05/21/2012 :  08:40:40  Show Profile  Reply with Quote
Hi,

That one, is the one that is not working in my case:

msg 102, level 15, state 1, line 1
incorrect syntax near 'month'

datediff(month, Exam_Column, Class_Column)

And i tried:

datediff(month, Exam_Column, Class_Column) from Table_1 into Period_Column

not working
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16745 Posts

Posted - 05/21/2012 :  08:49:04  Show Profile  Reply with Quote

SELECT datediff(month, Exam_Column, Class_Column) AS Period_Column from Table_1



KH
Time is always against us

Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 05/21/2012 :  09:03:41  Show Profile  Reply with Quote
Perfect! Thanks for your help!

I was misspelling a letter ;-(

Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 05/21/2012 :  09:38:11  Show Profile  Reply with Quote

How could I add this Period_Column to Table_1? That way I will have the Period_Column information in the same table


SELECT datediff(month, Exam_Column, Class_Column) AS Period_Column from Table_1

thanks
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16745 Posts

Posted - 05/21/2012 :  09:41:04  Show Profile  Reply with Quote
2 way.
1. as a normal column
- you need to update this column value manually
2. as a computed column
- the value is auto computed whenever the value of the 2 other column changes

Normal Column
-------------
alter table Table_1 add Period_Column integer

Computed Column
---------------
alter table Table_1 add Period_Column as datediff(month, Exam_Column, Class_Column)


KH
Time is always against us

Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 05/21/2012 :  12:13:23  Show Profile  Reply with Quote
That part is working perfectly.

Other issue that I found is that, the formula is rounding up, whenever my date day is bigger than 01, for instance:

Party_Column Bday_Column

09/03/2012 31/12/2011


Output given in Terms_Column, using the formula Datediff:

Terms_Column
3


In the reality, It has to be 2 terms, I want to use the condition that If any of my columns has a day between (01-14), do not round up my date, so I will have as an output:

Terms_Column
2

And if my day is between (15-31) round up.


how can I use Ceiling in the formula:

ALTER TABLE X
ADD Terms_Column AS DATEDIFF (MONTH, Party_Column, Bday_Column)


Many thanks



quote:
Originally posted by khtan

2 way.
1. as a normal column
- you need to update this column value manually
2. as a computed column
- the value is auto computed whenever the value of the 2 other column changes

Normal Column
-------------
alter table Table_1 add Period_Column integer

Computed Column
---------------
alter table Table_1 add Period_Column as datediff(month, Exam_Column, Class_Column)


KH
Time is always against us



Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000