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. Need a script, SDF or UDF for checking the following conditions and make IF Else in a stored procedure SQL Server

Need a script, SDF or UDF for checking the following conditions and make IF Else in a stored procedure SQL Server

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintoolshelp
7 Posts 4 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.
  • S Offline
    S Offline
    simpledeveloper
    wrote on last edited by
    #1

    Hi, I have 3 different types of values that can come, like for example, I00259, T00123, C00456 etc like that, I want to check an if Else statement in my stored procedure that, 1. If a value has 6 characters in length, and starts with I and ends with a number 2. If a value has 6 characters in length, and starts with T and ends with a number and 3. If a value has 6 characters in length, and starts with C and ends with a number I have to write different sections if the conditions satisfy, any help in SQL Server please.

    L W Richard DeemingR 4 Replies Last reply
    0
    • S simpledeveloper

      Hi, I have 3 different types of values that can come, like for example, I00259, T00123, C00456 etc like that, I want to check an if Else statement in my stored procedure that, 1. If a value has 6 characters in length, and starts with I and ends with a number 2. If a value has 6 characters in length, and starts with T and ends with a number and 3. If a value has 6 characters in length, and starts with C and ends with a number I have to write different sections if the conditions satisfy, any help in SQL Server please.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You can use the SQL Server LEN() Function[^] to check the length of the string, and the SQL Server SUBSTRING() Function[^] to extract the first character.

      S 1 Reply Last reply
      0
      • S simpledeveloper

        Hi, I have 3 different types of values that can come, like for example, I00259, T00123, C00456 etc like that, I want to check an if Else statement in my stored procedure that, 1. If a value has 6 characters in length, and starts with I and ends with a number 2. If a value has 6 characters in length, and starts with T and ends with a number and 3. If a value has 6 characters in length, and starts with C and ends with a number I have to write different sections if the conditions satisfy, any help in SQL Server please.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Try panicking. 1, 2, and 3 are the same assignment. And, you will fail.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

        1 Reply Last reply
        0
        • S simpledeveloper

          Hi, I have 3 different types of values that can come, like for example, I00259, T00123, C00456 etc like that, I want to check an if Else statement in my stored procedure that, 1. If a value has 6 characters in length, and starts with I and ends with a number 2. If a value has 6 characters in length, and starts with T and ends with a number and 3. If a value has 6 characters in length, and starts with C and ends with a number I have to write different sections if the conditions satisfy, any help in SQL Server please.

          W Offline
          W Offline
          W Balboos GHB
          wrote on last edited by
          #4

          Someone's trying to teach you how to organize things. Here's a way to consider what they want to teach you: Is the first test you make the first character or the length of the string? As proposed in your (homework) question, one is a more efficient option than the other. Another thing your being taught is "how to look up something you've never done before". You need to make sure you can handle the latter, in particular, if you ever really want to learn how to code.

          Ravings en masse^

          "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

          "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

          1 Reply Last reply
          0
          • L Lost User

            You can use the SQL Server LEN() Function[^] to check the length of the string, and the SQL Server SUBSTRING() Function[^] to extract the first character.

            S Offline
            S Offline
            simpledeveloper
            wrote on last edited by
            #5

            Thank you Richard

            L 1 Reply Last reply
            0
            • S simpledeveloper

              Thank you Richard

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I am not an SQL expert, but I do know how to use Google. it is a skill you need to develop.

              1 Reply Last reply
              0
              • S simpledeveloper

                Hi, I have 3 different types of values that can come, like for example, I00259, T00123, C00456 etc like that, I want to check an if Else statement in my stored procedure that, 1. If a value has 6 characters in length, and starts with I and ends with a number 2. If a value has 6 characters in length, and starts with T and ends with a number and 3. If a value has 6 characters in length, and starts with C and ends with a number I have to write different sections if the conditions satisfy, any help in SQL Server please.

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                It would probably be easiest to test this using LIKE: LIKE (Transact-SQL) - SQL Server | Microsoft Docs[^] Eg: "I" followed by five numbers would be:

                value LIKE 'I[0-9][0-9][0-9][0-9][0-9]'


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                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