Click here to Skip to main content
15,175,525 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to do this VB Calculation Sql Level
I have Two Columns one is year 1 it is having 2.5 year (decimal) and another one is year 2 it is having 2.8 year (decimal) i need to add this and Calculate total year

below Calculation I attached I need to do this in Sql
How to do

Dim arr1() As String
       Dim arr2() As String
       Dim intY As Integer
       Dim intM As Integer
       ' Remove extraneous spaces
       TextBox1.Text = TextBox1.Text.Trim
       ' Ensure that there is a period
       If Not TextBox1.Text.Contains(".") Then
           TextBox1.Text += "."
       End If
       ' Remove extraneous spaces
       TextBox2.Text = TextBox2.Text.Trim
       ' Ensure that there is a period
       If Not TextBox2.Text.Contains(".") Then
           TextBox2.Text += "."
       End If
       ' Split into elements of an array (Index 0=Years, Index 1=Months)
       arr1 = TextBox1.Text.Split(CChar("."))
       arr2 = TextBox2.Text.Split(CChar("."))
       ' Convert to Integers and sum the values
           intY = CInt(arr1(0)) + CInt(arr2(0))
           intM = CInt(arr1(1)) + CInt(arr2(1))
       Catch ex As Exception
           ' Integer conversion failed for one of the values
           MessageBox.Show("One of the inputs is not a numeric", "Error", _
                  MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
           ' If the months value is greater or equal to 12
           ' Adjust the months value and the year value
           If intM > 11 Then
               intY += intM \ 12
               intM -= (intM \ 12) * 12
           End If
           ' Assign the new computed value to the TextBox
           TextBox3.Text = Format(intY, "###") & "." & Format(intM, "##")
       End Try

any one help me
Updated 27-Aug-14 22:14pm
Magic Wonder 28-Aug-14 3:01am
What will you consider below values in terms of years and months?

1. 2.5
2. 2.11
3. 2.13
hebsiboy 28-Aug-14 3:09am
i have Two Columns

year 1 : 2.5
year 2 : 2.11
Answer : 5.4
george4986 28-Aug-14 5:30am
what is ur expected result if u add 2.5 and 2.8?
hebsiboy 28-Aug-14 5:57am
Answer : 5.1 Five Year and one Month

By the simple way you can try this

get values from textboxes but do not split
multiply them with 12 so now you have two values in months
add them and divide the value by 12, this will be your answer.
First of all, I think you are confusing things in your explanation.

A value of 2.5 (decimal) is two and half years, it means 2 years and 6 months.
And a value of 2.8 (decimal) is 2 years, 9 months and 15 days

So the addition would result in 5 years, 3 months and 15 days, what would be 5,33333 in decimal.


But in the examples you say, you are having a format of Years.Months and not Years.Decimals of year.

So, the way to go is split your input.
Value_1 = 2.5 to be separated in Years_1 = 2 and Months_1 = 5
Value_2 = 2.8 to be separated in Years_2 = 2 and Months_2 = 8

You thought that part correectly, but you choose a bad end for the idea. The correct way should be:

When ready, you add Years_1 and Years_2, what gives you 4
Then you add Months_1 and Months_2, what gives you 13
When Months_result is bigger than or equal to 12, then you substract 12 from the Months_result and add 1 to the Years_result
That way 4 years and 13 months ends in 5 years and 1 month

Your 2nd example:
2.5 + 2.11 = 4.16 = (4+1).(16-12) = 5.4

I hope you get the idea. The implementation of that is quite easy and it is your task.
Try it and come back if facing problems

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900