Trigger to concatenate two columns and update in new column

  • Hi,

    i have a table with columns

    ID

    F_Name

    L_name

    Full_Name

    my requirement is when user inserts id, F_name, L_name

    using trigger - concatenate F_Name + Lname and update in Full_Name column.

    it should occur when insert / update happens

    Please help me....

  • Rather than a trigger, can you change the table so that Full_Name is a calculated column?

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537
  • Mark-101232 (9/24/2012)


    Rather than a trigger, can you change the table so that Full_Name is a calculated column?

    +1

    A calculated column is far better here than a trigger for a number of reasons.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Agreed.

    For example:

    ALTER TABLE dbo.tablename

    DROP COLUMN Full_Name;

    ALTER TABLE dbo.tablename

    ADD Full_Name AS CAST(ISNULL(F_Name + ' ', '') + ISNULL(Lname, '') AS varchar(100))

    -- change 100 to whatever max length you want for the column

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • ScottPletcher (9/24/2012)


    Agreed.

    For example:

    ALTER TABLE dbo.tablename

    DROP COLUMN Full_Name;

    ALTER TABLE dbo.tablename

    ADD Full_Name AS CAST(ISNULL(F_Name + ' ', '') + ISNULL(Lname, '') AS varchar(100))

    -- change 100 to whatever max length you want for the column

    It would take a little more space but I recommend adding the PERSISTED hint.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (9/24/2012)

    It would take a little more space but I recommend adding the PERSISTED hint.

    For me, it depends, mostly on how and/or how often the full_name was used.

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

Viewing 6 posts - 1 through 6 (of 6 total)

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