Results 1 to 17 of 17

Thread: [RESOLVED] Sql Server Columns Update Issue!

  1. #1

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Resolved [RESOLVED] Sql Server Columns Update Issue!

    Hi

    I have database table with two columns, I want to update some values by adding a text at the beginning and at the end of these values!?

    For example:
    Table name: food
    Column 1 name: food id
    Column 2 name: plant

    Original values in the table before adding text update:

    [food id] [plant]
    --------------------------
    [1] [APPLE FR]
    [2] [BANANA FR]
    [3] [TOMATO FR/VEG]


    Values in the table after adding text update:

    [food id] [plant]
    --------------------------
    [1] [GRANNY SMITH APPLE FRUIT]
    [2] [BLUEFIELD BANANA FRUIT]
    [3] [CHERRY TOMATO FRUIT/VEGETABLE]

    Note: there are spaces between the values in the column

    Anybody help me please with update scripts
    Regards…

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Sql Server Columns Update Issue!

    You can do something as simple as this:

    update food
    set plant = 'GRANNY SMITH APPLE FRUIT'
    where plant = 'APPLE FR'

    or if you know exact id then this:

    update food
    set plant = 'GRANNY SMITH APPLE FRUIT'
    where food_id = 1

  3. #3

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: Sql Server Columns Update Issue!

    Quote Originally Posted by RhinoBull
    You can do something as simple as this:

    update food
    set plant = 'GRANNY SMITH APPLE FRUIT'
    where plant = 'APPLE FR'

    or if you know exact id then this:

    update food
    set plant = 'GRANNY SMITH APPLE FRUIT'
    where food_id = 1
    thanks RhinoBull for your help
    But I'm looking to how add text to the values not replacing them
    Any idea please...

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Sql Server Columns Update Issue!

    update food
    set plant = 'GRANNY SMITH ' + plant + 'UIT'
    where food_id = 1

  5. #5

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: Sql Server Columns Update Issue!

    Quote Originally Posted by brucevde
    update food
    set plant = 'GRANNY SMITH ' + plant + 'UIT'
    where food_id = 1
    Thanks brucevde

    the scripts works fine...

    if possible could you please script case "food id 3"

    Regrads...

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: Sql Server Columns Update Issue!

    Couldn't you lift the code from brucevde's sample and modify it to suit your need? Just replace the value in the WHERE criteria.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: Sql Server Columns Update Issue!

    Quote Originally Posted by dee-u
    Couldn't you lift the code from brucevde's sample and modify it to suit your need? Just replace the value in the WHERE criteria.

    I’ve try it, but I couldn’t know how solve it
    Because the adding in the middle of the values!! My level of programming in SQL is beginner

  8. #8
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,495

    Re: Sql Server Columns Update Issue!

    So show us what you tried and why it is wrong
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  9. #9

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: Sql Server Columns Update Issue!

    Quote Originally Posted by GaryMazzone
    So show us what you tried and why it is wrong

    update food
    set plant = 'CHERRY TOMATO' + plant + 'UIT'/'ETABLE'
    where food_id = 3

  10. #10
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,495

    Re: Sql Server Columns Update Issue!

    What is it you want to write here? You are trying to do a division on strings that is not possible!

    maybe:

    sql Code:
    1. update food
    2. set plant = 'CHERRY TOMATO' + plant + 'UIT / ETABLE'
    3. where food_id = 3
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  11. #11

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: Sql Server Columns Update Issue!

    Quote Originally Posted by GaryMazzone
    What is it you want to write here? You are trying to do a division on strings that is not possible!

    maybe:

    sql Code:
    1. update food
    2. set plant = 'CHERRY TOMATO' + plant + 'UIT / ETABLE'
    3. where food_id = 3
    I’m really confused, sorry but I didn’t understand what you mean…

    As I have said I’m newbie in the SQL …

    Anyway thanks for your support

  12. #12
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,495

    Re: Sql Server Columns Update Issue!

    What you wrote means

    Set plant = 'CHERRY TOMATO' + plant + 'UIT'/'ETABLE'

    That the string plant Add CHERRY TOMATO before the value in plant then add UIT after the value of plant.

    Next in the statment is a division (/) then the string 'ETABLE' which mean that the value 'CHERRY TOMATO' + plant + 'UIT' (which is a string) and divide it by the value 'ETABLE' (which is also a string). That is not allowed.

    I assume (and of course I can be wrong here) you want to and the string
    'UIT / ETABLE' after the current value in the plant field.

    Is that correct?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  13. #13
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Sql Server Columns Update Issue!

    You wrote -

    'UIT' / 'ETABLE' in you code. The / character is the Division character for dividing numbers (4 / 2 = 2) so you would in effect be trying to divide words.

    Try this instead -

    UPDATE food
    SET Plant = 'CHERRY ' + left(Plant,9) + 'UIT/' + right(Plant,3) + 'ETABLE'
    WHERE food_id = 3
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  14. #14
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,495

    Re: Sql Server Columns Update Issue!

    OK I just looked at you first post as to what you want that is more dificult

    CHERRY TOMATO FRUIT/VEGETABLE is what you want and what you have is
    TOMATO FR/VEG

    What is the database you are using to store the data in it will make a difference in how you do this.

    You will need to add CHERRY to the first part of the string then use a substring to add the UIT then andother substring to add the ETABLE to the ending
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  15. #15

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: Sql Server Columns Update Issue!

    Quote Originally Posted by NeedSomeAnswers
    You wrote -

    'UIT' / 'ETABLE' in you code. The / character is the Division character for dividing numbers (4 / 2 = 2) so you would in effect be trying to divide words.

    Try this instead -

    UPDATE food
    SET Plant = 'CHERRY ' + left(Plant,9) + 'UIT/' + right(Plant,3) + 'ETABLE'
    WHERE food_id = 3

    That’s what I’m looking for
    Many thanks NeedSomeAnswers

  16. #16

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: Sql Server Columns Update Issue!

    Quote Originally Posted by GaryMazzone
    OK I just looked at you first post as to what you want that is more dificult

    CHERRY TOMATO FRUIT/VEGETABLE is what you want and what you have is
    TOMATO FR/VEG

    What is the database you are using to store the data in it will make a difference in how you do this.

    You will need to add CHERRY to the first part of the string then use a substring to add the UIT then andother substring to add the ETABLE to the ending

    I appreciate your effort and time for helping…

    Best regards

  17. #17
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: [RESOLVED] Sql Server Columns Update Issue!

    No Problem
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



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