Map values in column to values in SQL





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I want to replace certain values with corresponding values in SQL. I cannot create a cross-reference table. The values can be specified in 'txt' or 'properties' file in my application



I looked at CASE statement but I have 200 replacements to make for which CASE is not sufficient



SELECT dummy FROM TestTable;


In my answer if I get LOT_NUMBER I want to replace it with Lot Number or if I get LOT_NUMBER_XREF I want to replace it with Lot Number Cross-Reference



dummy
LOT_NUMBER
LOT_NUMBER_XREF


to be replaced with



dummy
Lot Number
Lot Number Cross-Reference









share|improve this question




















  • 3





    Give us some sample table data and the expected result - all as formatted text, not images. Simplify! stackoverflow.com/help/mcve

    – jarlh
    Jan 3 at 20:05






  • 1





    Create a cross-reference table with columns for the old and new values, then join to it.

    – Eric Brandt
    Jan 3 at 20:06













  • @EricBrandt - I cannot create a cross-reference table. The values can be specified in 'txt' or 'properties' file in my application. I just added info to question.

    – raj247
    Jan 3 at 20:10






  • 1





    @raj247: Then create a table and fill it with the values from the files.

    – sticky bit
    Jan 3 at 20:12






  • 1





    Why should I provide an MCVE for what seems to me to be a very simple SQL query?

    – Eric Brandt
    Jan 3 at 20:15


















0















I want to replace certain values with corresponding values in SQL. I cannot create a cross-reference table. The values can be specified in 'txt' or 'properties' file in my application



I looked at CASE statement but I have 200 replacements to make for which CASE is not sufficient



SELECT dummy FROM TestTable;


In my answer if I get LOT_NUMBER I want to replace it with Lot Number or if I get LOT_NUMBER_XREF I want to replace it with Lot Number Cross-Reference



dummy
LOT_NUMBER
LOT_NUMBER_XREF


to be replaced with



dummy
Lot Number
Lot Number Cross-Reference









share|improve this question




















  • 3





    Give us some sample table data and the expected result - all as formatted text, not images. Simplify! stackoverflow.com/help/mcve

    – jarlh
    Jan 3 at 20:05






  • 1





    Create a cross-reference table with columns for the old and new values, then join to it.

    – Eric Brandt
    Jan 3 at 20:06













  • @EricBrandt - I cannot create a cross-reference table. The values can be specified in 'txt' or 'properties' file in my application. I just added info to question.

    – raj247
    Jan 3 at 20:10






  • 1





    @raj247: Then create a table and fill it with the values from the files.

    – sticky bit
    Jan 3 at 20:12






  • 1





    Why should I provide an MCVE for what seems to me to be a very simple SQL query?

    – Eric Brandt
    Jan 3 at 20:15














0












0








0








I want to replace certain values with corresponding values in SQL. I cannot create a cross-reference table. The values can be specified in 'txt' or 'properties' file in my application



I looked at CASE statement but I have 200 replacements to make for which CASE is not sufficient



SELECT dummy FROM TestTable;


In my answer if I get LOT_NUMBER I want to replace it with Lot Number or if I get LOT_NUMBER_XREF I want to replace it with Lot Number Cross-Reference



dummy
LOT_NUMBER
LOT_NUMBER_XREF


to be replaced with



dummy
Lot Number
Lot Number Cross-Reference









share|improve this question
















I want to replace certain values with corresponding values in SQL. I cannot create a cross-reference table. The values can be specified in 'txt' or 'properties' file in my application



I looked at CASE statement but I have 200 replacements to make for which CASE is not sufficient



SELECT dummy FROM TestTable;


In my answer if I get LOT_NUMBER I want to replace it with Lot Number or if I get LOT_NUMBER_XREF I want to replace it with Lot Number Cross-Reference



dummy
LOT_NUMBER
LOT_NUMBER_XREF


to be replaced with



dummy
Lot Number
Lot Number Cross-Reference






mysql sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 22:39









GMB

21.9k61128




21.9k61128










asked Jan 3 at 20:03









raj247raj247

97110




97110








  • 3





    Give us some sample table data and the expected result - all as formatted text, not images. Simplify! stackoverflow.com/help/mcve

    – jarlh
    Jan 3 at 20:05






  • 1





    Create a cross-reference table with columns for the old and new values, then join to it.

    – Eric Brandt
    Jan 3 at 20:06













  • @EricBrandt - I cannot create a cross-reference table. The values can be specified in 'txt' or 'properties' file in my application. I just added info to question.

    – raj247
    Jan 3 at 20:10






  • 1





    @raj247: Then create a table and fill it with the values from the files.

    – sticky bit
    Jan 3 at 20:12






  • 1





    Why should I provide an MCVE for what seems to me to be a very simple SQL query?

    – Eric Brandt
    Jan 3 at 20:15














  • 3





    Give us some sample table data and the expected result - all as formatted text, not images. Simplify! stackoverflow.com/help/mcve

    – jarlh
    Jan 3 at 20:05






  • 1





    Create a cross-reference table with columns for the old and new values, then join to it.

    – Eric Brandt
    Jan 3 at 20:06













  • @EricBrandt - I cannot create a cross-reference table. The values can be specified in 'txt' or 'properties' file in my application. I just added info to question.

    – raj247
    Jan 3 at 20:10






  • 1





    @raj247: Then create a table and fill it with the values from the files.

    – sticky bit
    Jan 3 at 20:12






  • 1





    Why should I provide an MCVE for what seems to me to be a very simple SQL query?

    – Eric Brandt
    Jan 3 at 20:15








3




3





Give us some sample table data and the expected result - all as formatted text, not images. Simplify! stackoverflow.com/help/mcve

– jarlh
Jan 3 at 20:05





Give us some sample table data and the expected result - all as formatted text, not images. Simplify! stackoverflow.com/help/mcve

– jarlh
Jan 3 at 20:05




1




1





Create a cross-reference table with columns for the old and new values, then join to it.

– Eric Brandt
Jan 3 at 20:06







Create a cross-reference table with columns for the old and new values, then join to it.

– Eric Brandt
Jan 3 at 20:06















@EricBrandt - I cannot create a cross-reference table. The values can be specified in 'txt' or 'properties' file in my application. I just added info to question.

– raj247
Jan 3 at 20:10





@EricBrandt - I cannot create a cross-reference table. The values can be specified in 'txt' or 'properties' file in my application. I just added info to question.

– raj247
Jan 3 at 20:10




1




1





@raj247: Then create a table and fill it with the values from the files.

– sticky bit
Jan 3 at 20:12





@raj247: Then create a table and fill it with the values from the files.

– sticky bit
Jan 3 at 20:12




1




1





Why should I provide an MCVE for what seems to me to be a very simple SQL query?

– Eric Brandt
Jan 3 at 20:15





Why should I provide an MCVE for what seems to me to be a very simple SQL query?

– Eric Brandt
Jan 3 at 20:15












3 Answers
3






active

oldest

votes


















1














If you have many different values to replace, you should create a table to store the mapping of old values to new values. Then you can join that table in your query.



The below SQL replaces the old value with the new value (or leaves it as is if no translation was defined) :



SELECT 
COALESCE(m.new_value, t.value)
FROM
table t
LEFT JOIN mapping_table m
ON t.value = m.old_value


For a limited number of values to translate you can CASE. or maybe a CTE with UNION :



WITH mapping_table AS (
SELECT 'LOT_NUMBER' AS old_value, 'Lot Number' AS new_value
SELECT 'LOT_NUMBER_XREF', 'Lot Number Cross-Reference'
)
SELECT
COALESCE(m.new_value, t.value)
FROM
table t
LEFT JOIN mapping_table m
ON t.value = m.old_value





share|improve this answer

































    1














    If you can't use cross-reference table, use (you'll have to generate it in your app) sub-query or cte instead!
    CTE:



    with mapping as (
    select 'LOT_NUMBER' as old_value, 'Lot Number' as new_value
    union all select 'LOT_NUMBER_XREF', 'Lot Number Cross-Reference'
    )
    select
    t.value,
    coalesce(m.new_value, 'none') as new_value
    from tbl_your_table t
    left join mapping m on t.value = m.old_value


    Or SubQuery:



    select 
    t.value,
    coalesce(m.new_value, 'none') as new_value
    from tbl_your_table t
    left join (
    select 'LOT_NUMBER' as old_value, 'Lot Number' as new_value
    union all select 'LOT_NUMBER_XREF', 'Lot Number Cross-Reference'
    ) m on t.value = m.old_value





    share|improve this answer































      1














      If you have something generic like _ charecter then what you can do is use LIKE operator and



                   SELECT replace(dummy,'_',' ') from table where 
      dummy like '%_%' -- check generic regex


      then, use CASE WHEN on top of this with DUMMY IN(X,REF) Then REPLACE(DUMMY,'X',CROSS) likewise for the others as well this will reduce the no of cases.



      The thing is just check the generalised terms and their replacements store it IN clause prefiltering the most general ones. If the count is still large you need a temporary table for lookup reference like array in other programming languages






      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%2f54029037%2fmap-values-in-column-to-values-in-sql%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









        1














        If you have many different values to replace, you should create a table to store the mapping of old values to new values. Then you can join that table in your query.



        The below SQL replaces the old value with the new value (or leaves it as is if no translation was defined) :



        SELECT 
        COALESCE(m.new_value, t.value)
        FROM
        table t
        LEFT JOIN mapping_table m
        ON t.value = m.old_value


        For a limited number of values to translate you can CASE. or maybe a CTE with UNION :



        WITH mapping_table AS (
        SELECT 'LOT_NUMBER' AS old_value, 'Lot Number' AS new_value
        SELECT 'LOT_NUMBER_XREF', 'Lot Number Cross-Reference'
        )
        SELECT
        COALESCE(m.new_value, t.value)
        FROM
        table t
        LEFT JOIN mapping_table m
        ON t.value = m.old_value





        share|improve this answer






























          1














          If you have many different values to replace, you should create a table to store the mapping of old values to new values. Then you can join that table in your query.



          The below SQL replaces the old value with the new value (or leaves it as is if no translation was defined) :



          SELECT 
          COALESCE(m.new_value, t.value)
          FROM
          table t
          LEFT JOIN mapping_table m
          ON t.value = m.old_value


          For a limited number of values to translate you can CASE. or maybe a CTE with UNION :



          WITH mapping_table AS (
          SELECT 'LOT_NUMBER' AS old_value, 'Lot Number' AS new_value
          SELECT 'LOT_NUMBER_XREF', 'Lot Number Cross-Reference'
          )
          SELECT
          COALESCE(m.new_value, t.value)
          FROM
          table t
          LEFT JOIN mapping_table m
          ON t.value = m.old_value





          share|improve this answer




























            1












            1








            1







            If you have many different values to replace, you should create a table to store the mapping of old values to new values. Then you can join that table in your query.



            The below SQL replaces the old value with the new value (or leaves it as is if no translation was defined) :



            SELECT 
            COALESCE(m.new_value, t.value)
            FROM
            table t
            LEFT JOIN mapping_table m
            ON t.value = m.old_value


            For a limited number of values to translate you can CASE. or maybe a CTE with UNION :



            WITH mapping_table AS (
            SELECT 'LOT_NUMBER' AS old_value, 'Lot Number' AS new_value
            SELECT 'LOT_NUMBER_XREF', 'Lot Number Cross-Reference'
            )
            SELECT
            COALESCE(m.new_value, t.value)
            FROM
            table t
            LEFT JOIN mapping_table m
            ON t.value = m.old_value





            share|improve this answer















            If you have many different values to replace, you should create a table to store the mapping of old values to new values. Then you can join that table in your query.



            The below SQL replaces the old value with the new value (or leaves it as is if no translation was defined) :



            SELECT 
            COALESCE(m.new_value, t.value)
            FROM
            table t
            LEFT JOIN mapping_table m
            ON t.value = m.old_value


            For a limited number of values to translate you can CASE. or maybe a CTE with UNION :



            WITH mapping_table AS (
            SELECT 'LOT_NUMBER' AS old_value, 'Lot Number' AS new_value
            SELECT 'LOT_NUMBER_XREF', 'Lot Number Cross-Reference'
            )
            SELECT
            COALESCE(m.new_value, t.value)
            FROM
            table t
            LEFT JOIN mapping_table m
            ON t.value = m.old_value






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 3 at 20:20

























            answered Jan 3 at 20:12









            GMBGMB

            21.9k61128




            21.9k61128

























                1














                If you can't use cross-reference table, use (you'll have to generate it in your app) sub-query or cte instead!
                CTE:



                with mapping as (
                select 'LOT_NUMBER' as old_value, 'Lot Number' as new_value
                union all select 'LOT_NUMBER_XREF', 'Lot Number Cross-Reference'
                )
                select
                t.value,
                coalesce(m.new_value, 'none') as new_value
                from tbl_your_table t
                left join mapping m on t.value = m.old_value


                Or SubQuery:



                select 
                t.value,
                coalesce(m.new_value, 'none') as new_value
                from tbl_your_table t
                left join (
                select 'LOT_NUMBER' as old_value, 'Lot Number' as new_value
                union all select 'LOT_NUMBER_XREF', 'Lot Number Cross-Reference'
                ) m on t.value = m.old_value





                share|improve this answer




























                  1














                  If you can't use cross-reference table, use (you'll have to generate it in your app) sub-query or cte instead!
                  CTE:



                  with mapping as (
                  select 'LOT_NUMBER' as old_value, 'Lot Number' as new_value
                  union all select 'LOT_NUMBER_XREF', 'Lot Number Cross-Reference'
                  )
                  select
                  t.value,
                  coalesce(m.new_value, 'none') as new_value
                  from tbl_your_table t
                  left join mapping m on t.value = m.old_value


                  Or SubQuery:



                  select 
                  t.value,
                  coalesce(m.new_value, 'none') as new_value
                  from tbl_your_table t
                  left join (
                  select 'LOT_NUMBER' as old_value, 'Lot Number' as new_value
                  union all select 'LOT_NUMBER_XREF', 'Lot Number Cross-Reference'
                  ) m on t.value = m.old_value





                  share|improve this answer


























                    1












                    1








                    1







                    If you can't use cross-reference table, use (you'll have to generate it in your app) sub-query or cte instead!
                    CTE:



                    with mapping as (
                    select 'LOT_NUMBER' as old_value, 'Lot Number' as new_value
                    union all select 'LOT_NUMBER_XREF', 'Lot Number Cross-Reference'
                    )
                    select
                    t.value,
                    coalesce(m.new_value, 'none') as new_value
                    from tbl_your_table t
                    left join mapping m on t.value = m.old_value


                    Or SubQuery:



                    select 
                    t.value,
                    coalesce(m.new_value, 'none') as new_value
                    from tbl_your_table t
                    left join (
                    select 'LOT_NUMBER' as old_value, 'Lot Number' as new_value
                    union all select 'LOT_NUMBER_XREF', 'Lot Number Cross-Reference'
                    ) m on t.value = m.old_value





                    share|improve this answer













                    If you can't use cross-reference table, use (you'll have to generate it in your app) sub-query or cte instead!
                    CTE:



                    with mapping as (
                    select 'LOT_NUMBER' as old_value, 'Lot Number' as new_value
                    union all select 'LOT_NUMBER_XREF', 'Lot Number Cross-Reference'
                    )
                    select
                    t.value,
                    coalesce(m.new_value, 'none') as new_value
                    from tbl_your_table t
                    left join mapping m on t.value = m.old_value


                    Or SubQuery:



                    select 
                    t.value,
                    coalesce(m.new_value, 'none') as new_value
                    from tbl_your_table t
                    left join (
                    select 'LOT_NUMBER' as old_value, 'Lot Number' as new_value
                    union all select 'LOT_NUMBER_XREF', 'Lot Number Cross-Reference'
                    ) m on t.value = m.old_value






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 3 at 20:16









                    Alex ShamAlex Sham

                    312411




                    312411























                        1














                        If you have something generic like _ charecter then what you can do is use LIKE operator and



                                     SELECT replace(dummy,'_',' ') from table where 
                        dummy like '%_%' -- check generic regex


                        then, use CASE WHEN on top of this with DUMMY IN(X,REF) Then REPLACE(DUMMY,'X',CROSS) likewise for the others as well this will reduce the no of cases.



                        The thing is just check the generalised terms and their replacements store it IN clause prefiltering the most general ones. If the count is still large you need a temporary table for lookup reference like array in other programming languages






                        share|improve this answer




























                          1














                          If you have something generic like _ charecter then what you can do is use LIKE operator and



                                       SELECT replace(dummy,'_',' ') from table where 
                          dummy like '%_%' -- check generic regex


                          then, use CASE WHEN on top of this with DUMMY IN(X,REF) Then REPLACE(DUMMY,'X',CROSS) likewise for the others as well this will reduce the no of cases.



                          The thing is just check the generalised terms and their replacements store it IN clause prefiltering the most general ones. If the count is still large you need a temporary table for lookup reference like array in other programming languages






                          share|improve this answer


























                            1












                            1








                            1







                            If you have something generic like _ charecter then what you can do is use LIKE operator and



                                         SELECT replace(dummy,'_',' ') from table where 
                            dummy like '%_%' -- check generic regex


                            then, use CASE WHEN on top of this with DUMMY IN(X,REF) Then REPLACE(DUMMY,'X',CROSS) likewise for the others as well this will reduce the no of cases.



                            The thing is just check the generalised terms and their replacements store it IN clause prefiltering the most general ones. If the count is still large you need a temporary table for lookup reference like array in other programming languages






                            share|improve this answer













                            If you have something generic like _ charecter then what you can do is use LIKE operator and



                                         SELECT replace(dummy,'_',' ') from table where 
                            dummy like '%_%' -- check generic regex


                            then, use CASE WHEN on top of this with DUMMY IN(X,REF) Then REPLACE(DUMMY,'X',CROSS) likewise for the others as well this will reduce the no of cases.



                            The thing is just check the generalised terms and their replacements store it IN clause prefiltering the most general ones. If the count is still large you need a temporary table for lookup reference like array in other programming languages







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 3 at 20:20









                            Himanshu AhujaHimanshu Ahuja

                            9952219




                            9952219






























                                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%2f54029037%2fmap-values-in-column-to-values-in-sql%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

                                Monofisismo

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas