Results 1 to 5 of 5

Thread: Combined two columns

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Combined two columns

    Hi anyone here knows how to combine the two columns in a table with the same data type: varchar(255).
    When you query the example below:
    Code:
    Vegetable               Fruits
    Potato                  NULL
    Squash                  NULL
    Cabbage                 NULL
    NULL                    Apple
    NULL                    Banana
    NULL                    Mango
    The Result will be:

    Code:
    Foods
    Potato
    Squash
    Cabbage
    Apple
    Banana
    Mango
    Is it possible? Thanks in advance

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Combined two columns

    Are you sure that only one column will have a value in any one record?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Re: Combined two columns

    How can I do this in an sql query? In case it is not possible to combine the two. In one column there is a condition like this: If Vegetable Column is Null use Fruit Column and if Fruit Column is Null use Vegetable Column.

    Code:
    CASE Vegetable WHEN NULL
       Fruit
    ELSE
       Vegetable
    END AS 'Foods'
    It returns null in my case. Don't know if it is possible? Or there is something I missed.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Combined two columns

    In your SELECT list, use ISNULL()

    Code:
    SELECT
    field2,
    field3,
    ISNULL(Vegetable, Fruit) AS Foods,
    field5,
    field6...

  5. #5
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: Combined two columns

    i usually combine col from different table through union

    Code:
    select itm as foods from vege
    union
    select itm as foods from fruit

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width