I'm doing some converting from Oracle to MSSQL and I was reading a guide by Oracle on B Supported SQL Syntax and Functions.
I noticed it was stated that there is a NOT NVL function (and its MSSQL equivalent was IS NOT NULL).
I'm compiling a list for my colleagues so we can have a one-stop resource for syntax and supported functions, am I correct in assuming that NOT NVL works like so:
There are 3 columns, name, location, loves_marmite
Andrew | UK | Yes
NOT NVL(loves_marmite, 'Nope')
So the data displayed would be:
Andrew | UK | Nope
I just don't get why it would be listed as an Oracle Function when it's just a logic issue, and what's more is that Oracle has IS NULL and IS NOT NULL.
I'm sorry I'm just looking for some clarification before I pass this document on to my colleagues.
EDIT : If possible would someone have a comprehensive list of function and syntax differences between the two platforms?
Check NVL2(param1, param2, param3) function.
If param1 is NOT (NULL or EMPTY STRING) it returns param2 else returns param3.
You could write:
NVL2(loves_marmite, 'Nope', something_else)
Also, see this answer for a list of null-related functions in Oracle
First, please see the isNull function. But Oracle may be trying to tell you to replace the NVL funcionality with a case;
SELECT CASE WHEN Foo IS NOT NULL THEN bar
ELSE BLA
END
Related
I am trying to connect a Filemaker DB to Firebird SQL DB in both ways import to FM and export back to Firebird DB.
So far it works using the MBS Plug-in but FM 13 Pro canot handle NULL.
That means that for example Timestamp fields that are empty (NULL) produce a "0" value.
Thats means in Time something like 01.01.1889 00:00:00.
So my idea was to simply ignore fields containing NULL.
But here my poor knowlege stops.
First I thought I can do this with WHERE, but this is ignoring whole records sets:
SELECT * FROM TABLE WHERE FIELD IS NOT NULL
Also I tried to filter it later on like this:
If (IsEmpty (MBS("SQL.GetFieldAsDateTime"; $command; "FIELD") ) = 0 ; MBS("SQL.GetFieldAsDateTime"; $command; "FIELD"))
With no result either.
This is a direct answer to halfbit's suggestion, which is correct but not for this SQL dialect. In a query to provide a replacement value when a field is NULL you need to use COALESCE(x,y). Where if X is null, Y will be used, and if Y is null then the field is NULL. Thats why it is common for me to use it like COALESCE(table.field,'') such that a constant is always outputted if table.field happens to be NULL.
select COALESCE(null,'Hello') as stackoverflow from rdb$database
You can use COALESCE() for more than two arguments, I just used two for conciseness.
I dont know the special SQL dialect, but
SELECT field1, field2, value(field, 0), ...FROM TABLE
should help you:
value gives the first argument, ie, your field if it is NOT NULL or the second argument if it is.
(Other than using a UDF) Any REGEXP-In-SQL support for DB2 9.7 ?
Starting with DB2 11.1 there is built-in regex support. One of the new function is REGEXP_SUBSTR and there are some more.
SELECT REGEXP_SUBSTR('hello to you', '.o',1,1)
FROM sysibm.sysdummy1
I'm komikoni(Keisuke Konishi).
I created the regular expression function (UDF) which does not exist in db2.
The UDF using the SQL/XML(Xquery).
You can easily install.
List of regular expressions provide UDF
REG_MATCHES
provides Coincidence existence ( Scalar )
REG_REPLACE
string substitution ( Scalar )
REG_COUNT
number of matches retrieved ( Scalar )
REG_POSITION
match position acquisition ( Scalar )
REG_SUBSTR
gets a string matching ( Scalar )
REG_SUBSTR_TABLE
list of matching string information ( Table )
REG_TOKENIZE_TABLE
list of mismatched string information (divided by a separator string) ( Table )
REG_ALLTOKEN_TABLE
list of mismatch string and matching string information ( Table )
Scripts can be downloaded from here.
(Sorry in Japanese)
https://www.ibm.com/developerworks/jp/data/library/db2/j_d-regularexpression/
(English : Machine translation Script : The last of a Japanese page)
I look forward to your feedback and comments.
The real answer is that DB2 does support regular expression since PureXML was added (v9.7 included) via xQuery with the matches function.
For example:
db2 "with val as (
select t.text
from texts t
where xmlcast(xmlquery('fn:matches(\$TEXT,''^[A-Za-z 0-9]*$'')') as integer) = 0
)
select * from val"
For more information:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/xqrfnmat.html
That works fine except for DB2 z/OS - in DB2 v10 z/OS you must use PASSING as follows
with val as (
select t.text
from texts t
where xmlcast(xmlquery('fn:matches($v,"^[A-Za-z 0-9]*$")'
PASSING t.text as "v" ) as integer) = 0
)
select * from val
There is no built-in support for regular expressions in DB2 9.7.
The only way is using UDFs or table functions as described in the article 'OMG Ponies' added in the comment.
#dan1111: I do not appreciate my post being edited, especially if people can't read the question correctly. The OP asked Any REGEXP-In-SQL support for DB2 9.7
SQL is not XQuery !!!
Sorry, don't delete the text of my 100% correct answer. You can add a comment or write your own answer.
Are there any nvl() equivalent functions in SQL?
Or something close enough to be used in the same way in certain scenarios?
UPDATE:
no if statementsno case statementsno isnullno coalesce
select nvl (purge_date,"SODIUFOSDIUFSDOIFUDSF") from id_rec where id=36581;
(expression)
SODIUFOSDIUFSDOIFUDSF
1 row(s) retrieved.
select isnull (purge_date,"SODIUFOSDIUFSDOIFUDSF") from id_rec where id=36581;
674: Routine (isnull) can not be resolved.
Error in line 1
Near character position 8
select coalesce (purge_date,"SODIUFOSDIUFSDOIFUDSF") from id_rec where id=36581;
674: Routine (coalesce) can not be resolved.
Error in line 1
Near character position 8
select decode(purge_date, NULL, "01/01/2009", purge_date) from id_rec where id=74115;
800: Corresponding types must be compatible in CASE expression.
Error in line 1
Near character position 57
ISNULL (for a single replace)
or
COALESCE (Returns the first nonnull expression among its arguments.)
SQL Server:
IsNull or COALESCE
http://msdn.microsoft.com/en-us/library/ms184325.aspx
Sybase:
isnull function
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.blocks/html/blocks/blocks162.htm
Postgres:
I couldn't find one though haven't fully checked. Suggests to select where IS NULL and build from here
http://archives.postgresql.org/pgsql-sql/1998-06/msg00142.php
DB2 - COALESCE
http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/admin/r0000780.htm
You seem to be using Informix.
AFAIK, there is DECODE there:
DECODE(field, NULL, 'it is null, man', field) should give you same result as NVL(field, 'it is null, man')
Please post exact name and version of the RDBMS you are using.
The problem with your DECODE statement that is generating the 800 error is simple. '01/01/2009' is being treated as a string, and it's actually the 4th argument that generates the error.
Appreciate that the input and output of a DECODE statement can be different data-types, so the engine requires you to be more explicit in this case. (Do you want purge_date cast as a string or the string '01/01/2009', or the string argument parsed as a date or the original date? There's no way for the engine to know.
Try this:
SELECT DECODE(purge_date, NULL, '01/01/2009'::DATE, purge_date)
You could also write that 3rd argument as:
DATE('01/01/2009')
MDY(1,1,2009)
depending on version and personal preference.
What is MySQL equivalent of the Nz Function in Microsoft Access? Is Nz a SQL standard?
In Access, the Nz function lets you return a value when a variant is null. Source
The syntax for the Nz function is:
Nz ( variant, [ value_if_null ] )
The COALESCE() function does what you describe. It's standard SQL and it should be supported in all SQL databases.
The IFNULL() function is not standard SQL. Only some brands of databases support this function.
COALESCE does just what the OP is asking for, as does IFNULL:
SELECT Nz(MightBeNullVar, 0) FROM ... (MS Access version)
SELECT COALESCE(MightBeNullVar, 0) FROM ... (MySQL version)
SELECT IFNULL(MightBeNullVar, 0) FROM ... (MySQL version)
The difference is the COALESCE can search through multiple variables and return the first non-null one:
SELECT COALESCE(MightBeNullVar, MightAlsoBeNullVar, CouldBeNullVar, 0) FROM ... (MySQL version)
each of these will return a 0 (zero) if none of the values have a set value (are null).
The IFNULL is (pretty meaninglessly) faster. There's probably other better things to optimize in your query before bothering with IFNULL vs COALESCE issues. If you have multiple things to check, use COALESCE. If you only have a single value to check, use IFNULL.
You might want to look at IFNULL or COALESCE. If I recall correctly, IFNULL works for MySQL.
Have look to the null safe operator <=>
Maybe it could help :
http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html#operator_equal-to
Perhaps Knowing what MS Access NZ() Function actually does would be helpful (prior to answering with completely invalid suggestions). The NZ() Function test for Null and Replaces the Null with an empty string, a Zero or optionally a value that the user enters.
COALESCE doesn't even come close, in fact it returns a Null if no none Null values in a 'List???'
IFNULL() Function is what you're looking for.
http://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html
I found in MYSQL and apparently other database engines that there is a "greatest" function that can be used like: greatest(1, 2, 3, 4), and it would return 4. I need this, but I am using IBM's DB2. Does anybody know of such an equivalent function, even if it only accepts 2 parameters?
I found somewhere that MAX should do it, but it doesn't work... it only works on selecting the MAX of a column.
If there is no such function, does anybody have an idea what a stored procedure to do this might look like? (I have no stored procedure experience, so I have no clue what DB2 would be capable of).
Why does MAX not work for you?
select max(1,2,8,3,1,7) from sysibm.sysdummy1
gives me
1
---------------
8
1 record(s) selected.
As Dave points out, MAX should work as it's overloaded as both a scalar and a column function (the scalar takes 2 or more arguments). This is the case in DB2 for LUW, DB2 for z/OS and DB2 for i5/OS. What exact version and platform of DB2 are you using, and what is the exact statement you are using? One of the requirements of the scalar version of MAX is that all the arguments are "compatible" - I suspect there may be a subtle type difference in one or more of the arguments you're passing to the function.
On Linux V9.1, the "select max (1,2,3) ..." gives -
SQL0440N No authorized routine named "MAX" of type "FUNCTION" having
compatible arguments was found. SQLSTATE=42884
It is a scalar function requiring either a single value or a single column name. On z/os, it behaves differently.
However, It does work as expected on Linux 9.5.
Two options:
What about sorting the column in descending and grabbing the top 1 row?
According to my "SQL Pocket Guide", MAX(x) returns the greatest value in a set.
UPDATE: Apparently #1 won't work if you are looking at columns.
It sounds crazy, but no such function exists in DB2, at least not in version 9.1. If you want to select the greater of two columns, it would be best to use a case expression.
You can also define your own max function. For example:
create function importgenius.max2(x double, y double)
returns double
language sql
contains sql
deterministic
no external action
begin atomic
if y is null or x >= y then return x;
else return y;
end if;
end
Defining the inputs and outputs as doubles lets you take advantage of type promotion, so this function will also work for integers. The "deterministic" and "no external action" statements help the database engine optimize use of the function.
If you want another max function to work for character inputs, you'll have to give it another name.
Please check with following query:
select * from table1 a,
(select appno as sub_appno,max(sno) as sub_maxsno from table1 group by appno) as tab2
where a.appno =tab2.sub_appno and a.sno=tab2.sub_maxsno