I'm trying to get a count for a column to see the max characters. I'm getting a warning, I know it doesn't effect, but it's more of an annoyance and would like to eliminate the warning.
My example is as follows:
Declare #Countthis varchar (255)
select #Counthis = max(len(col1)) from #temp
Print '------- This is the largest count for this column-----' + #Countthis
The warning I receive is:
Warning: Null value is eliminated by an aggregate or other SET operation.
I tried using Case statement but I couldn't figure it out. If the value is NULL just ignore the value.
Is this possible?
You can use
Declare #Countthis varchar (255)
select #Counthis = max(len(IsNull(col1,''))) from #temp
Related
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
I'm trying to create a search function that is able to search by condition, platenumber and maximum volume using a select statement:
select Condition, PlateNumber, MaximumVolumeLoad
from [Truck Table]
where Condition=#id OR PlateNumber=#id OR MaximumVolumeLoad>=#id
However, the problem is My MaximumVolumeLoad column is set into int and whenever I search for the condition, I get this error:
Conversion failed when converting the nvarchar value 'good' to data type int.
Is there any way where I can search for them at the same time without having to create another query?
This seems like a bad idea, but you can do it by converting the value to a number:
select Condition, PlateNumber, MaximumVolumeLoad
from [Truck Table] tt
where Condition = #id or
PlateNumber = #id or
MaximumVolumeLoad >= try_convert(int, #id);
Note that if the value is not a valid integer, this will return NULL, so it will never match MaximumVolumeLoad. Presumably, this is the correct behavior.
May be an alternative to what has been suggested by Gordon is to convert the column to varchar before the compare. This should behave in the same way.
select Condition, PlateNumber, MaximumVolumeLoad
from [Truck Table] tt
where Condition = #id or
PlateNumber = #id or
cast(MaximumVolumeLoad as varchar(10)) >= #id;
Note: Forgot to add that it assumes that you will have alphabets in the #id. Based on ASCII character set numbers come before alphabets. So this should work as expected if it is any string that is entered.
But is you check for '2' >= '+' the results could go wrong. (if #id has a value of a '+')
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))
My curiosity always gets the best of me and I've searched online for an explanation to this and came up with nothing (could be because I didn't use the right terms.)
Can someone please explain why SQL Server returns a value of zero (0) when the following is executed, instead of an empty string ('').
DECLARE #I AS INT
SET #I = NULL
SELECT ISNULL(#I, '') -- 0
As declared here, the second argument to ISNULL is the replacement_value, which "must be of a type that is implicitly convertible to the type of check_expresssion." Implicitly converting '' to INT results in 0.
Because #I is declared as an INT, the empty string is implicitly CAST as an integer resulting in a ZERO.
I know is an old question, but I want to share.
The issue here is not because the casting of the ' ' expression, is because the int data type is not null-able and, when you try to SET #I = NULL SQL set the default value zero(0).
That's why the statement ISNULL(#I, '') does not find any null in #I and returns its value(0).
Hope it helps somebody out there.
Please suppose that in SQL Server 2005, if you launch the following query:
SELECT CHICKEN_CODE FROM ALL_CHICKENS WHERE MY_PARAMETER = 'N123123123';
you obtain:
31
as result.
Now, I would like to write a function that, given a value for MY_PARAMETER, yields the corresponding value of CHICKEN_CODE, found in the table ALL_CHICKENS.
I have written the following stored function in SQL Server 2005:
ALTER FUNCTION [dbo].[determines_chicken_code]
(
#input_parameter VARCHAR
)
RETURNS varchar
AS
BEGIN
DECLARE #myresult varchar
SELECT #myresult = CHICKEN_CODE
FROM dbo.ALL_CHICKENS
WHERE MY_PARAMETER = #input_parameter
RETURN #myresult
END
But if I launch the following query:
SELECT DBO.determines_chicken_code('N123123123')
it yields:
NULL
Why?
Thank you in advance for your kind cooperation.
define the length of your varchar variables like this
varchar(100)
Without the 100 (or whatever length you choose) its lengh is 1 and the where clause will filter out the correct results.
Specify a length for your varchar (ex.: varchar(100)). Without length, varchar = 1 char.
As per other PS, You can store only one char in the #myresult because you have not specified any length, bcoz 1 char length is default for Varchar datatype.
Why we are getting NUll, not the first char:
If there are multiple records are filtered on basis of Where clause in ALL_CHICKENS table then the value of CHICKEN_CODE column is picked up from last row in ALL_CHICKENS table.
It seems that the last row has null value in CHICKEN_CODE column.
Specify a length for #input_parameter, #myresult as by default varchar lengh is 1.