How to convert an nvarchar to decimal in SQL Server?
I need to convert a value like '38,716.1311'
to 38716.1311.
I have tried using this method but can't successfully convert:
DECLARE @TempValue NVARCHAR(100) = '38,716.1311'
SELECT
CONVERT(DECIMAL(18, 2), @TempValue)
Can anyone suggest the correct query? Thanks in advance
You can code it to any decimal (18, 2) value.
Syntax: SELECT CONVERT(<DATA_TYPE>, <VALUE>); --DATA_TYPE is the type we want to convert to. --VALUE is the value we want to convert into DATA_TYPE. Example: SELECT 'Weight of Yogesh Vaishnav is ' + CONVERT(NVARCHAR(20), weight) AS person_weight FROM person WHERE name = 'Yogesh Vaishnav';
Use the CAST() function to convert an integer to a DECIMAL data type. This function takes an expression or a column name as the argument, followed by the keyword AS and the new data type. In our example, we converted an integer (12) to a decimal value (12.00).
NVARCHAR is a locale-sensitive character data type that allows storing character data in variable-length fields as strings of single-byte or multibyte letters, numbers, and other characters supported by the code set of the necessary database locale.
Just remove comma from your string prior to conversion:
Select CONVERT(DECIMAL(18,2),replace('38,716.1311', ',', ''))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With