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. Manipulate SQL String

Manipulate SQL String

Scheduled Pinned Locked Moved Database
databasehelptutorialquestion
8 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.
  • T Offline
    T Offline
    tantja
    wrote on last edited by
    #1

    Hi, I need help with selecting part of column value. Say that I have a database that return me a InventoryID InventoryID as123-CE123daf235 23d45543-CE23432 234dave-CEj324lio I want to select everything before -CE, i couldn't use Substring() function because each value have a different character id. can anyone advice me how to do this??? Thanks in advance!!!

    D B 2 Replies Last reply
    0
    • T tantja

      Hi, I need help with selecting part of column value. Say that I have a database that return me a InventoryID InventoryID as123-CE123daf235 23d45543-CE23432 234dave-CEj324lio I want to select everything before -CE, i couldn't use Substring() function because each value have a different character id. can anyone advice me how to do this??? Thanks in advance!!!

      D Offline
      D Offline
      DerekFL
      wrote on last edited by
      #2

      SELECT LEFT(InventoryID, LEN(InventoryID) - CHARINDEX('CE',InventoryID))

      T 1 Reply Last reply
      0
      • T tantja

        Hi, I need help with selecting part of column value. Say that I have a database that return me a InventoryID InventoryID as123-CE123daf235 23d45543-CE23432 234dave-CEj324lio I want to select everything before -CE, i couldn't use Substring() function because each value have a different character id. can anyone advice me how to do this??? Thanks in advance!!!

        B Offline
        B Offline
        Blue_Boy
        wrote on last edited by
        #3

        select substring(InventoryID,1,CHARINDEX('CE',InventoryID)-2) from tablename


        I Love T-SQL "Don't torture yourself,let the life to do it for you."

        T 1 Reply Last reply
        0
        • D DerekFL

          SELECT LEFT(InventoryID, LEN(InventoryID) - CHARINDEX('CE',InventoryID))

          T Offline
          T Offline
          tantja
          wrote on last edited by
          #4

          Thank you....the substring function work without -2 at the end. Thanks again!!!

          1 Reply Last reply
          0
          • B Blue_Boy

            select substring(InventoryID,1,CHARINDEX('CE',InventoryID)-2) from tablename


            I Love T-SQL "Don't torture yourself,let the life to do it for you."

            T Offline
            T Offline
            tantja
            wrote on last edited by
            #5

            I have another small question regarding this: select substring(inventoryId,1,Charindex('-CE',InventoryID)) from Inventory work if all the inventoryID contain -CE, however there're some inventoryID that doesn't contain -CE and for those that doesn't contain -CE i want to select the whole field. My SQL statement is really big, if i use If..Else statement then it's like if InventoryID like '-CE' begin .....the whole nine yard of select statement... else .....another nine yard of select statement.... i was wonder if you can use if within select statement...what i picture are: SELECT *, (inventoryid without -CE for those that contain -CE) FROM Inventory I hope you understand my question. Thanks!

            B 1 Reply Last reply
            0
            • T tantja

              I have another small question regarding this: select substring(inventoryId,1,Charindex('-CE',InventoryID)) from Inventory work if all the inventoryID contain -CE, however there're some inventoryID that doesn't contain -CE and for those that doesn't contain -CE i want to select the whole field. My SQL statement is really big, if i use If..Else statement then it's like if InventoryID like '-CE' begin .....the whole nine yard of select statement... else .....another nine yard of select statement.... i was wonder if you can use if within select statement...what i picture are: SELECT *, (inventoryid without -CE for those that contain -CE) FROM Inventory I hope you understand my question. Thanks!

              B Offline
              B Offline
              Blue_Boy
              wrote on last edited by
              #6

              InventoryID contain values like this: 23d45543CE23432 23d45543-CE23432 73d45543CE23434 73d45543-CE23434 ... and so on Try T-SQL down below I think it will work for you. select case when CHARINDEX('-CE',inventoryid ) =0 then inventoryid else substring(inventoryid ,1,CHARINDEX('-CE',inventoryid )-1) end as inventoryID from Inventory


              I Love T-SQL "Don't torture yourself,let the life to do it for you."

              T 1 Reply Last reply
              0
              • B Blue_Boy

                InventoryID contain values like this: 23d45543CE23432 23d45543-CE23432 73d45543CE23434 73d45543-CE23434 ... and so on Try T-SQL down below I think it will work for you. select case when CHARINDEX('-CE',inventoryid ) =0 then inventoryid else substring(inventoryid ,1,CHARINDEX('-CE',inventoryid )-1) end as inventoryID from Inventory


                I Love T-SQL "Don't torture yourself,let the life to do it for you."

                T Offline
                T Offline
                tantja
                wrote on last edited by
                #7

                Thank you so much...that's just want i needed... Thanks!!!

                B 1 Reply Last reply
                0
                • T tantja

                  Thank you so much...that's just want i needed... Thanks!!!

                  B Offline
                  B Offline
                  Blue_Boy
                  wrote on last edited by
                  #8

                  No problem. You are welcome. :)


                  I Love T-SQL "Don't torture yourself,let the life to do it for you."

                  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