Articles in this section
Category / Section

How to Convert a String to a Decimal with Negative Numbers

Published:

When working with a range of numbers that include both positive and negative values and the column appears as a string, you may encounter issues when converting the data type. This article will guide you through the process of converting the string column to a decimal data type, while preserving negative numbers.

Solution

To convert the string column to a decimal data type, use the following expression:

Expression – try_convert(decimal(38,2), REPLACE([number], ',', '.'))

image.png

This expression uses the try_convert function to attempt the conversion of the string to a decimal data type with a precision of 38 and a scale of 2. The REPLACE function is used to replace any commas with periods, ensuring that the decimal separator is consistent.

Example

Consider the following example:

SELECT try_convert(decimal(38,2), REPLACE([number], ',', '.')) AS [ConvertedNumber]
FROM [YourTable]

This query will convert the [number] column in the [YourTable] to a decimal data type, while preserving negative numbers.

Conclusion

By using the try_convert function and the REPLACE function, you can successfully convert a string column containing both positive and negative numbers to a decimal data type. If you need further assistance, please feel free to reach out to our support team.

Additional References

Was this article useful?
Like
Dislike
Help us improve this page
Please provide feedback or comments
RM
Written by Reethika Moovendhan
Updated
Comments (0)
Please  to leave a comment
Access denied
Access denied