SQL CAST with ISNULL results in conversionfailure - sql-server

I've got a nullable column of numbers, which is unfortunately declared as a varchar in the database, and so I want to convert the NULL to 0. Doing this accomplishes that.
select ISNULL(col, '0') from table;
So now there are no null possibilities, and then I want to convert the column to an actual int value, so I tried to wrap that.
select CAST(ISNULL(col, '0') AS INT) from table
When that runs I get a conversion error, as shown below. I don't understand why, or how to get around this:
Conversion failed when converting the varchar value 'NULL' to data type int.

Try like this: Take your ISNULL statement outside
SELECT ISNULL(TRY_CAST(col AS INT),0) AS [col]
FROM SAMPLE_TAB;
If this does not help, then please comment your SQL version.

Related

Error converting data type nvarchar to float in SQL Server 2008

select sum(cast(mmax as float)
from table
mmax is of datatype nvarchar and the value is
string,int,decimal, value
I trying to sum of like value 17.50,35.00.
I am avoiding string value in where clause
But not solved this problem
Error is thrown
String/Varchar values with commas such as "10,000" pass the IsNumeric() test but do not cast/convert into numeric types without raising an error.
You can replace the commas and perform the cast and sum operation:
select sum(cast(replace(mmax,',','') as float))
from tbl
where isnumeric(maxx)>0
One of the values cannot be converted to a float. You may have a million values that can convert if one (has a letter O instead of a 0 for example) you will get that message.

T-SQL converting string into float

In a stored procedure on my SQL Server, I am trying to convert values from a varchar column into a float format.
The values into the varchar column are numbers with a sign at the beginning and a '.' before decimals.
Examples: '+0000000000000044.09' or '-0000000000114995.61'
If I try this: convert(float,mystring), it doesn't work.
I have:
Error converting data type varchar to float
Is this kind of conversion possible?
Or is there another way to convert a string with a sign and a '.' into a float?
As your examples both work, I'd guess there's another value somewhere in your table that's causing the problem. In recent versions of SQL Server (2012 onwards), the TRY_CONVERT function can be useful for tracking down this kind of issue.
TRY_CONVERT will not throw an exception on a conversion failure, but instead return a NULL value, so you can figure out which values are causing the problem like this:
SELECT * FROM your_table WHERE TRY_CONVERT(FLOAT, your_column_name) IS NULL
If any rows are returned, those are the rows with problem values that can't be converted to FLOAT.
You can try using CAST(mystring as float) or TRY_CONVERT(float,mystring).
Thuough convert(float,mystring) also should work fine. I would suggest checking your data.

Conversion failed when using nvarchar, but not varchar

I've run into a situation where I'm getting an error if I run the same query on the same table, the difference being the column type. The query used is:
SELECT [name] FROM [demo] WHERE [name] = 1111111
If the name column is varchar(7) then it runs no problem.
If the name column is nvarchar(7) then it gives an error:
Conversion failed when converting the nvarchar value 'BBBBBBB' to data type int.
The error makes sense - I can see why the integer value can't be directly compared to the string value. I also realise that I can resolve it with a cast to a string for the condition.
However, what I'm not getting is why the behaviour is different for nvarchar and varchar. This chart appears to suggest that they should have the same behaviour for type conversion.
Does anyone know why this is happening?
If you using nvarchar column your query should look like:
SELECT [name] FROM [demo] WHERE [name] = N'1111111'
N at the begin of string to make sure that your string is unicode string (nvarchar)

Error cast varchar as decimal

I have this query in SQL Server 2008:
select ID,CAST(replace(SUBSTRING(LTRIM([value]),1,8)+'.'+SUBSTRING(LTRIM([value]),9,7),',','.')AS DECIMAL (16,7)),
from T1
This is returning cast error.
To find where is the problem I tried the following:
select ID,SUBSTRING(LTRIM([value]),1,8)+'.'+SUBSTRING(LTRIM([value]),9,7),
isnumeric(SUBSTRING(LTRIM([value]),1,8)+'.'+SUBSTRING(LTRIM([value]),9,7))
from T1
where isnumeric(SUBSTRING(LTRIM([value]),1,8)+'.'+SUBSTRING(LTRIM([value]),9,7))<>1
But it returns 0 rows, then I guess that all the values are feasible to cast them to decimal but when I run the first query it fails.
Am I missunderstanding something that creates the problem?
P.D: Value is varchar datatype.
If you were using 2012 then you can use Try_Convert()
However there is a way to get this to work with 2008 by using a CASE statement. This works because CASE stops evaluating when it finds a match.
Here's a simple example for Varchar to Int but you can easily adapt it for Decimal.
CASE WHEN [value] LIKE '%[^0-9]%' THEN [value]
ELSE Convert(varchar(20), CAST([value] as INT))
END

how to check for a null value in t-sql

I want to check for a value in a table to see if it's null.
If so, I just want output to be '0.000'; otherwise, I'd like the actual value which must be converted to a varchar field.
The definition of the column in the database is decimal(10,3).
When I run my below query (pertinent part of the query), I get an error message:
Error converting data type varchar to numeric.
Here is my t-sql syntax that is getting the error. How do I correct it?
isnull(nullif(Ryder_Price,'0.000'),convert(varchar(max),Ryder_Price))
EDIT
Here is the full SELECT statement:
Select '4'+','
+','+Case when Country_Code= 1 then 'USA' else 'CAN' End
+','+
+','+isnull(nullif(Ryder_Price,'0.000'),convert(varchar(max),Ryder_Price))
+','+isnull(nullif(Ryder_Price,'0.000'),convert(varchar(max),Ryder_Price))
+','+isnull(nullif(Ryder_Price,'0.000'),convert(varchar(max),Ryder_Price))
+','+
+','+'01/01/1900'
+','+'01/01/2099'PartMaster_String
,4 ord,part_no
, RIGHT('000'+convert(varchar(3),ATA_SYSCode),3)+'-'+RIGHT('000'+convert(varchar(3),ATA_Asmcode),3)+'-'+RIGHT('000'+convert(varchar(3),ATA_Partcode),3) taskstring
From tbl_CPM_PARTS_MASTER_PTC
The reason for the error message is that it is possible for the line you posted to simply have Ryder_Price as a value, which is a decimal type. And then you are trying to concatenate it to a string.
To acheive your stated goal:
I want to check for a value in a table to see if it's null.
If so, I just want output to be '0.000'; otherwise, I'd like the
actual value which must be converted to a varchar field.
Try this:
convert(varchar(max),isnull(Ryder_Price,'0.000'))

Resources