how to find and update partial text in the SQL column2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?How to concatenate text from multiple rows into a single text string in SQL server?How can I do an UPDATE statement with JOIN in SQL?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableFind all tables containing column with specified name - MS SQL Server

Good for you! in Russian

Extra alignment tab has been changed to cr. } using table, tabular and resizebox

How much stiffer are 23c tires over 28c?

How did the power source of Mar-Vell's aircraft end up with her?

What does a stand alone "T" index value do?

Is "history" a male-biased word ("his+story")?

Is there a window switcher for GNOME that shows the actual window?

Offered promotion but I'm leaving. Should I tell?

Does "variables should live in the smallest scope as possible" include the case "variables should not exist if possible"?

How much attack damage does the AC boost from a shield prevent on average?

Exporting list of URLs

Should I tell my boss the work he did was worthless

Regex for certain words causes Spaces

Why does the negative sign arise in this thermodynamic relation?

Rejected in 4th interview round citing insufficient years of experience

A three room house but a three headED dog

How to create a hard link to an inode (ext4)?

Word for a person who has no opinion about whether god exists

Is there an equal sign with wider gap?

How do you like my writing?

They call me Inspector Morse

Why does Captain Marvel assume the people on this planet know this?

Can someone explain what is being said here in color publishing in the American Mathematical Monthly?

Could you please stop shuffling the deck and play already?



how to find and update partial text in the SQL column



2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?How to concatenate text from multiple rows into a single text string in SQL server?How can I do an UPDATE statement with JOIN in SQL?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableFind all tables containing column with specified name - MS SQL Server










0















I have some challenging column update in SQL. I need to update partial text in the column



 ID DESCRIPTION
1 Life Membership 10-Sep-2018 to 31-Dec-2018
2 Oklahoma City Section 10-Sep-2018 to 31-Dec-2018


Need to update



 ID DESCRIPTION
1 Life Membership 01-Jan-2019 to 31-Dec-2019
2 Oklahoma City Section 01-Jan-2019 to 31-Dec-2019


Membership or section always there then duration of the date. Please help me how to update this.










share|improve this question



















  • 1





    The problem here, really, is your design. This should be 3 different columns Description, StartDate and EndDate.

    – Larnu
    Mar 6 at 16:16















0















I have some challenging column update in SQL. I need to update partial text in the column



 ID DESCRIPTION
1 Life Membership 10-Sep-2018 to 31-Dec-2018
2 Oklahoma City Section 10-Sep-2018 to 31-Dec-2018


Need to update



 ID DESCRIPTION
1 Life Membership 01-Jan-2019 to 31-Dec-2019
2 Oklahoma City Section 01-Jan-2019 to 31-Dec-2019


Membership or section always there then duration of the date. Please help me how to update this.










share|improve this question



















  • 1





    The problem here, really, is your design. This should be 3 different columns Description, StartDate and EndDate.

    – Larnu
    Mar 6 at 16:16













0












0








0








I have some challenging column update in SQL. I need to update partial text in the column



 ID DESCRIPTION
1 Life Membership 10-Sep-2018 to 31-Dec-2018
2 Oklahoma City Section 10-Sep-2018 to 31-Dec-2018


Need to update



 ID DESCRIPTION
1 Life Membership 01-Jan-2019 to 31-Dec-2019
2 Oklahoma City Section 01-Jan-2019 to 31-Dec-2019


Membership or section always there then duration of the date. Please help me how to update this.










share|improve this question
















I have some challenging column update in SQL. I need to update partial text in the column



 ID DESCRIPTION
1 Life Membership 10-Sep-2018 to 31-Dec-2018
2 Oklahoma City Section 10-Sep-2018 to 31-Dec-2018


Need to update



 ID DESCRIPTION
1 Life Membership 01-Jan-2019 to 31-Dec-2019
2 Oklahoma City Section 01-Jan-2019 to 31-Dec-2019


Membership or section always there then duration of the date. Please help me how to update this.







sql sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 6 at 18:02







James123

















asked Mar 6 at 16:14









James123James123

4,28045154282




4,28045154282







  • 1





    The problem here, really, is your design. This should be 3 different columns Description, StartDate and EndDate.

    – Larnu
    Mar 6 at 16:16












  • 1





    The problem here, really, is your design. This should be 3 different columns Description, StartDate and EndDate.

    – Larnu
    Mar 6 at 16:16







1




1





The problem here, really, is your design. This should be 3 different columns Description, StartDate and EndDate.

– Larnu
Mar 6 at 16:16





The problem here, really, is your design. This should be 3 different columns Description, StartDate and EndDate.

– Larnu
Mar 6 at 16:16












3 Answers
3






active

oldest

votes


















0














This looks every row of column "description" for '10-Sep-2018' and replaces it to '01-Jan-2019'



update yourtable set description=replace(description,'10-Sep-2018','01-Jan-2019')



Note that as Larnu mentions, this is an incredibly bad design. This query will also change the second date too if it is '10-Sep-2018'






share|improve this answer























  • The dates are not static text. those date will change

    – James123
    Mar 6 at 16:21


















0














I'm not sure of your full dataset, but it sounds like you want to crop out and paste in. Here, I used CharIndex() to find the words 'Membership' and 'Section', then SubString to manipulate the strings based on the location of those words: (You can also use this logic to create a table where these values are in separate columns to improve your versatility)



Create Table #tbl
(
v VarChar(50)
)
Insert Into #tbl Values
('Life Membership 10-Sep-2018 to 31-Dec-2018'),
('Oklahoma City Section 10-Sep-2018 to 31-Dec-2018')


Query:



SELECT 
Case
When CHARINDEX('Membership', v) > 0 Then
Substring(v,1,CHARINDEX('Membership',v)+10) + '01-Jan-2019' + Substring(v,CHARINDEX('Membership',v)+22,14) + '9'
When CHARINDEX('Section', v) > 0 Then
Substring(v,1,CHARINDEX('Section',v)+7) + '01-Jan-2019' + Substring(v,CHARINDEX('Section',v)+19,14) + '9'
End As m
From #tbl


Result:



m
Life Membership 01-Jan-2019 to 31-Dec-2019
Oklahoma City Section 01-Jan-2019 to 31-Dec-2019





share|improve this answer























  • Sorry you confused `Life Membership 10-Sep-2018 to 31-Dec-2018' 1 row and Oklahoma City Section 10-Sep-2018 to 31-Dec-2018' 2rd row

    – James123
    Mar 6 at 18:02











  • James123, Regrets if I am off your mark. I was wondering if your dataset is such that the ending dates could be different from '31-12-2018' as well as if your first date would be a static change to '01-Jan-2019' as I have demonstrated... ie: does this script help you? If not, what is off the mark?

    – level3looper
    Mar 6 at 18:26


















0














There is no other way but to use replace() twice:



declare @fromdate varchar(11) = '10-Sep-2018';
declare @todate varchar(11) = '31-Dec-2018';
declare @newfromdate varchar(11) = '01-Jan-2019';
declare @newtodate varchar(11) = '31-Dec-2019';

create table tablename (id int, description varchar(255));

insert into tablename (id, description) values
(1, 'Life Membership 10-Sep-2018 to 31-Dec-2018'),
(2, 'Oklahoma City Section 10-Sep-2018 to 31-Dec-2018');

update tablename
set description =
replace(replace(description, @fromdate, @newfromdate), @todate, @newtodate);


See the demo






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55027593%2fhow-to-find-and-update-partial-text-in-the-sql-column%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This looks every row of column "description" for '10-Sep-2018' and replaces it to '01-Jan-2019'



    update yourtable set description=replace(description,'10-Sep-2018','01-Jan-2019')



    Note that as Larnu mentions, this is an incredibly bad design. This query will also change the second date too if it is '10-Sep-2018'






    share|improve this answer























    • The dates are not static text. those date will change

      – James123
      Mar 6 at 16:21















    0














    This looks every row of column "description" for '10-Sep-2018' and replaces it to '01-Jan-2019'



    update yourtable set description=replace(description,'10-Sep-2018','01-Jan-2019')



    Note that as Larnu mentions, this is an incredibly bad design. This query will also change the second date too if it is '10-Sep-2018'






    share|improve this answer























    • The dates are not static text. those date will change

      – James123
      Mar 6 at 16:21













    0












    0








    0







    This looks every row of column "description" for '10-Sep-2018' and replaces it to '01-Jan-2019'



    update yourtable set description=replace(description,'10-Sep-2018','01-Jan-2019')



    Note that as Larnu mentions, this is an incredibly bad design. This query will also change the second date too if it is '10-Sep-2018'






    share|improve this answer













    This looks every row of column "description" for '10-Sep-2018' and replaces it to '01-Jan-2019'



    update yourtable set description=replace(description,'10-Sep-2018','01-Jan-2019')



    Note that as Larnu mentions, this is an incredibly bad design. This query will also change the second date too if it is '10-Sep-2018'







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 6 at 16:18









    George MenoutisGeorge Menoutis

    2,803421




    2,803421












    • The dates are not static text. those date will change

      – James123
      Mar 6 at 16:21

















    • The dates are not static text. those date will change

      – James123
      Mar 6 at 16:21
















    The dates are not static text. those date will change

    – James123
    Mar 6 at 16:21





    The dates are not static text. those date will change

    – James123
    Mar 6 at 16:21













    0














    I'm not sure of your full dataset, but it sounds like you want to crop out and paste in. Here, I used CharIndex() to find the words 'Membership' and 'Section', then SubString to manipulate the strings based on the location of those words: (You can also use this logic to create a table where these values are in separate columns to improve your versatility)



    Create Table #tbl
    (
    v VarChar(50)
    )
    Insert Into #tbl Values
    ('Life Membership 10-Sep-2018 to 31-Dec-2018'),
    ('Oklahoma City Section 10-Sep-2018 to 31-Dec-2018')


    Query:



    SELECT 
    Case
    When CHARINDEX('Membership', v) > 0 Then
    Substring(v,1,CHARINDEX('Membership',v)+10) + '01-Jan-2019' + Substring(v,CHARINDEX('Membership',v)+22,14) + '9'
    When CHARINDEX('Section', v) > 0 Then
    Substring(v,1,CHARINDEX('Section',v)+7) + '01-Jan-2019' + Substring(v,CHARINDEX('Section',v)+19,14) + '9'
    End As m
    From #tbl


    Result:



    m
    Life Membership 01-Jan-2019 to 31-Dec-2019
    Oklahoma City Section 01-Jan-2019 to 31-Dec-2019





    share|improve this answer























    • Sorry you confused `Life Membership 10-Sep-2018 to 31-Dec-2018' 1 row and Oklahoma City Section 10-Sep-2018 to 31-Dec-2018' 2rd row

      – James123
      Mar 6 at 18:02











    • James123, Regrets if I am off your mark. I was wondering if your dataset is such that the ending dates could be different from '31-12-2018' as well as if your first date would be a static change to '01-Jan-2019' as I have demonstrated... ie: does this script help you? If not, what is off the mark?

      – level3looper
      Mar 6 at 18:26















    0














    I'm not sure of your full dataset, but it sounds like you want to crop out and paste in. Here, I used CharIndex() to find the words 'Membership' and 'Section', then SubString to manipulate the strings based on the location of those words: (You can also use this logic to create a table where these values are in separate columns to improve your versatility)



    Create Table #tbl
    (
    v VarChar(50)
    )
    Insert Into #tbl Values
    ('Life Membership 10-Sep-2018 to 31-Dec-2018'),
    ('Oklahoma City Section 10-Sep-2018 to 31-Dec-2018')


    Query:



    SELECT 
    Case
    When CHARINDEX('Membership', v) > 0 Then
    Substring(v,1,CHARINDEX('Membership',v)+10) + '01-Jan-2019' + Substring(v,CHARINDEX('Membership',v)+22,14) + '9'
    When CHARINDEX('Section', v) > 0 Then
    Substring(v,1,CHARINDEX('Section',v)+7) + '01-Jan-2019' + Substring(v,CHARINDEX('Section',v)+19,14) + '9'
    End As m
    From #tbl


    Result:



    m
    Life Membership 01-Jan-2019 to 31-Dec-2019
    Oklahoma City Section 01-Jan-2019 to 31-Dec-2019





    share|improve this answer























    • Sorry you confused `Life Membership 10-Sep-2018 to 31-Dec-2018' 1 row and Oklahoma City Section 10-Sep-2018 to 31-Dec-2018' 2rd row

      – James123
      Mar 6 at 18:02











    • James123, Regrets if I am off your mark. I was wondering if your dataset is such that the ending dates could be different from '31-12-2018' as well as if your first date would be a static change to '01-Jan-2019' as I have demonstrated... ie: does this script help you? If not, what is off the mark?

      – level3looper
      Mar 6 at 18:26













    0












    0








    0







    I'm not sure of your full dataset, but it sounds like you want to crop out and paste in. Here, I used CharIndex() to find the words 'Membership' and 'Section', then SubString to manipulate the strings based on the location of those words: (You can also use this logic to create a table where these values are in separate columns to improve your versatility)



    Create Table #tbl
    (
    v VarChar(50)
    )
    Insert Into #tbl Values
    ('Life Membership 10-Sep-2018 to 31-Dec-2018'),
    ('Oklahoma City Section 10-Sep-2018 to 31-Dec-2018')


    Query:



    SELECT 
    Case
    When CHARINDEX('Membership', v) > 0 Then
    Substring(v,1,CHARINDEX('Membership',v)+10) + '01-Jan-2019' + Substring(v,CHARINDEX('Membership',v)+22,14) + '9'
    When CHARINDEX('Section', v) > 0 Then
    Substring(v,1,CHARINDEX('Section',v)+7) + '01-Jan-2019' + Substring(v,CHARINDEX('Section',v)+19,14) + '9'
    End As m
    From #tbl


    Result:



    m
    Life Membership 01-Jan-2019 to 31-Dec-2019
    Oklahoma City Section 01-Jan-2019 to 31-Dec-2019





    share|improve this answer













    I'm not sure of your full dataset, but it sounds like you want to crop out and paste in. Here, I used CharIndex() to find the words 'Membership' and 'Section', then SubString to manipulate the strings based on the location of those words: (You can also use this logic to create a table where these values are in separate columns to improve your versatility)



    Create Table #tbl
    (
    v VarChar(50)
    )
    Insert Into #tbl Values
    ('Life Membership 10-Sep-2018 to 31-Dec-2018'),
    ('Oklahoma City Section 10-Sep-2018 to 31-Dec-2018')


    Query:



    SELECT 
    Case
    When CHARINDEX('Membership', v) > 0 Then
    Substring(v,1,CHARINDEX('Membership',v)+10) + '01-Jan-2019' + Substring(v,CHARINDEX('Membership',v)+22,14) + '9'
    When CHARINDEX('Section', v) > 0 Then
    Substring(v,1,CHARINDEX('Section',v)+7) + '01-Jan-2019' + Substring(v,CHARINDEX('Section',v)+19,14) + '9'
    End As m
    From #tbl


    Result:



    m
    Life Membership 01-Jan-2019 to 31-Dec-2019
    Oklahoma City Section 01-Jan-2019 to 31-Dec-2019






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 6 at 17:13









    level3looperlevel3looper

    726127




    726127












    • Sorry you confused `Life Membership 10-Sep-2018 to 31-Dec-2018' 1 row and Oklahoma City Section 10-Sep-2018 to 31-Dec-2018' 2rd row

      – James123
      Mar 6 at 18:02











    • James123, Regrets if I am off your mark. I was wondering if your dataset is such that the ending dates could be different from '31-12-2018' as well as if your first date would be a static change to '01-Jan-2019' as I have demonstrated... ie: does this script help you? If not, what is off the mark?

      – level3looper
      Mar 6 at 18:26

















    • Sorry you confused `Life Membership 10-Sep-2018 to 31-Dec-2018' 1 row and Oklahoma City Section 10-Sep-2018 to 31-Dec-2018' 2rd row

      – James123
      Mar 6 at 18:02











    • James123, Regrets if I am off your mark. I was wondering if your dataset is such that the ending dates could be different from '31-12-2018' as well as if your first date would be a static change to '01-Jan-2019' as I have demonstrated... ie: does this script help you? If not, what is off the mark?

      – level3looper
      Mar 6 at 18:26
















    Sorry you confused `Life Membership 10-Sep-2018 to 31-Dec-2018' 1 row and Oklahoma City Section 10-Sep-2018 to 31-Dec-2018' 2rd row

    – James123
    Mar 6 at 18:02





    Sorry you confused `Life Membership 10-Sep-2018 to 31-Dec-2018' 1 row and Oklahoma City Section 10-Sep-2018 to 31-Dec-2018' 2rd row

    – James123
    Mar 6 at 18:02













    James123, Regrets if I am off your mark. I was wondering if your dataset is such that the ending dates could be different from '31-12-2018' as well as if your first date would be a static change to '01-Jan-2019' as I have demonstrated... ie: does this script help you? If not, what is off the mark?

    – level3looper
    Mar 6 at 18:26





    James123, Regrets if I am off your mark. I was wondering if your dataset is such that the ending dates could be different from '31-12-2018' as well as if your first date would be a static change to '01-Jan-2019' as I have demonstrated... ie: does this script help you? If not, what is off the mark?

    – level3looper
    Mar 6 at 18:26











    0














    There is no other way but to use replace() twice:



    declare @fromdate varchar(11) = '10-Sep-2018';
    declare @todate varchar(11) = '31-Dec-2018';
    declare @newfromdate varchar(11) = '01-Jan-2019';
    declare @newtodate varchar(11) = '31-Dec-2019';

    create table tablename (id int, description varchar(255));

    insert into tablename (id, description) values
    (1, 'Life Membership 10-Sep-2018 to 31-Dec-2018'),
    (2, 'Oklahoma City Section 10-Sep-2018 to 31-Dec-2018');

    update tablename
    set description =
    replace(replace(description, @fromdate, @newfromdate), @todate, @newtodate);


    See the demo






    share|improve this answer





























      0














      There is no other way but to use replace() twice:



      declare @fromdate varchar(11) = '10-Sep-2018';
      declare @todate varchar(11) = '31-Dec-2018';
      declare @newfromdate varchar(11) = '01-Jan-2019';
      declare @newtodate varchar(11) = '31-Dec-2019';

      create table tablename (id int, description varchar(255));

      insert into tablename (id, description) values
      (1, 'Life Membership 10-Sep-2018 to 31-Dec-2018'),
      (2, 'Oklahoma City Section 10-Sep-2018 to 31-Dec-2018');

      update tablename
      set description =
      replace(replace(description, @fromdate, @newfromdate), @todate, @newtodate);


      See the demo






      share|improve this answer



























        0












        0








        0







        There is no other way but to use replace() twice:



        declare @fromdate varchar(11) = '10-Sep-2018';
        declare @todate varchar(11) = '31-Dec-2018';
        declare @newfromdate varchar(11) = '01-Jan-2019';
        declare @newtodate varchar(11) = '31-Dec-2019';

        create table tablename (id int, description varchar(255));

        insert into tablename (id, description) values
        (1, 'Life Membership 10-Sep-2018 to 31-Dec-2018'),
        (2, 'Oklahoma City Section 10-Sep-2018 to 31-Dec-2018');

        update tablename
        set description =
        replace(replace(description, @fromdate, @newfromdate), @todate, @newtodate);


        See the demo






        share|improve this answer















        There is no other way but to use replace() twice:



        declare @fromdate varchar(11) = '10-Sep-2018';
        declare @todate varchar(11) = '31-Dec-2018';
        declare @newfromdate varchar(11) = '01-Jan-2019';
        declare @newtodate varchar(11) = '31-Dec-2019';

        create table tablename (id int, description varchar(255));

        insert into tablename (id, description) values
        (1, 'Life Membership 10-Sep-2018 to 31-Dec-2018'),
        (2, 'Oklahoma City Section 10-Sep-2018 to 31-Dec-2018');

        update tablename
        set description =
        replace(replace(description, @fromdate, @newfromdate), @todate, @newtodate);


        See the demo







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 6 at 18:08

























        answered Mar 6 at 17:29









        forpasforpas

        16.4k3627




        16.4k3627



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55027593%2fhow-to-find-and-update-partial-text-in-the-sql-column%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

            Алба-Юлія

            Захаров Федір Захарович