Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL case function to check for charindex as '' or ','

SQL case function to check for charindex as '' or ','

Scheduled Pinned Locked Moved Database
databasehelp
15 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Wendelius

    Trustapple wrote:

    It still is not working

    Getting and error message? I think it should be more like (may contain several typos):

    select customername,
    case
    when CHARINDEX(',', customername)>0 then
    substring(customername,1,CHARINDEX(',', customername) -1)
    else
    substring(customername,1,CHARINDEX(' ', customername) -1)
    end as [firstname ],
    case
    when CHARINDEX(',', customername)>0 then
    SUBSTRING(customername, CHARINDEX(',', customername) + 1, LEN(customername))
    else
    SUBSTRING(customername, CHARINDEX(' ', customername) + 1, LEN(customername))
    end AS [LastName]
    from TableName

    The need to optimize rises from a bad design.My articles[^]

    B Offline
    B Offline
    Ben Fair
    wrote on last edited by
    #6

    I think it should be:

    select customername,
    -- get the last name
    case
    when CHARINDEX(',', customername) > 0 then
    SUBSTRING(customername, 1, CHARINDEX(',', customername) - 1) -- comma is delimiter
    when CHARINDEX(' ', customername) > 0 then
    SUBSTRING(customername, 1, CHARINDEX(' ', customername) - 1) -- space is delimiter
    else
    '' -- neither comma nor space found
    end as [lastname],
    -- get the first name
    case
    when CHARINDEX(',', customername) > 0 then
    SUBSTRING(customername, CHARINDEX(',', customername) + 1, LEN(customername) - CHARINDEX(',', customername) + 1) -- comma is delimiter
    when CHARINDEX(' ', customername) > 0 then
    SUBSTRING(customername, CHARINDEX(' ', customername) + 1, LEN(customername) - CHARINDEX(' ', customername) + 1) -- space is delimiter
    else
    '' -- neither comma nor space found
    end AS [firstname]
    from TableName

    Keep It Simple Stupid! (KISS)

    W T 2 Replies Last reply
    0
    • T Trustapple

      Hey Mika, Thanks for you trying to help.My server i unavailable now.I will be able to access it only after 5hours.As soon as i try thi s query i will post a reply and let you know. Many Thanks for your help....:) Merry Christmas

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #7

      Trustapple wrote:

      Thanks for you trying to help

      No problem :)

      Trustapple wrote:

      I will be able to access it only after 5hours.As soon as i try thi s query i will post a reply and let you know

      At that time it's 3:30 AM here so I'll be :zzz: :zzz: :zzz: But I'll have a look at your reply in the morning. If you encounter problems I believe that someone else will help you forward in the meantime.

      Trustapple wrote:

      Merry Christmas

      Merry Christmas to you too :)

      The need to optimize rises from a bad design.My articles[^]

      T 2 Replies Last reply
      0
      • B Ben Fair

        I think it should be:

        select customername,
        -- get the last name
        case
        when CHARINDEX(',', customername) > 0 then
        SUBSTRING(customername, 1, CHARINDEX(',', customername) - 1) -- comma is delimiter
        when CHARINDEX(' ', customername) > 0 then
        SUBSTRING(customername, 1, CHARINDEX(' ', customername) - 1) -- space is delimiter
        else
        '' -- neither comma nor space found
        end as [lastname],
        -- get the first name
        case
        when CHARINDEX(',', customername) > 0 then
        SUBSTRING(customername, CHARINDEX(',', customername) + 1, LEN(customername) - CHARINDEX(',', customername) + 1) -- comma is delimiter
        when CHARINDEX(' ', customername) > 0 then
        SUBSTRING(customername, CHARINDEX(' ', customername) + 1, LEN(customername) - CHARINDEX(' ', customername) + 1) -- space is delimiter
        else
        '' -- neither comma nor space found
        end AS [firstname]
        from TableName

        Keep It Simple Stupid! (KISS)

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #8

        Yeah, you're rught. I misplaced the commas. Corrected now.

        Ben Fair wrote:

        SUBSTRING(customername, CHARINDEX(',', customername) + 1, LEN(customername) - CHARINDEX(',', customername) + 1) -- comma is delimiter

        Although it's 'cleaner' to use the rest of the length as a parameter, it's not necessary. Basically if you want the rest of the string you can pass any value as long as it's at least the rest of the length.

        The need to optimize rises from a bad design.My articles[^]

        B 1 Reply Last reply
        0
        • W Wendelius

          Yeah, you're rught. I misplaced the commas. Corrected now.

          Ben Fair wrote:

          SUBSTRING(customername, CHARINDEX(',', customername) + 1, LEN(customername) - CHARINDEX(',', customername) + 1) -- comma is delimiter

          Although it's 'cleaner' to use the rest of the length as a parameter, it's not necessary. Basically if you want the rest of the string you can pass any value as long as it's at least the rest of the length.

          The need to optimize rises from a bad design.My articles[^]

          B Offline
          B Offline
          Ben Fair
          wrote on last edited by
          #9

          Oh, I wasn't aware of that; I'll have to remember that! Thanks!

          Keep It Simple Stupid! (KISS)

          1 Reply Last reply
          0
          • W Wendelius

            Trustapple wrote:

            Thanks for you trying to help

            No problem :)

            Trustapple wrote:

            I will be able to access it only after 5hours.As soon as i try thi s query i will post a reply and let you know

            At that time it's 3:30 AM here so I'll be :zzz: :zzz: :zzz: But I'll have a look at your reply in the morning. If you encounter problems I believe that someone else will help you forward in the meantime.

            Trustapple wrote:

            Merry Christmas

            Merry Christmas to you too :)

            The need to optimize rises from a bad design.My articles[^]

            T Offline
            T Offline
            Trustapple
            wrote on last edited by
            #10

            Hey Mika, Thanks again to you and Ben....It worked.I made slight modification in the query though...a very slight one. The credit goes to you guys .... Please see the working query and let me know if any tweaking is necessary...becuase i will be cghnaging this select to update query for around 9million records.....:) select customername, -- get the last name case when CHARINDEX(',', customername) > 0 then SUBSTRING(customername, 1, CHARINDEX(',', customername) - 1) -- comma is delimiter when CHARINDEX(' ', customername) > 0 then SUBSTRING(customername, CHARINDEX(' ', customername) + 1, LEN(customername) - CHARINDEX(' ', customername) + 1) -- space is delimiter else '' -- neither comma nor space found end as [lastname], -- get the first name case when CHARINDEX(',', customername) > 0 then SUBSTRING(customername, CHARINDEX(',', customername) + 1, LEN(customername) - CHARINDEX(',', customername) + 1) -- comma is delimiter when CHARINDEX(' ', customername) > 0 then SUBSTRING(customername, 1, CHARINDEX(' ', customername) - 1) -- space is delimiter else '' -- neither comma nor space found end AS [firstname] from TableName

            1 Reply Last reply
            0
            • W Wendelius

              Trustapple wrote:

              Thanks for you trying to help

              No problem :)

              Trustapple wrote:

              I will be able to access it only after 5hours.As soon as i try thi s query i will post a reply and let you know

              At that time it's 3:30 AM here so I'll be :zzz: :zzz: :zzz: But I'll have a look at your reply in the morning. If you encounter problems I believe that someone else will help you forward in the meantime.

              Trustapple wrote:

              Merry Christmas

              Merry Christmas to you too :)

              The need to optimize rises from a bad design.My articles[^]

              T Offline
              T Offline
              Trustapple
              wrote on last edited by
              #11

              Hey Mika, The time now is 7:59 AM for you,i beleive....Happy sleeping:). Thanks again to you and Ben....It worked.I made slight modification in the query though...a very slight one. The credit goes to you guys .... Please see the working query and let me know if any tweaking is necessary...becuase i will be cghnaging this select to update query for around 9million records.....:) select customername, -- get the last name case when CHARINDEX(',', customername) > 0 then SUBSTRING(customername, 1, CHARINDEX(',', customername) - 1) -- comma is delimiter when CHARINDEX(' ', customername) > 0 then SUBSTRING(customername, CHARINDEX(' ', customername) + 1, LEN(customername) - CHARINDEX(' ', customername) + 1) -- space is delimiter else '' -- neither comma nor space found end as [lastname], -- get the first name case when CHARINDEX(',', customername) > 0 then SUBSTRING(customername, CHARINDEX(',', customername) + 1, LEN(customername) - CHARINDEX(',', customername) + 1) -- comma is delimiter when CHARINDEX(' ', customername) > 0 then SUBSTRING(customername, 1, CHARINDEX(' ', customername) - 1) -- space is delimiter else '' -- neither comma nor space found end AS [firstname] from TableName

              1 Reply Last reply
              0
              • B Ben Fair

                I think it should be:

                select customername,
                -- get the last name
                case
                when CHARINDEX(',', customername) > 0 then
                SUBSTRING(customername, 1, CHARINDEX(',', customername) - 1) -- comma is delimiter
                when CHARINDEX(' ', customername) > 0 then
                SUBSTRING(customername, 1, CHARINDEX(' ', customername) - 1) -- space is delimiter
                else
                '' -- neither comma nor space found
                end as [lastname],
                -- get the first name
                case
                when CHARINDEX(',', customername) > 0 then
                SUBSTRING(customername, CHARINDEX(',', customername) + 1, LEN(customername) - CHARINDEX(',', customername) + 1) -- comma is delimiter
                when CHARINDEX(' ', customername) > 0 then
                SUBSTRING(customername, CHARINDEX(' ', customername) + 1, LEN(customername) - CHARINDEX(' ', customername) + 1) -- space is delimiter
                else
                '' -- neither comma nor space found
                end AS [firstname]
                from TableName

                Keep It Simple Stupid! (KISS)

                T Offline
                T Offline
                Trustapple
                wrote on last edited by
                #12

                Hey , Thanks a lot to you and Mika it worked. You guys have been wonderful. Hey i ran into another problem.... I might need to integrate one more condition;ie along with our query i want it to also do this that is if the customername is Mika (ie the name is only one word and it doesnot contain any ',' or ' ')Then it should update to FirstName.....Am pasting the working query below can any of you integrate this to my query..... Name FirstName LastName Mika Mika Ben Ben QUERY:update TableName set lastname= -- get the last name case when CHARINDEX(',', customername) > 0 then SUBSTRING(customername, 1, CHARINDEX(',', customername) - 1) -- comma is delimiter when CHARINDEX(' ', customername) > 0 then SUBSTRING(customername, CHARINDEX(' ', customername) + 1, LEN(customername) - CHARINDEX(' ', customername) + 1) -- space is delimiter else '' -- neither comma nor space found end, -- get the first name firstname = case when CHARINDEX(',', customername) > 0 then SUBSTRING(customername, CHARINDEX(',', customername) + 1, LEN(customername) - CHARINDEX(',', customername) + 1) -- comma is delimiter when CHARINDEX(' ', customername) > 0 then SUBSTRING(customername, 1, CHARINDEX(' ', customername) - 1) -- space is delimiter else '' -- neither comma nor space found end

                modified on Thursday, December 11, 2008 2:23 AM

                W 1 Reply Last reply
                0
                • T Trustapple

                  Hey , Thanks a lot to you and Mika it worked. You guys have been wonderful. Hey i ran into another problem.... I might need to integrate one more condition;ie along with our query i want it to also do this that is if the customername is Mika (ie the name is only one word and it doesnot contain any ',' or ' ')Then it should update to FirstName.....Am pasting the working query below can any of you integrate this to my query..... Name FirstName LastName Mika Mika Ben Ben QUERY:update TableName set lastname= -- get the last name case when CHARINDEX(',', customername) > 0 then SUBSTRING(customername, 1, CHARINDEX(',', customername) - 1) -- comma is delimiter when CHARINDEX(' ', customername) > 0 then SUBSTRING(customername, CHARINDEX(' ', customername) + 1, LEN(customername) - CHARINDEX(' ', customername) + 1) -- space is delimiter else '' -- neither comma nor space found end, -- get the first name firstname = case when CHARINDEX(',', customername) > 0 then SUBSTRING(customername, CHARINDEX(',', customername) + 1, LEN(customername) - CHARINDEX(',', customername) + 1) -- comma is delimiter when CHARINDEX(' ', customername) > 0 then SUBSTRING(customername, 1, CHARINDEX(' ', customername) - 1) -- space is delimiter else '' -- neither comma nor space found end

                  modified on Thursday, December 11, 2008 2:23 AM

                  W Offline
                  W Offline
                  Wendelius
                  wrote on last edited by
                  #13

                  Trustapple wrote:

                  Thanks

                  You're welcome.

                  Trustapple wrote:

                  along with our query i want it to also do this that is if the customername is Mika (ie the name is only one word and it doesnot contain any ',' or ' ')Then it should update to FirstName.....

                  Do you mean something like this:

                  update TableName
                  set lastname
                  = case
                  when CHARINDEX(',', customername) > 0 then
                  SUBSTRING(customername, 1, CHARINDEX(',', customername) - 1) -- comma is delimiter
                  when CHARINDEX(' ', customername) > 0 then
                  SUBSTRING(customername, CHARINDEX(' ', customername) + 1, LEN(customername) - CHARINDEX(' ', customername) + 1) -- space is delimiter
                  else
                  TableName.LastName -- no delimitter so don't change lastname in the table
                  end,
                  firstname
                  = case
                  when CHARINDEX(',', customername) > 0 then
                  SUBSTRING(customername, CHARINDEX(',', customername) + 1, LEN(customername) - CHARINDEX(',', customername) + 1) -- comma is delimiter
                  when CHARINDEX(' ', customername) > 0 then
                  SUBSTRING(customername, 1, CHARINDEX(' ', customername) - 1) -- space is delimiter
                  else
                  customername -- no delimiter so use the full data to firstname
                  end ...

                  The need to optimize rises from a bad design.My articles[^]

                  T 1 Reply Last reply
                  0
                  • W Wendelius

                    Trustapple wrote:

                    Thanks

                    You're welcome.

                    Trustapple wrote:

                    along with our query i want it to also do this that is if the customername is Mika (ie the name is only one word and it doesnot contain any ',' or ' ')Then it should update to FirstName.....

                    Do you mean something like this:

                    update TableName
                    set lastname
                    = case
                    when CHARINDEX(',', customername) > 0 then
                    SUBSTRING(customername, 1, CHARINDEX(',', customername) - 1) -- comma is delimiter
                    when CHARINDEX(' ', customername) > 0 then
                    SUBSTRING(customername, CHARINDEX(' ', customername) + 1, LEN(customername) - CHARINDEX(' ', customername) + 1) -- space is delimiter
                    else
                    TableName.LastName -- no delimitter so don't change lastname in the table
                    end,
                    firstname
                    = case
                    when CHARINDEX(',', customername) > 0 then
                    SUBSTRING(customername, CHARINDEX(',', customername) + 1, LEN(customername) - CHARINDEX(',', customername) + 1) -- comma is delimiter
                    when CHARINDEX(' ', customername) > 0 then
                    SUBSTRING(customername, 1, CHARINDEX(' ', customername) - 1) -- space is delimiter
                    else
                    customername -- no delimiter so use the full data to firstname
                    end ...

                    The need to optimize rises from a bad design.My articles[^]

                    T Offline
                    T Offline
                    Trustapple
                    wrote on last edited by
                    #14

                    Hey Mika, I got it working.Thanks for your help......again :). Lots of Bestwishes, Jiju

                    W 1 Reply Last reply
                    0
                    • T Trustapple

                      Hey Mika, I got it working.Thanks for your help......again :). Lots of Bestwishes, Jiju

                      W Offline
                      W Offline
                      Wendelius
                      wrote on last edited by
                      #15

                      Trustapple wrote:

                      Thanks for your help

                      You're welcome.

                      The need to optimize rises from a bad design.My articles[^]

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups