FEAT: Add support for decimal value in scientific notation by gargsaumya · Pull Request #313 · microsoft/mssql-python

Work Item / Issue Reference

AB#40022

GitHub Issue: #<ISSUE_NUMBER>


Summary

This pull request improves the handling of decimal values, especially those in scientific notation, when converting to SQL VARCHAR types. The changes ensure that decimals are consistently formatted as fixed-point strings rather than potentially problematic scientific notation, preventing SQL Server conversion errors. Additionally, new tests have been added to verify correct behavior for a variety of decimal values.

Decimal formatting and conversion improvements:

  • Updated _map_sql_type in mssql_python/cursor.py to use format(param, 'f') instead of str(param) for MONEY and SMALLMONEY types, ensuring decimals are always converted to fixed-point strings. [1] [2]
  • Modified executemany in mssql_python/cursor.py to format decimal values as fixed-point strings before insertion when mapped to SQL_VARCHAR.

Test suite enhancements:

  • Added test_decimal_scientific_notation_to_varchar in tests/test_004_cursor.py to verify that decimals (including those with scientific notation) are correctly converted and stored as VARCHAR without causing conversion errors.

Test cleanup and simplification:

  • Removed exception handling logic in test_numeric_leading_zeros_precision_loss and test_numeric_extreme_exponents_precision_loss that previously skipped tests on conversion errors, as these errors should no longer occur with the improved formatting. [1] [2]