The isnull function requires 2 argument(s) [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
SELECT IIF(ISNULL(MAX((AttTmpInOutFrmMachine.AttId))),0,MAX((AttTmpInOutFrmMachine.AttId)))+1
FROM AttTmpInOutFrmMachine

The second argument in ISNull is value that will be used when your field data is actually null. Example:
IsNUll(someint, 0)
the above will return someint if someint not null, otherwise, it will return 0

This is enough
SELECT ISNULL(MAX(AttTmpInOutFrmMachine.AttId),0)+1 FROM AttTmpInOutFrmMachine

As Syntax for IIF is :
IIF ( boolean_expression, true_value, false_value )
I think you are looking out for logic like below:
SELECT IIF(ISNULL(MAX(AttTmpInOutFrmMachine.AttId),0)=0
,0
,MAX(AttTmpInOutFrmMachine.AttId))+1 as NextAttId
FROM AttTmpInOutFrmMachine

The first is the value that should checked against null and the second one is the value with that null should be replaced.
See here for a exmplanation for ms-sql
Syntax
ISNULL ( check_expression , replacement_value )
Arguments
check_expression Is the expression to be checked for NULL.
check_expression can be of any type. replacement_value Is the
expression to be returned if check_expression is NULL.
replacement_value must be of a type that is implicitly convertible to
the type of check_expresssion.

Related

How to convert varchar format into integer format with NULLs in column in SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I have a column with a varchar(255) format.
The column has number values and NULL values.
I need to convert the column to an integer format, but it fails because of the NULL values.
I have looked it up online and this issue is stated many places. But I can't seem to find the right solution. The solution has to work in a Microsoft SQL server enviroment.
I have tried this:
CAST(varcharname AS INT) AS varname
CAST(CASE WHEN varcharname IS NOT NULL THEN varcharname ELSE NULL END AS INT) AS varname
CAST(NULLIF(varcharname , '') AS INT) AS varname
CAST(NULLIF(varcharname , NULL) AS INT) AS varname
The problem is not the null values, and I can prove it:
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=c6734d0967eae022b0af2ee3a2b694db
More likely there is something else in the column that fails the conversion. Perhaps some whitespace? Remember that an empty string is not the same as NULL, and a string with only whitespace is not the same as an empty string.
Try this:
cast(nullif(rtrim(varcharname),'') as int)
One other thing to consider is if there is more data in the column and you are restricting results with a WHERE clause.
For example, let's say you have this data:
id
varcharname
1
'1'
2
NULL
3
'3'
4
'4'
5
'Five'
You may have a query with WHERE clause condition like this:
SELECT cast(varcharname as int) WHERE id <> 5
hoping to get results like this:
~
1
NULL
3
4
This solution will often (not always!) still fail, because the database may decide it's more efficient to apply the cast before the WHERE clause.
In this case (or if the earlier solution don't work) you can use TRY_CAST().
However, rather than use TRY_CAST() to merely smooth over your errors, first look to find out which rows/values are causing the errors (so you can fix them, and also fix whatever upstream process saved the bad data):
SELECT [id], varcharname
FROM [MyTable]
WHERE TRY_CAST(varcharname as int) IS NULL and varcharname IS NOT NULL
Finally, remember it's very poor practice to use varchar columns to store number (or date) data in the first place, to the point I consider such schemas to be broken.

Incorrect syntax when setting variable equal to a number in WHEN condition of T-SQL CASE statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Context: I have a scalar function that returns a formatted string. The type of formatting performed depends on the length LEN() of the string passed - this is handled with a T-SQL CASE statement.
Question: why am I getting a syntax error when setting variable, #LengthOfString, equal to a number?
Error:
Incorrect syntax near '='
Relevant code:
CREATE OR ALTER FUNCTION FormatString
(#PassedString varchar(255) NULL)
...
DECLARE #FormattedString varchar(255)
DECLARE #LengthOfString int = LEN(#PassedString)
SET #FormattedString = (SELECT
CASE #LengthOfString
WHEN #LengthOfString = 5
THEN RETURN Format(<formatting>)
WHEN ...
Remove #LengthOfString next to the CASE
That statement should look like this:
SET #FormattedString = (SELECT CASE
WHEN #LengthOfString = 5 THEN RETURN Format(<formatting>)
WHEN ...

Need to change field value from blank to "Unknown" in MS Access [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I need to replace all blank records within a query with "Unknown"
Tried setting the default value to "Unknown" but that did not work
Desired outcome:
"Blank" Records -> "Unknown" Records
Created an update query to replace Null values with "Unknown"
You can use IIF and Isnull to default null values to unknown - e.g.
select iif(isnull(field1), 'Unknown', field1),
iif(isnull(field2), 'Unknown', field2) from table1
(effectively saying if field1 is null return Unknown, otherwise return field 1)

divide by zero in specific case in sql server [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am getting Divide by zero error for the following statement.
select ((((57151130.0000000000)+(57612530.0000000000))/2)/((12548020.0000000000)-(34321580.0000000000)+(21773560.0000000000)))*366
The fact that you are supplying literal values is a little odd, however, one method of avoiding a divide by 0 error is using NULLIF (Transact-SQL).
NULLIF takes 2 parameters. If the values of the 2 expressions for the parameters are the same value, then NULLIF returns NULL, otherwise it returns the value of the first expression. For example (in literal terms):
SELECT NULLIF(1,0), NULLIF('a' + 'b' + 'c','abc');
This returns the values 1 and NULL. For your query, the format would therefore be:
SELECT (((57151130.0000000000+57612530.0000000000)/2)/NULLIF(12548020.0000000000-34321580.0000000000+21773560.0000000000,0))*366
Note I have removed several of the parenthesis as there's no need to wrap every value/expression in a pair. Doing so will likely lower readability.
Then, if the expression under the divisor has the value 0, the value NULL will be returned instead. Considering that NULL represents an unknown value, and {expr}/0 is certainly an unknown value as well, the value would be the most appropriate.
If you then need to represent the NULL value in a particular way, you can wrap the whole expression in a further ISNULL.
In addition, you can use CASE to check zero values statement:
select (
(((57151130.0000000000)+(57612530.0000000000))/2)
/
CASE WHEN((12548020.0000000000)-(34321580.0000000000)+(21773560.0000000000)) = 0 THEN NULL
ELSE ((12548020.0000000000)-(34321580.0000000000)+(21773560.0000000000))
END
)*366

converting varchar to integer [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am trying to count the number of records in the Face column that is between 0 and 25000. The dataset has Face as a char(20) so I needed to convert it to an integer before making any boolean expression. Here's my code below:
SELECT count(*)
FROM tablename
WHERE cast(face AS integer) > 0 and cast(face AS integer) <=25000;
I get an error, saying
Conversion failed when converting the varchar value '0.0' to data type int.
Face column contains null values and records like '0.0' and '0.0000'
I've looked online and haven't found anything useful on how to convert varchar to integer. Help anyone?
If your column has decimal points in it, your mistake is trying to interpret it as an integer:
select count(*)
from tablename
where cast(face AS float) > 0.0 and cast(face AS float) <= 25000.0
Is Face always guaranteed to be numeric? Hopefully so or any approach won't work very well for you.
Since you have decimal values, your best bet is to cast it as a float and then do your comparison:
SELECT count(*)
FROM tablename
WHERE cast(face AS float)> 0 and cast(face AS float) <=25000;