I have a field that I want to convert to int so I can to a count, however some of the field's values are 'null' so I'm getting an exception when it comes across these
Conversion failed when converting the nvarchar value '_____' to data type int.
I thought id be able to do something like the below but I get the same result.
CAST( CASE [value] WHEN 'is null' THEN 1 ELSE [value] END AS INT)
I want to change all null values in [value] to 1 and then change all [value] to int so I can calculate this field.
You should have no issue converting a NULL value to an int, so I assume the issue is a string 'NULL'.
SQL Server offers TRY_CONVERT(). I recommend that you use that;
select try_convert(int, [value])
In other databases, you can use a regular expression to validate the data.
it will be is null that is not string
CAST( (CASE WHEN [value] is null THEN 1 ELSE [value] END) AS INT)
but its better to use try_convert what actually #Gordon recommend
Since you aren't actually looking for the NULL--which is the absence of data. You have bad data, i.e. the actual text 'NULL' in a column.
I would actually recommend you updating all the values to NULL so you can avoid doing try casting data to fix your problem. That would be something like:
UPDATE yourtable
SET [value] = NULL
WHERE [value] = 'NULL'
Then I would also change the column to a true INT type to avoid this in the future. But that's a bit outside the scope of your question.
Of course, you can't always change the data types. If you can, then this will be a more permanent solution.
Related
I've come across an issue (that I've partially solved) but can't seem to find a reason behind the failing in the first place.
I have a field in a table which holds a combination of alpha and numerical values. The field is a char(20) data type (which is wrong, but unchangeable) and holds either a NULL value, 'Unknown' or the "numbers" 0, 50, 100. The char field pads the values with trailing white space. This is a known and we can't do a thing about it.
To remove the Unknown values, we have a series of coalesce statements in place, and these two return the error message as per the title.
,coalesce(DHMCC.[HESA Module Total Proportion Taught], 'Missing')
,cast(isnull(DHMCC.[HESA Module Total Proportion Taught] ,'Missing') as varchar(10))
The query I have is why am I getting this error when I'm not converting a data type of varchar to float (or am I?)
Does anyone have an idea as to where to look next to try to fix this error?
The STR() function accepts a float datatype as the first argument, therefore SQL Server is implicitly converting whatever you pass to this function, which in your case is the CHAR(20) column. Since unknown can't be converted to a float, you get the error.
If you run the following with the actual execution plan enabled:
DECLARE #T TABLE (Col CHAR(20));
INSERT #T VALUES (NULL);
SELECT Result = ISNULL(STR(Col, 25, 0), 'Missing')
FROM #T
Then checkthe execution plan XML you will see the implicit conversion:
<ScalarOperator ScalarString="isnull(str(CONVERT_IMPLICIT(float(53),[Col],0),(25),(0)),'Missing')">
The simplest solution is probably to use a case expression and not bother with any conversion at all (only if you know you will only have the 5 values you listed:
DECLARE #T TABLE (Col CHAR(20));
INSERT #T VALUES (NULL), ('0'), ('50'), ('100');--, ('Unknown');
SELECT Result = CASE WHEN Col IS NULL OR Col = 'Unknown' THEN 'Missing' ELSE Col END
FROM #T;
Result
---------
Missing
0
50
100
Missing
If you really want the STR() function, you can make the conversion explicit, but use TRY_CONVERT() so that anything that is not a float simply returns NULL:
DECLARE #T TABLE (Col CHAR(20));
INSERT #T VALUES (NULL), ('0'), ('50'), ('100');--, ('Unknown');
SELECT Result = ISNULL(STR(TRY_CONVERT(FLOAT, Col), 25, 0), 'Missing')
FROM #T
Result
------------
Missing
0
50
100
Missing
Although, since you the numbers you have stated are integers, I would be inclined to convert them to integers rather than floats:
DECLARE #T TABLE (Col CHAR(20));
INSERT #T VALUES (NULL), ('0'), ('50'), ('100'), ('Unknown');
SELECT Result = ISNULL(CONVERT(VARCHAR(10), TRY_CONVERT(INT, Col)), 'Missing')
FROM #T;
Result
---------
Missing
0
50
100
Missing
Thanks to #GarethD
I've only just come across TRY_CONVERT and this seems like the better option, so thanks him for that pointer, also trying with TRY_CAST as well.
The data really should be held in a varchar field, it's referential and not for calculation, and this seems to work equally as well,
-- Declare #varText as varchar(16) = '10 '
-- Declare #varText as char(16) = 'Unknown'
-- Declare #varText as char(16) = ''
SELECT
ISNULL(NULLIF(TRY_CAST(LTRIM(RTRIM(#varText)) as varchar(16)), ''), 'Missing') AS HESA
I've created this test scenario which works ok.
My Query
Select * from MyTable
The table consists 300k rows.
It runs for 200k+ rows and this error pops up.
How to handle this to get the full data?
Does MyTable have any computed columns?
Table consists of a computed column with the name IsExceeds which is given below for your reference.
This is the computed column formula:
(CONVERT([int],[Pro_PCT])-CONVERT([int],replace([Max_Off],'%','')))
Field Definitions:
[Pro_PCT] [nvarchar](50) NULL,
[Max_Off] [nvarchar](50) NULL,
[IsExceeds] AS (CONVERT([int],[Pro_PCT])-CONVERT([int],replace([Max_Off],'%','')))
Kindly convert in float then convert into int.
declare #n nvarchar(20)
set #n='11.11'
if (isnumeric(#n)=0)
SELECT 0
else
SELECT CAST(CONVERT(float, #n) as int) AS n
Why are you storing amounts as strings? That is the fundamental problem.
So, I would suggest fixing your data. Something like this:
update mytable
set max_off = replace(max_off, '%');
alter table mytable alter pro_pct numeric(10, 4);
alter table mytable alter max_off numeric(10, 4);
(Without sample data or an example of the data I am just guessing on a reasonable type.)
Then, you can define IsExceeds as:
(Pro_PCT - Max_Off)
Voila! No problems.
Based on the formula - either Pro_PCT or Max_Off contains the value 11.11 (well, with an extra % for Max_Off. Perhaps they also contain other values that can't be converted to int.
Here's what you can do to find all the rows that will cause this problem:
Select *
from MyTable
where try_cast(Pro_PCT as int) is null
or try_cast(replace([Max_Off],'%','') as int) is null
After you've found them, you can either fix the values or change the calculation of the computed column to use try_cast or try_convert instead of convert.
Check the bad data with
select * from MyTable where isnumeric( Max_Off ) = 0
Why this statement return 0 instead of empty string? I am actually using a view which select statement containg hardcoded column return NULL. While I am trying to check if its a NULL, it actually returns 0.
SELECT ISNULL((SELECT NULL as Col), '')
Any suggestion or help would be appreciated...
snapshot
NULL does not have a clear type, but in a plain SELECT NULL, it gets returned as type int. When you have an expression involving an int and a char(N), int wins, the char(N) value gets converted to int, not the other way around. To make things more confusing, '' happens to be convertible to int without any problem, and the result of the conversion is 0.
SELECT ISNULL((SELECT CAST(NULL AS char(1)) AS col), '') should return an empty string.
I am trying to compare a column col1 and a variable #myvar in a WHERE clause. Both usually contain GUIDs, but may also have NULL values.
I thought I could get around the fact that NULL=NULL evaluates to FALSE by using WHERE ISNULL(col1, '')=ISNULL(#myvar, ''). That would compare two empty strings instead, and evaluate to TRUE.
This will, however, produce the following error message:
Msg 8169, Level 16, State 2, Line 3 Conversion failed when converting
from a character string to uniqueidentifier.
I tried
DECLARE #myvar uniqueidentifier = NULL
SELECT ISNULL(#myvar,'') as col1
Same error message.
Two questions:
First, I am trying to convert a uniqueidentifier variable - even though it has a NULL value - to an (empty!) string, not the other way around, as the error message suggests. What gives?
Second, is there a better way to word that WHERE clause I need, to allow for comparing uniqueidentifiers that might be NULL?
I think below expression can be used to check if the GUID column is empty
CAST(0x0 AS UNIQUEIDENTIFIER)
some thing like
...WHERE GuidId <> CAST(0x0 AS UNIQUEIDENTIFIER)
Since the first argument you are passing isnull is not a literal null, it will determine the return type of that call, a uniqueidentifier in your case. The second argument, '', cannot be cast to this type, hence the error you're getting.
One way around this is just to explicitly check for nulls:
WHERE (#myvar IS NULL AND col1 IS NULL) OR (col1 = #myvar)
The reason ISNULL isn't working for you is that the replacement value (the value to be used if the check expression really is null) must be implicitly convertible to the type of the check expression.
Your WHERE clause can use a col IS NULL AND #var IS NULL to check that state.
As others have pointed out, exclude the NULL values from the results and THEN do the comparison. You can use COALESCE to exclude NULL values from comparisons.
Try the following code:
WHERE ISNULL([Guid], NEWID()) = #myvar
Here is another way to overcome this issue:
DECLARE #myvar uniqueidentifier = NEWID()
SELECT * FROM TABLE
Where ISNULL(col1,#myvar) = ISNULL(Col2,#myvar)
This will resolve your error. Conversion failed when converting from a character string to uniqueidentifier.
I needed something similar on a where clause to compare 2 fields.
Declaring a uniqueidentifier variable is causing performance issues.
So I've used something like this.
WHERE COALESCE(Table1.Field1, CAST('00000000-0000-0000-0000-000000000000' AS UNIQUEIDENTIFIER))=COALESCE(Table2.Field2, CAST('00000000-0000-0000-0000-000000000000' AS UNIQUEIDENTIFIER))
I have a table that stores different info into a value column as varchar(max)
I need to be able to extract some of the info from this table, convert it to an integer and average the numbers. I'm running into an issue though when trying to convert.
This does not work:
select cast(value as float) as value
from table
Can anyone tell me how to properly convert this?
Presumably, the problem is that some values are not in a numeric format. Try this instead:
select (case when isnumeric(value) = 1 then cast(value as float) end)
from table
This converts all the numbers to float, and puts NULLs in the remaining fields.
If you want to see the values that are causing problems, use this:
select value
from table
where isnumeric(value) = 0 and value is not null