Reputation: 1704
I need to convert a numeric value to nvarchar in sql command.
Can anyone please help me.
Upvotes: 44
Views: 189247
Reputation: 261
If the culture of the result doesn't matters or we're only talking of integer values, CONVERT
or CAST
will be fine.
However, if the result must match a specific culture, FORMAT
might be the function to go:
DECLARE @value DECIMAL(19,4) = 1505.5698
SELECT CONVERT(NVARCHAR, @value) --> 1505.5698
SELECT FORMAT(@value, 'N2', 'en-us') --> 1,505.57
SELECT FORMAT(@value, 'N2', 'de-de') --> 1.505,57
For more information on FORMAT
see here.
Of course, formatting the result should be a matter of the UI layer of the software.
Upvotes: 2
Reputation: 261
declare @MyNumber float
set @MyNumber = 123.45
select 'My number is ' + CAST(@MyNumber as nvarchar(max))
Upvotes: 6
Reputation: 41902
declare @MyNumber int
set @MyNumber = 123
select 'My number is ' + CAST(@MyNumber as nvarchar(20))
Upvotes: 18