Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Conversion failed when converting the nvarchar val

Author  Topic 

micnie_2020
Posting Yak Master

232 Posts

Posted - 2014-11-01 : 05:36:38
Hi All,

I got error:
Conversion failed when converting the nvarchar value to data type int.




declare @deci int
declare @UserName nvarchar(200)
declare @decimal nvarchar(2)

set @decimal='0D'
set @UserName='Micnie'
select @deci=case when @decimal='0D' then 0 else 8 end

SET @sSQL=' SELECT '
SET @sSQL=@sSQL+' Price=Round((Price/Rate),Convert(Int,'+@deci+')) '
SET @sSQL=@sSQL+' FROM TBL_'+@UserName
EXEC sp_executesql @sSQL


Please help.

Thank you.

Regards,
Micheale

mole999
Starting Member

49 Posts

Posted - 2014-11-01 : 08:05:21
excuse my ignorance

isn't 0D a string, (even though we recognise it as HEX)



http://msdn.microsoft.com/en-GB/library/ms191530.aspx

Mole
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-11-01 : 09:40:07
[code]declare @deci int
declare @UserName nvarchar(200)
declare @decimal nvarchar(2)

set @decimal='0D'
set @UserName='Micnie'
select @deci=case when @decimal='0D' then 0 else 8 end

SET @sSQL=' SELECT '
SET @sSQL=@sSQL+' Price=Round(Price / Rate, ' + CONVERT(VARCHAR(12), @deci) + ') '
SET @sSQL=@sSQL+' FROM TBL_'+@UserName
EXEC sp_executesql @sSQL [/code]


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
   

- Advertisement -