update 1 col with values of col2 and 3

  • ChrisM@home (1/1/2012)


    UPDATE Mytable SET col3 = col1 + " ," + col2

    If col1 or col2 are a numeric datatype, I'd explicitly cast.

    If either col1 or col2 are nullable, then use ISNULL with an appropriate replacement.

    Post removed.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • stebennettsjb (1/1/2012)


    Hi all,

    simple one for you experts im sure. I have a table with values in col1 and col2

    I want to put both values into a new col with text say col1 + " ," + col2

    thanks

    S

    I can't think of a business need to do this.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • stebennettsjb (1/1/2012)


    Hi Chris,

    Thats got it working! thank you,

    Welsh Corgi sorry for not explaining myself very well, I shall follow your links guide from now on.. Your free to breath a sigh of relief and move on from this thread.. i fear ive broken all the rules on this one.. apologies

    Thanks to both of you for your help..

    S

    No problem - sorry my first post missed the double-quotes. You can read this in BOL - see quoted identifiers and the SET statement to change the default behaviour.

    It doesn't matter if this is coursework or self-set homework, folks are here to help, although some may make you work a little harder than others 😉


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • Welsh Corgi (1/1/2012)


    ChrisM@home (1/1/2012)


    stebennettsjb (1/1/2012)


    ohh sorry i was using your examples!

    read the links when you replied.. Sorry for not adding them before :unsure:

    Use single quotes ste (Steve?) - double quotes indicate an object such as a column.

    Like this:

    SET col3 = CAST(col1 AS VARCHAR(5))+ ',' + CAST(col2 AS VARCHAR(5))

    How can you tell him what to do if you do not not know what the structure is?

    If you do not know what the DDL is then you are talking garbage and you are contributing to the problem! 😛

    A reasonably educated guess is often sufficient - but I'll readily confess to talking garbage from time to time. Usually for beer or sexual favours.


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • Welsh,

    While im thankful to any replies to my questions, and i have apologised for not asking it in the "correct" manner. Please.. please feel free to never reply to anything i might post again.

    If ive annoyed this much (no idea how my question and responses did. But i cant sit here and write sorry every 12 seconds to try and appease you), feel free to leave your comments and not edit them out, You clearly feel passionately about this so go for it.

    Chris,

    Thank you again!

  • Thank you for correcting me!

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • yee gods man,

    im playing around with sql and lightswitch trying to pick up some dev skills (clearly just beginning), as just working though books isnt doing it for me..

    Im a "accidental dba" trying to learn and improve.. Not someone looking for answers to some coursework.

    I mean really who told you, you have a Professor X like abilities to read the minds of posters :ermm:.. or why you feel your pretentious holier than thou attitude works well on the net is beyond rationale. But seriously your coming over as bit of a d!ÂŁk.. You may have a point about "cheating in the classroom" but it really helps ones argument to know WTF there talking about in the first place before starting on a holy crusade.

    Ill leave it at that and hope you do to.. but sadly i expect to see the ramblings of what if this thread is anything to go by, is a sanctimonious buffoon shortly.. :unsure:

  • ...

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • ...

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • ?

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • I think you all should take a deep breath and consider stop posting to this thread.

    If you don't have anything nice to say, don't say anything at all.

    You're not going to earn anything positive from calling names.

    -- Gianluca Sartori

  • I missed all the ranting, but thought I might try to add something useful. During the thread someone brought up not seeing a business need for this. The one that comes to mind to me is concantinating names - lastname + ', ' + firstname.

    This is really a function of the front end display and should be done there instead of the database.

  • Yes you are right about there being a business need to concatenate.

    I misread his question but since the values are numbers that are being concatenated I was wondering about the business need.

    Perhaps for practice. 🙂

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • my point was that, sure, there COULD be a buisness need, but it's really a formatting issue and should be done at display time rather than stored. :hehe:

  • Uripedes Pants (1/3/2012)


    my point was that, sure, there COULD be a buisness need, but it's really a formatting issue and should be done at display time rather than stored. :hehe:

    As a general rule, this is a good one. But that's what it is - a general rule. In this particular case there's insufficient information about the business use of the result set to reach a conclusion.


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

Viewing 15 posts - 16 through 29 (of 29 total)

You must be logged in to reply to this topic. Login to reply