Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert nvarchar to decimal in SQL

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

like image 902
Arun D Avatar asked Mar 01 '17 09:03

Arun D


People also ask

Does nvarchar allow Decimals?

You can code it to any decimal (18, 2) value.

How do I convert nvarchar?

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';

How do I convert a number to a DECIMAL in SQL?

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).

Does nvarchar accept numbers?

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.


1 Answers

Just remove comma from your string prior to conversion:

Select CONVERT(DECIMAL(18,2),replace('38,716.1311', ',', '')) 
like image 141
Andrey Korneyev Avatar answered Oct 05 '22 12:10

Andrey Korneyev