How to set a null for a new column in DB2?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • winniewang
    New Member
    • Apr 2007
    • 4

    How to set a null for a new column in DB2?

    Hi everyone,
    Who can tell me the syntax like "NULL AS" of SQL Server query in DB2?
    I want to write a query like this(SQL Server query):
    SELECT ResellerKey, NULL AS CustomerKe
    FROM A
    UNION
    SELECT NULL AS ResellerKey, CustomerKe
    FROM B
    How to set a null for a new column in DB2?
    WHo can tell me?
    Any response is appreciate.

    Thanks^_^
    Winnie
  • Snib
    New Member
    • Mar 2007
    • 24

    #2
    Originally posted by winniewang
    Hi everyone,
    Who can tell me the syntax like "NULL AS" of SQL Server query in DB2?
    I want to write a query like this(SQL Server query):
    SELECT ResellerKey, NULL AS CustomerKe
    FROM A
    UNION
    SELECT NULL AS ResellerKey, CustomerKe
    FROM B
    How to set a null for a new column in DB2?
    WHo can tell me?
    Any response is appreciate.

    Thanks^_^
    Winnie
    Winnie,

    You should be able to do this with the follow Db2 SQL:

    SELECT ResellerKey, ''
    FROM A
    UNION
    SELECT '' , CustomerKe
    FROM B

    If '' does not work then use ' ' - basically the same but with a space between the singel quotes. If ResellerKey or CustomerKe are numeric then just replace '' with 0.

    Regards

    Snib

    Comment

    • kalexin
      New Member
      • Apr 2007
      • 20

      #3
      Originally posted by Snib
      Winnie,

      You should be able to do this with the follow Db2 SQL:

      SELECT ResellerKey, ''
      FROM A
      UNION
      SELECT '' , CustomerKe
      FROM B

      If '' does not work then use ' ' - basically the same but with a space between the singel quotes. If ResellerKey or CustomerKe are numeric then just replace '' with 0.

      Regards

      Snib

      Hi Snib,
      The null string '' is not a "true" NULL value in DB2. Therefore the above DB2 statement is not equivalent to the SQL server statement.
      However, the NULLIF function can be used to "fool" the database and return a true null instead of a null string.


      SELECT ResellerKey, NULLIF(1,1)
      FROM A
      UNION
      SELECT NULLIF(1,1) , CustomerKe
      FROM B

      Comment

      Working...