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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. PROBLEM IN STORED PROCEDURE SUBSTRING :((

PROBLEM IN STORED PROCEDURE SUBSTRING :((

Scheduled Pinned Locked Moved Database
helpdatabase
4 Posts 2 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.
  • D Offline
    D Offline
    dotnetcsharpdev
    wrote on last edited by
    #1

    i have a simple stored procedure   and i want to split postcode in it which could be of length either 7 or 6 and tha second part will always be of length 3. am pasting the sql here. and also the error message. Plz help me resolve this error <code> SELECT          CONVERT(varchar, DecryptByKey(Postcode)) as Postcode,           CONVERT(varchar, DecryptByKey(Postcode)) as Postcode1,           CONVERT(varchar, DecryptByKey(Postcode)) as Postcode2,                                                                        if Postcode.length = 7                          begin                               set Postcode1= substring(Postcode,5,7)                               set Postcode1= substring(Postcode,1,4)                          end                     else                          begin                               set Postcode1 = substring(Postcode,4,6)                               set Postcode1 = substring(Postcode,1,3)                                                   end            &n

    A 1 Reply Last reply
    0
    • D dotnetcsharpdev

      i have a simple stored procedure   and i want to split postcode in it which could be of length either 7 or 6 and tha second part will always be of length 3. am pasting the sql here. and also the error message. Plz help me resolve this error <code> SELECT          CONVERT(varchar, DecryptByKey(Postcode)) as Postcode,           CONVERT(varchar, DecryptByKey(Postcode)) as Postcode1,           CONVERT(varchar, DecryptByKey(Postcode)) as Postcode2,                                                                        if Postcode.length = 7                          begin                               set Postcode1= substring(Postcode,5,7)                               set Postcode1= substring(Postcode,1,4)                          end                     else                          begin                               set Postcode1 = substring(Postcode,4,6)                               set Postcode1 = substring(Postcode,1,3)                                                   end            &n

      A Offline
      A Offline
      Ashfield
      wrote on last edited by
      #2

      What database are you using? The syntax for if Postcode.length = 7 doesn't look like it will work - certainly not in sql server. In sql server it would be

      SELECT CONVERT(varchar, DecryptByKey(Postcode)) as Postcode,
      CONVERT(varchar, DecryptByKey(Postcode)) as Postcode1,
      CONVERT(varchar, DecryptByKey(Postcode)) as Postcode2,

      POSTCODE1 = case when len(postcode) = 7 then substring(Postcode,5,7) else substring(Postcode,4,6) end,
      postcode2 = case when len(postcode) = 7 then substring(Postcode,1,4) else substring(Postcode,1,3)
      FROM xyz
      WHERE (abc = @def)

      hope this helps

      Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

      D 1 Reply Last reply
      0
      • A Ashfield

        What database are you using? The syntax for if Postcode.length = 7 doesn't look like it will work - certainly not in sql server. In sql server it would be

        SELECT CONVERT(varchar, DecryptByKey(Postcode)) as Postcode,
        CONVERT(varchar, DecryptByKey(Postcode)) as Postcode1,
        CONVERT(varchar, DecryptByKey(Postcode)) as Postcode2,

        POSTCODE1 = case when len(postcode) = 7 then substring(Postcode,5,7) else substring(Postcode,4,6) end,
        postcode2 = case when len(postcode) = 7 then substring(Postcode,1,4) else substring(Postcode,1,3)
        FROM xyz
        WHERE (abc = @def)

        hope this helps

        Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

        D Offline
        D Offline
        dotnetcsharpdev
        wrote on last edited by
        #3

        thanx Ashfield for ur reply I figured it out earlier itself and have implemented it almost the same way. copying my code incase it helps anyone else. <pre> LEN(CONVERT(varchar, DecryptByKey(Postcode))) as Postcodeln,      case when LEN(CONVERT(varchar, DecryptByKey(Postcode))) = '7' then             substring(CONVERT(varchar, DecryptByKey(Postcode)),1,4)      else           substring(CONVERT(varchar, DecryptByKey(Postcode)),1,3)      end Postcode1,      case when LEN(CONVERT(varchar, DecryptByKey(Postcode))) = '6' then             substring(CONVERT(varchar, DecryptByKey(Postcode)),4,6)      else           substring(CONVERT(varchar, DecryptByKey(Postcode)),5,7)      end Postcode2, </pre> Many Thanks

        A 1 Reply Last reply
        0
        • D dotnetcsharpdev

          thanx Ashfield for ur reply I figured it out earlier itself and have implemented it almost the same way. copying my code incase it helps anyone else. <pre> LEN(CONVERT(varchar, DecryptByKey(Postcode))) as Postcodeln,      case when LEN(CONVERT(varchar, DecryptByKey(Postcode))) = '7' then             substring(CONVERT(varchar, DecryptByKey(Postcode)),1,4)      else           substring(CONVERT(varchar, DecryptByKey(Postcode)),1,3)      end Postcode1,      case when LEN(CONVERT(varchar, DecryptByKey(Postcode))) = '6' then             substring(CONVERT(varchar, DecryptByKey(Postcode)),4,6)      else           substring(CONVERT(varchar, DecryptByKey(Postcode)),5,7)      end Postcode2, </pre> Many Thanks

          A Offline
          A Offline
          Ashfield
          wrote on last edited by
          #4

          No problem, glad to have helped

          Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

          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