Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Combining columns in sql.

  • 06-07-2006 4:40pm
    #1
    Registered Users Posts: 354 ✭✭


    Is there any way to merge 2 columns in sql. I'm using LEFT JOIN to combine the 2 tables which leaves a number of blank cells. I'ld like to use a value in the first table in those cells of the resulting table Date column.
    I tried renaming a column but I get Date and Date1 back and I'ld prefer to avoid using a new column for the sake of those few few entries.

    Also, a related question, I'ld like to put default text in the blank cells of another column of the resulting table.

    Can anybody suggest a possible solution?


Comments

  • Registered Users, Registered Users 2 Posts: 1,193 ✭✭✭liamo


    If by "Blank" you mean "Null", then
    Select IsNull(<SomeField>, '01/01/2000') as Date
    will return the value in SomeField unless it's NULL in which case '01/01/2000' will be returned in a column called "Date"


    Regards,

    Liam


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Commissar wrote:
    Is there any way to merge 2 columns in sql.
    Sure. It depends on what you're merging and what you consider merging to mean, though.
    I'm using LEFT JOIN to combine the 2 tables which leaves a number of blank cells. I'ld like to use a value in the first table in those cells of the resulting table Date column.

    IsNull is your friend here too.

    The basic usage is:
    IsNull(SomeField, SomeOtherFieldToUseOnlyWhenSomeFieldIsNull)

    Unfortunately, this is DB-specific. Going from Memory, in Oracle and DB2, the function is actually called NVL. MSSQL, Sybas both use IsNull. No idea about mySql, sorry.

    In MSSQL, there's an even more powerful field called COALESCE. THis can be given effectively any number of parameters (256 max?) and basically it goes from left-to-right and returns teh first non-null value from amongst them.

    So IsNull is just a simplified 2-parameter COALESCE :)

    Also, a related question, I'ld like to put default text in the blank cells of another column of the resulting table.
    As the previous poster suggested, IsNull / Nvl comes to the rescue again. Instead of a field in the second parameter, you just stick your default value. Remember it should be / has to be the smae datatype, so you don't want to do :

    IsNull(SomeIntegerField, "Empty")

    jc

    Can anybody suggest a possible solution?[/QUOTE]


  • Registered Users Posts: 354 ✭✭Commissar


    ISNULL did the trick. Thanks for the help Liamo and Bonkey.


Advertisement