Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « What is the Dual table in Oracle and why do I need it?Creating MongoDB as a service on Windows 8 »
    comments

    If you ever have to start working with Oracle you have to keep in mind that NULLs and blank values don't work exactly the same as in SQL Server. Let's take a look at some examples

    Create this table and insert some rows

    1. CREATE TABLE TestNull(Col2 VARCHAR(100));
    2. INSERT INTO TestNull VALUES(NULL);
    3. INSERT INTO TestNull VALUES('Bla');
    4. INSERT INTO TestNull VALUES('');
    5. INSERT INTO TestNull VALUES(' ');

    As you can see we inserted four rows, one row is null, one row is blank, one row has a space and one row has Bla.

    Now let's run the following query

    1. SELECT Col2,
    2.   NVL(Col2,'EmptyOrNull') a,
    3.   COALESCE(Col2,'EmptyOrNull') b,
    4.   ASCII(col2) c
    5. FROM TestNull;

    Here are the results in a html table

    COL2 A B C
    null EmptyOrNull EmptyOrNullnull
    Bla Bla Bla 66
    null EmptyOrNull EmptyOrNullnull
    32

    Here is an image of the same results
    Oracle SQL Developer Results

    See what happened, Oracle changed the blanks to NULLs.

    We can easily test this theory, let's create a table with a column that has a not null constraint

    1. CREATE TABLE TestNull2(Col2 VARCHAR(100) NOT NULL);

    Now of course if you try to insert a NULL it will blow up

    Here is the error
    SQL Error: ORA-01400: cannot insert NULL into ("SYSTEM"."TESTNULL2"."COL2")
    01400. 00000 - "cannot insert NULL into (%s)"

    Inserting Bla works without a problem

    1. INSERT INTO TestNull2 VALUES('Bla');

    What about a blank, what will happen now?

    SQL Error: ORA-01400: cannot insert NULL into ("SYSTEM"."TESTNULL2"."COL2")
    01400. 00000 - "cannot insert NULL into (%s)"

    As you can see the blank gets converted to a NULL and you get the same error. This is very different from SQL Server.

    Will a space succeed?

    A space is no problem.

    Coalesce differences
    Just be aware that coalesce won't work the same either. Oracle doesn't have isnull but it has the nvl function instead

    Run the following two statements

    1. SELECT NVL('','No') AS a
    2. FROM dual;

    In both cases you are getting No back from the function, as you can see a blank is treated as null.

    Be aware of these differences between Oracle and SQL Server, you could have some strange results back from queries if you assume it works the same

    We will take a look at that strange table dual in another post

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    2774 views
    InstapaperVote on HN

    1 comment

    Comment from: Ted Krueger (onpnt) [Member]
    Ted Krueger (onpnt) Awesome post! May seem small but that is a major difference and requires a lot of different thinking both when storing data and writing code against it.
    01/08/13 @ 18:12

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)