MSSQL case statement











up vote
2
down vote

favorite












I want to create a view which only shows me values which have a % in it.



I tried following



 Alter VIEW [dbo].[history] as

select

[objid]
,[interval]
,[value_channel]
,[value_channelid]
,case when [value_text] like '[%]' then right([value text],2)
,[value_raw_text]
,[coverage_raw]

from .....


but I get the error
Incorrect syntax near ','.



any idea?










share|improve this question




















  • 3




    Classic. You forgot the "end" at the end of the case.
    – George Menoutis
    Nov 8 at 11:14















up vote
2
down vote

favorite












I want to create a view which only shows me values which have a % in it.



I tried following



 Alter VIEW [dbo].[history] as

select

[objid]
,[interval]
,[value_channel]
,[value_channelid]
,case when [value_text] like '[%]' then right([value text],2)
,[value_raw_text]
,[coverage_raw]

from .....


but I get the error
Incorrect syntax near ','.



any idea?










share|improve this question




















  • 3




    Classic. You forgot the "end" at the end of the case.
    – George Menoutis
    Nov 8 at 11:14













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I want to create a view which only shows me values which have a % in it.



I tried following



 Alter VIEW [dbo].[history] as

select

[objid]
,[interval]
,[value_channel]
,[value_channelid]
,case when [value_text] like '[%]' then right([value text],2)
,[value_raw_text]
,[coverage_raw]

from .....


but I get the error
Incorrect syntax near ','.



any idea?










share|improve this question















I want to create a view which only shows me values which have a % in it.



I tried following



 Alter VIEW [dbo].[history] as

select

[objid]
,[interval]
,[value_channel]
,[value_channelid]
,case when [value_text] like '[%]' then right([value text],2)
,[value_raw_text]
,[coverage_raw]

from .....


but I get the error
Incorrect syntax near ','.



any idea?







sql sql-server tsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 12:53









Rahul Neekhra

514426




514426










asked Nov 8 at 11:13









cyberoner1

112




112








  • 3




    Classic. You forgot the "end" at the end of the case.
    – George Menoutis
    Nov 8 at 11:14














  • 3




    Classic. You forgot the "end" at the end of the case.
    – George Menoutis
    Nov 8 at 11:14








3




3




Classic. You forgot the "end" at the end of the case.
– George Menoutis
Nov 8 at 11:14




Classic. You forgot the "end" at the end of the case.
– George Menoutis
Nov 8 at 11:14












1 Answer
1






active

oldest

votes

















up vote
5
down vote













There are tow things missing in your alter view statement:



One is the end after the case, and the other one is an alias to the column.



Alter VIEW [dbo].[history] as

select

[objid]
,[interval]
,[value_channel]
,[value_channelid]
,case when [value_text] like '[%]' then right([value_text],2) end as value_text
,[value_raw_text]
,[coverage_raw]

from .....


You might also want to add an else to the case expression, otherwise it will simply return null when the condition is not matched:



Alter VIEW [dbo].[history] as

select

[objid]
,[interval]
,[value_channel]
,[value_channelid]
,case when [value_text] like '[%]' then right([value_text],2) else [value_text] end as value_text
,[value_raw_text]
,[coverage_raw]

from .....





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',
    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%2f53206607%2fmssql-case-statement%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote













    There are tow things missing in your alter view statement:



    One is the end after the case, and the other one is an alias to the column.



    Alter VIEW [dbo].[history] as

    select

    [objid]
    ,[interval]
    ,[value_channel]
    ,[value_channelid]
    ,case when [value_text] like '[%]' then right([value_text],2) end as value_text
    ,[value_raw_text]
    ,[coverage_raw]

    from .....


    You might also want to add an else to the case expression, otherwise it will simply return null when the condition is not matched:



    Alter VIEW [dbo].[history] as

    select

    [objid]
    ,[interval]
    ,[value_channel]
    ,[value_channelid]
    ,case when [value_text] like '[%]' then right([value_text],2) else [value_text] end as value_text
    ,[value_raw_text]
    ,[coverage_raw]

    from .....





    share|improve this answer

























      up vote
      5
      down vote













      There are tow things missing in your alter view statement:



      One is the end after the case, and the other one is an alias to the column.



      Alter VIEW [dbo].[history] as

      select

      [objid]
      ,[interval]
      ,[value_channel]
      ,[value_channelid]
      ,case when [value_text] like '[%]' then right([value_text],2) end as value_text
      ,[value_raw_text]
      ,[coverage_raw]

      from .....


      You might also want to add an else to the case expression, otherwise it will simply return null when the condition is not matched:



      Alter VIEW [dbo].[history] as

      select

      [objid]
      ,[interval]
      ,[value_channel]
      ,[value_channelid]
      ,case when [value_text] like '[%]' then right([value_text],2) else [value_text] end as value_text
      ,[value_raw_text]
      ,[coverage_raw]

      from .....





      share|improve this answer























        up vote
        5
        down vote










        up vote
        5
        down vote









        There are tow things missing in your alter view statement:



        One is the end after the case, and the other one is an alias to the column.



        Alter VIEW [dbo].[history] as

        select

        [objid]
        ,[interval]
        ,[value_channel]
        ,[value_channelid]
        ,case when [value_text] like '[%]' then right([value_text],2) end as value_text
        ,[value_raw_text]
        ,[coverage_raw]

        from .....


        You might also want to add an else to the case expression, otherwise it will simply return null when the condition is not matched:



        Alter VIEW [dbo].[history] as

        select

        [objid]
        ,[interval]
        ,[value_channel]
        ,[value_channelid]
        ,case when [value_text] like '[%]' then right([value_text],2) else [value_text] end as value_text
        ,[value_raw_text]
        ,[coverage_raw]

        from .....





        share|improve this answer












        There are tow things missing in your alter view statement:



        One is the end after the case, and the other one is an alias to the column.



        Alter VIEW [dbo].[history] as

        select

        [objid]
        ,[interval]
        ,[value_channel]
        ,[value_channelid]
        ,case when [value_text] like '[%]' then right([value_text],2) end as value_text
        ,[value_raw_text]
        ,[coverage_raw]

        from .....


        You might also want to add an else to the case expression, otherwise it will simply return null when the condition is not matched:



        Alter VIEW [dbo].[history] as

        select

        [objid]
        ,[interval]
        ,[value_channel]
        ,[value_channelid]
        ,case when [value_text] like '[%]' then right([value_text],2) else [value_text] end as value_text
        ,[value_raw_text]
        ,[coverage_raw]

        from .....






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 12:16









        Zohar Peled

        50.8k73171




        50.8k73171






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206607%2fmssql-case-statement%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

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff