15,175,525 members
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 Vb.net Calculation I attached I need to do this in Sql
How to do
-------------------------------------------

VB
```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
Try
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)
Finally
' 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
Posted
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
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

## Solution 1

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

## Solution 3

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:

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

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