Solved

implicit conversion of nvarchar to int

Posted on 2015-01-01
7
253 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
My Query is not giving correct result. Please help 5 40
Trying to identify overlapping date ranges 5 22
SQL 2008 with .NET 4.5.2 4 28
SSRS  - Dropdown with Null 3 25
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

772 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