Solved

implicit conversion of nvarchar to int

Posted on 2015-01-01
7
250 Views
Last Modified: 2015-01-03
In MSSQL 2008, I have a table called tblmaster with an int column called "weight"

UPDATE documenter.dbo.tblmaster
SET  
  weight = '55.0'
where Account=6199155;

Open in new window


and it gives me an error
Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the varchar value '55.0' to data type int.

OK, so I try to cast it with

UPDATE documenter.dbo.tblmaster
SET  
  weight = CAST('55.0' as INT)
where Account=6199155;

Open in new window


and

UPDATE documenter.dbo.tblmaster
SET  
  weight = convert(int, '55.0')
where Account=6199155;

Open in new window


but no luck.

What gives?
0
Comment
Question by:mankowitz
  • 4
  • 3
7 Comments
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40526936
UPDATE documenter.dbo.tblmaster
SET  
  weight = 55.0
where Account=6199155;
0
 
LVL 24

Author Comment

by:mankowitz
ID: 40526946
Sorry, I should have mentioned that the constraint is that the '55.0' is supplied by another program which I am not able to modify. I am looking for a way to convert '55.0' into 55
0
 
LVL 33

Accepted Solution

by:
Mike Eghtebas earned 500 total points
ID: 40526954
What you have:

UPDATE documenter.dbo.tblmaster
SET  
  weight = CAST('55.0' as INT)
where Account=6199155;

is correct. Make sure field weight is also integer. The choice of Integer for weight field is unlikely.

try:
UPDATE documenter.dbo.tblmaster
SET  
  weight = CAST('55.0' as float)
where Account=6199155;
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 24

Author Closing Comment

by:mankowitz
ID: 40526976
Wow... that is completely nonintuitive. Thanks!!! Incidentally, the weight field really is an integer.
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40526991
you mean weight = CAST('55.0' as float) worked for you?
0
 
LVL 24

Author Comment

by:mankowitz
ID: 40529829
Yes... I thought that's why you suggested it as an answer. For whatever reason, you can't cast '55.0' as an INT, but you can cast it as a float, which, apparently is implicitly cast as an int.
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40529831
Oh, I didn't know I was (accidentally) this good. LOL

I assumed your weight column must be a float. This is why I suggested it. Well, as long as it worked for you at the end and we both leaned something about the conversion.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now