I am using vb6 front end and sybase as backend. Can I use IsNull in where clause OR I must use = NULL
SQLStr = "select distinct thrd_pty.id_thrd_pty, thrd_pty.name_thrd_pty, thrd_pty.nbr_tax_idtn " _
& "from thrd_pty, cntct_rltn, cntct " _
& "where cntct.id_cntct = '" & cntct(ColNum, CurThrdPty) & "' and cntct_rltn.id_super = cntct.id_cntct and cntct_rltn.name_super = 'cntct' and thrd_pty.id_thrd_pty = cntct_rltn.id_thrd_pty and cntct_rltn.dt_eff_end IsNull "
The usual syntax in Sybase is either AND cntct_rltn.dt_eff_end IS NULL (which is perfectly fine for your example) or ISNULL(cntct_rltn.dt_eff_end, 0) for the more complicated stuff where you want to replace null with a defined default value. But you can do any of the follownig in ASE 15:
Adaptive Server treats null values in different ways, depending on the operators that you use and the type of values you are comparing. In general, the result of comparing null values is UNKNOWN, since it is impossible to determine whether NULL is equal (or not equal) to a given value or to another NULL. The following cases return TRUE when expression is any column, variable or literal, or combination of these, which evaluates as NULL:
expression is null
expression = null
expression = #x where #x is a variable or parameter containing NULL. This exception facilitates writing stored procedures with null default parameters.
expression != n where n is a literal not containing NULL and expression evaluates to NULL. (source)
(uppercase is mine as a personal style preference)
As a side note, you can also use COALESCE() function to deal with NULL values, but in your use case it won't add anything useful.
Reference:
Testing column for NULL values at Sybase infocenter
ISNULL as a function
COALESCE
Use is null:
cntct_rltn.dt_eff_end is null
More info, here
EDIT:
cQ = "update var1 = null ".(can be) .cQ = "update var1 is null" ?????
– niru dyogi
If you are updating, you have to use the assignment operator =. You can use is null only when evaluating conditions (such as the one in the example you provided).
Related
CASE WHEN (r.code_value4 = 0 AND LENGTH(ltrim(rtrim(xx.AFFILIATE_CODE))) > 0) AND
r_intercompany.code_value1 is not null AND
ltrim(rtrim(xx.AFFILIATE_CODE)) <> (CASE WHEN xx.COMPANY_CODE_JE_EXCEPTION_FLAG = 1
THEN r.code_value3 ELSE r.code_value1 END)
THEN r_intercompany.code_value1
ELSE NVL(r_mga_acct.code_value2, xx.ACCOUNT_NUMBER) END
I have view in which the above part of the sql is being used in select statement and as well as to compare with a field while joining. When I run the view after making this change, it is throwing the error ORA-01722 invalid number.
Please let me know on how to correct this.
Thanks
" it is throwing the error ORA-01722 invalid number."
It is likely somewhere you are comparing a numeric column with a string column . Oracle is implicitly casting the string to a number but the column contains values which cannot be converted, so it hurls.
"Please let me know on how to correct this."
Don't rely on implicit data conversion. Go through your code, check the data types of all the columns. Where you find a varchar2 column being compared to a number you need to cast the number to a string. For literals that means quoting them - '1' instead of 1 - and for columns that means wrapping them in to_char() calls.
My Suspicion is either r.code_value4 field or xx.COMPANY_CODE_JE_EXCEPTION_FLAG field is String type hence try the below
CASE WHEN (r.code_value4 = '0' AND LENGTH(ltrim(rtrim(xx.AFFILIATE_CODE))) > 0) AND
r_intercompany.code_value1 is not null AND
ltrim(rtrim(xx.AFFILIATE_CODE)) <> (CASE WHEN xx.COMPANY_CODE_JE_EXCEPTION_FLAG = '1'
THEN r.code_value3 ELSE r.code_value1 END)
THEN r_intercompany.code_value1
ELSE NVL(r_mga_acct.code_value2, xx.ACCOUNT_NUMBER) END
?"ABC">"A" returns .F.
?"ABC">"B" returns .F.
?"ABC">"a" returns .T.
This seems not an ASCII comparison (as in other common programming languages).
I'v searched MSDN, however Relational Operators only explained comparison across numeric type or boolean. String and Value Comparison didn't mention lt/gt/lte/gte operators.
Why string comparison work like this? What's VFP's internal rules for string lt/gt comparison?
Actually VFP's string comparison is not much different than in other languages (some languages -ie: C#- doesn't even let you use >=, >, <= and < with strings). However, in VFP there is one setting that affects string comparison and it is 'SET EXACT'. Other than that, VFP strings are case sensitive, thus "ABC" > "a" is false (alpha sort is based on their ASCII character codes).
With default SET EXACT OFF, strings are compared up to the length of right operand. ie:
"ABC" = "A"
is compared like:
"A" = "A"
thus they are equal, so "ABC" > "A" is false (and also "ABC" < "A" is false, "ABC" = "A" is true).
To recap, with default SET EXACT setting, which is OFF:
"ABC" = "A" (but "A" = "ABC" is false - compare up to right operand's length).
AND, the alphabetic ordering use ASCII code.
SET EXACT OFF && Default if not set explicitly
? "ABC" = "A" && .T.
? "ABC" > "A" && .F.
? "ABC" < "B" && .T.
SET EXACT ON
? "ABC" = "A" && .F.
? "ABC" > "A" && .T.
? "ABC" < "B" && .T.
A special note: If you are doing this equality check in SQL commands, then there the ANSI rules are applied and SET EXACT has no effect there. By default SET ANSI is OFF and "ABC" = "A" (and due to SQL rules, changing the operands' left and right sides wouldn't matter, thus "A" = "ABC" is also true). ie:
Select * from myTable where firstName = "A"
would return records having firstName "Anthony", "Alice" ... and so on.
There is a special == (exactly equal) operator in VFP that works independantly from SET EXACT or SET ANSI setting. Beware its behavior is different with regular commands and SQL commands. With regular commands it really means 'exactly equal' including the trailing spaces in both of the compared strings. ie:
? 'ABC' == 'ABC ' && false, regardless of SET EXACT
With SQL however (regardless of SET ANSI setting):
select * from myTable where myField == 'A'
select * from myTable where myField == 'A '
select * from myTable where myField == 'A '
all mean the same and searches for records whose myField content is A (whatever the field size is - trailing spaces on both sides of the comparison are ignored).
Note: I think you can request document topics to be added in stack overflow. VFP documentation has just started and no additions done yet, AFAIK.
Is there any function to replace NULL or empty space with special character in hive? when I execute the below statement it returns a semantic exception stating trim works only on string/nvarchar
CASE
WHEN TRIM(q.address) = '' OR q.address IS NULL THEN '?'
ELSE q.address END as address
Please help.
Use LENGTH() to check the length of the column value. It returns > 0, if there is some value else return 0 for empty or NULL value.
Also frame the column value in CASE WHEN ... END block
The final query may look like:
SELECT CASE WHEN LENGTH(address) > 0 THEN address ELSE '?' END AS address
FROM table_name;
Please refer Replace the empty or NULL value with specific value in HIVE query result
Hope this help you!!!
In order to replace nulls you can use Coalesce
Coalesce( q.address, '?')
But it seems your field adress is not of the proper type to use trim, can you show us the type of that field?
I have a return value from the database but I can not filter the null value.
With rs_receita
Do Until .EOF
Set noaux2 = xml.createElement("Superavit")
noaux2.Text = IIf(IsEmpty(!Superavit), "", CStr(!Superavit))
Call noaux.appendChild(noaux2)
.MoveNext
Loop
End With
Set rs_receita = Nothing
Avoid IIf for this scenario. IIf always evaluates both expressions. So when !Superavit is null, this will cause an error.
A single-line If statement, on the other hand, will only evaluate the expression to be executed. Combine that with the IsNull() function to reliably assign a database value to a variable if it's not null:
If IsNull(!Superavit) Then noaux2.Text = "" Else noaux2.Text = CStr(!Superavit)
In VB6 (I'm sorry!) you can force coercion to a string by appending an empty string to the field value.
Edit: Phew, this article is a blast from the past. It gives a bunch of NULL handling ideas for classic VB6: https://technet.microsoft.com/en-us/library/Aa175775(v=SQL.80).aspx
I believe either of the following will work:
noaux2.Text = "" & rs("Superavit")
OR
noaux2.Text = vbNullString & rs("Superavit")
I have a table which has city column having few records with state values as well-separated by comma.
There are other records without, as well. I want to take the state values for those present into a separate field called state.
How to do that? I tried the code below and it is saying "missing right parenthesis":
SELECT DECODE(ORA_CITY,
INSTR(ORA_CITY,',') > 0,
SUBSTR(ORA_CITY, INSTR(ORA_CITY, ','), LENGTH(ORA_CITY) ) ,
NULL) AS STATE
from ADDRESS
I don't know if you still need it but use CASE:
SELECT CASE
WHEN INSTR(ORA_CITY, '5') > 0 THEN
SUBSTR(ORA_CITY, INSTR(ORA_CITY, '5'), LENGTH(ORA_CITY))
ELSE
NULL
END STATE
FROM ADDRESS
Clearly you have not understood decode syntax.
Try the following:
SELECT DECODE(INSTR(ORA_CITY,','),
0,
NULL,
SUBSTR(ORA_CITY, INSTR(ORA_CITY, ','), LENGTH(ORA_CITY) )) AS STATE
FROM ADDRESS
The correct syntax is:
DECODE( expression , search , result [, search , result]... [,
default] ), where
expression is the value to compare.
search is the value that is compared against expression.
result is the value returned, if expression is equal to search.
default is optional. If no matches are found, the DECODE function will
return default. If default is omitted, then the DECODE function will
return null (if no matches are found).
Examples here and here
SELECT REGEX_REPLACE(ORA_CITY, '.*, *', '') AS STATE
FROM ADDRESS
WHERE ORA_CITY LIKE '%,%'
This uses regular expression to replace all upto the comma, and then maybe spaces with nothing. A WHERE included.