> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# VBA to T-SQL via TDD: Step 10
- URL: https://nolongerset.com/vba-to-tsql-via-tdd-step-10/
- Published: 2021-09-18T00:32:30.000Z
- Updated: 2026-05-08T13:02:21.000Z
- Description: Step 10. Repeat steps 8 & 9 until all tests pass
- Author: Mike Wolfe
- Tags: VBA to T-SQL via TDD, #Import 2026-05-20 02:59

*This is Part 10 of [my series](https://nolongerset.com/vba-to-tsql-via-tdd/) on converting a VBA function to a SQL Server scalar function using test driven development. As a proof of concept, I will be [re-implementing the VBA Round function](https://nolongerset.com/trust-but-verify/) (which uses bankers rounding) as a SQL Server scalar function.*

---

Test-driven development is an iterative process.

You start with a dummy/placeholder function. Write a bunch of tests. Make sure they fail. Refactor the function. Rerun the tests. Refactor. Rerun. Lather. Rinse. Repeat. Until all the tests pass.

## Banker's Rounding Example

In [Step 9](https://nolongerset.com/vba-to-tsql-via-tdd-step-9/) of our example, we had four failing tests:

![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/09/image-54.png)

These four failures get to the heart of the difference between traditional rounding and banker's rounding. In traditional rounding, you "round half away from zero." In [banker's rounding](https://nolongerset.com/a-rounding-we-will-go/), you "round half toward even."

## Combining the Refactor & Rerun Tests Steps

In SQL Server Management Studio, we can save some time by combining steps 8 and 9 into a single query window. The ALTER FUNCTION statement must be the only statement in a batch, so we use the SSMS `GO` keyword to process the statement in its own batch.

Here's what the concept looks like in practice (you may need to open the image in a new browser tab to see it full-size):

![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/09/image-55.png)

Here's the working function, inspired by the article, *[SQL Server Rounding Methods](https://blogs.lessthandot.com/index.php/datamgmt/datadesign/sql-server-rounding-methods/)*, from the LessThanDot blog:

```sql
ALTER FUNCTION [dbo].[VBA_Round](
    @OriginalVal float,
    @RoundDigits int
)
RETURNS float
AS
BEGIN
	RETURN CASE When Abs(@OriginalVal - Round(@OriginalVal, @RoundDigits, 1)) * Power(10, @RoundDigits+1) = 5 
                Then Round(@OriginalVal, @RoundDigits, Case When Convert(int, Round(abs(@OriginalVal) * power(10,@RoundDigits), 0, 1)) % 2 = 1 Then 0 Else 1 End)
                Else Round(@OriginalVal, @RoundDigits) 
                End
END;
```

### Final notes

If you've been following along with this series, you may notice that I changed the type of the `@OriginalVa` input from `sql_variant` to `float`. Keep in mind that the *float* type is an [approximate numeric value](https://docs.microsoft.com/en-us/sql/t-sql/data-types/data-types-transact-sql?view=sql-server-ver15#data-type-categories), not an exact numeric value. Depending on your needs, you may want to create a separate function (something like *VBA\_Round\_Exact*) that takes and returns a *decimal* type.

---

### External references

[LessThanDot - SQL Server Rounding MethodsThere are various ways to round a number, and most of us don’t give it much thought, but we should. There are several methods for rounding: Round Up, Round Down, Round Away From Zero, Round Toward Zero, and Round Toward Even (also known as bankers rounding, unbiased rounding, Gaussian rounding, and …![](https://blogs.lessthandot.com/images/favicon.ico)LessThanDotGeorge Mastros (gmmastros)![](https://blogs.lessthandot.com/images/logo_prod.png)](https://blogs.lessthandot.com/index.php/datamgmt/datadesign/sql-server-rounding-methods/)

### Referenced articles

[How to Convert a VBA Function to a SQL Server Scalar FunctionMS Access Developers Can Remove All the Risk From This Complex Process With These 3 Words![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/09/caterpillars-2869783_1920.jpg)](https://nolongerset.com/vba-to-tsql-via-tdd/)

[Trust But VerifyWhat Ronald Reagan’s Words Can Teach Us About the Sample Code We Find on the Internet![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/09/president-67550_1920.jpg)](https://nolongerset.com/trust-but-verify/)

[VBA to T-SQL via TDD: Step 9Step 9\. Execute the test query in SQL Server![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/09/Step9.png)](https://nolongerset.com/vba-to-tsql-via-tdd-step-9/)

[A Rounding We Will GoTwo kinds of rounding, the VBA language spec vs. the Office VBA implementation, and a drop-in replacement for VBA.Round().![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2020/10/ball-407081_1920.jpg)](https://nolongerset.com/a-rounding-we-will-go/)