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. T-SQL Address line Packing

T-SQL Address line Packing

Scheduled Pinned Locked Moved Database
questiondatabase
6 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.
  • P Offline
    P Offline
    Pascal Ganaye
    wrote on last edited by
    #1

    I am trying to pack four address lines, to leave no blank between the lines and no null values. The code below is so horrid, it could qualify quite well to the coding horror forum. This is also a question so I post it here. The question is : - How would you write this more elegantly ? It would be fun to compare various other ways, I will perhaps post some

    DECLARE @line1 AS VARCHAR(255)
    DECLARE @line2 AS VARCHAR(255)
    DECLARE @line3 AS VARCHAR(255)
    DECLARE @line4 AS VARCHAR(255)

    SET @line1 = '12 orchard st '
    SET @line2 = NULL
    SET @line3 = ' '
    SET @line4 = 'BATH'

    SET @line1 = RTRIM(LTRIM(COALESCE(@line1,'')))
    SET @line2 = RTRIM(LTRIM(COALESCE(@line2,'')))
    SET @line3 = RTRIM(LTRIM(COALESCE(@line3,'')))
    SET @line4 = RTRIM(LTRIM(COALESCE(@line4,'')))

    DECLARE @cpt INTEGER

    WHILE @line1 = '' AND @cpt<3
    BEGIN
    SET @line1 = @line2
    SET @line2 = @line3
    SET @line3 = @line4
    SET @line4 = ''
    SET @cpt = @cpt + 1
    END

    SET @cpt = 0
    WHILE @line2 = '' AND @cpt<2
    BEGIN
    SET @line2 = @line3
    SET @line3 = @line4
    SET @line4 = ''
    SET @cpt = @cpt + 1
    END

    IF @line3 = ''
    BEGIN
    SET @line3 = @line4
    SET @line4 = ''
    END

    PRINT 'Line 1:' + @line1
    PRINT 'Line 2:' + @line2
    PRINT 'Line 3:' + @line3
    PRINT 'Line 4:' + @line4

    G L 2 Replies Last reply
    0
    • P Pascal Ganaye

      I am trying to pack four address lines, to leave no blank between the lines and no null values. The code below is so horrid, it could qualify quite well to the coding horror forum. This is also a question so I post it here. The question is : - How would you write this more elegantly ? It would be fun to compare various other ways, I will perhaps post some

      DECLARE @line1 AS VARCHAR(255)
      DECLARE @line2 AS VARCHAR(255)
      DECLARE @line3 AS VARCHAR(255)
      DECLARE @line4 AS VARCHAR(255)

      SET @line1 = '12 orchard st '
      SET @line2 = NULL
      SET @line3 = ' '
      SET @line4 = 'BATH'

      SET @line1 = RTRIM(LTRIM(COALESCE(@line1,'')))
      SET @line2 = RTRIM(LTRIM(COALESCE(@line2,'')))
      SET @line3 = RTRIM(LTRIM(COALESCE(@line3,'')))
      SET @line4 = RTRIM(LTRIM(COALESCE(@line4,'')))

      DECLARE @cpt INTEGER

      WHILE @line1 = '' AND @cpt<3
      BEGIN
      SET @line1 = @line2
      SET @line2 = @line3
      SET @line3 = @line4
      SET @line4 = ''
      SET @cpt = @cpt + 1
      END

      SET @cpt = 0
      WHILE @line2 = '' AND @cpt<2
      BEGIN
      SET @line2 = @line3
      SET @line3 = @line4
      SET @line4 = ''
      SET @cpt = @cpt + 1
      END

      IF @line3 = ''
      BEGIN
      SET @line3 = @line4
      SET @line4 = ''
      END

      PRINT 'Line 1:' + @line1
      PRINT 'Line 2:' + @line2
      PRINT 'Line 3:' + @line3
      PRINT 'Line 4:' + @line4

      G Offline
      G Offline
      Goutam Patra
      wrote on last edited by
      #2

      If you want to just make a single string then there is a solution

      DECLARE @line1 AS VARCHAR(255)
      DECLARE @line2 AS VARCHAR(255)
      DECLARE @line3 AS VARCHAR(255)
      DECLARE @line4 AS VARCHAR(255)
      DECLARE @line5 AS VARCHAR(2000)

      SET @line1 = '12 orchard st '
      SET @line2 = NULL
      SET @line3 = ' '
      SET @line4 = 'BATH'

      SET @line5 = (SELECT RTRIM(LTRIM(COALESCE(@line1,''))) +
      CASE WHEN LEN(RTRIM(LTRIM(COALESCE(@line2,'')))) > 0 THEN CHAR(13) ELSE '' END + RTRIM(LTRIM(COALESCE(@line2,''))) +
      CASE WHEN LEN(RTRIM(LTRIM(COALESCE(@line3,'')))) > 0 THEN CHAR(13) ELSE '' END + RTRIM(LTRIM(COALESCE(@line3,''))) +
      CASE WHEN LEN(RTRIM(LTRIM(COALESCE(@line4,'')))) > 0 THEN CHAR(13) ELSE '' END + RTRIM(LTRIM(COALESCE(@line4,''))))

      PRINT @line5

      P 1 Reply Last reply
      0
      • G Goutam Patra

        If you want to just make a single string then there is a solution

        DECLARE @line1 AS VARCHAR(255)
        DECLARE @line2 AS VARCHAR(255)
        DECLARE @line3 AS VARCHAR(255)
        DECLARE @line4 AS VARCHAR(255)
        DECLARE @line5 AS VARCHAR(2000)

        SET @line1 = '12 orchard st '
        SET @line2 = NULL
        SET @line3 = ' '
        SET @line4 = 'BATH'

        SET @line5 = (SELECT RTRIM(LTRIM(COALESCE(@line1,''))) +
        CASE WHEN LEN(RTRIM(LTRIM(COALESCE(@line2,'')))) > 0 THEN CHAR(13) ELSE '' END + RTRIM(LTRIM(COALESCE(@line2,''))) +
        CASE WHEN LEN(RTRIM(LTRIM(COALESCE(@line3,'')))) > 0 THEN CHAR(13) ELSE '' END + RTRIM(LTRIM(COALESCE(@line3,''))) +
        CASE WHEN LEN(RTRIM(LTRIM(COALESCE(@line4,'')))) > 0 THEN CHAR(13) ELSE '' END + RTRIM(LTRIM(COALESCE(@line4,''))))

        PRINT @line5

        P Offline
        P Offline
        Pascal Ganaye
        wrote on last edited by
        #3

        Then you would need SET @line1 = SUBSTRING(line5, [something here], [something here]) SET @line2 = SUBSTRING(line5, [something here], [something here]) SET @line3 = SUBSTRING(line5, [something here], [something here]) SET @line4 = SUBSTRING(line5, [something here], [something here]) Wouldn't you?

        G M 2 Replies Last reply
        0
        • P Pascal Ganaye

          Then you would need SET @line1 = SUBSTRING(line5, [something here], [something here]) SET @line2 = SUBSTRING(line5, [something here], [something here]) SET @line3 = SUBSTRING(line5, [something here], [something here]) SET @line4 = SUBSTRING(line5, [something here], [something here]) Wouldn't you?

          G Offline
          G Offline
          Goutam Patra
          wrote on last edited by
          #4

          Well, thats why i said in my post If you want it in a single string. and then this will not solve your purpose

          1 Reply Last reply
          0
          • P Pascal Ganaye

            Then you would need SET @line1 = SUBSTRING(line5, [something here], [something here]) SET @line2 = SUBSTRING(line5, [something here], [something here]) SET @line3 = SUBSTRING(line5, [something here], [something here]) SET @line4 = SUBSTRING(line5, [something here], [something here]) Wouldn't you?

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            Why do you insist on 4 variables, printing Patras solution will achieve the same output

            Never underestimate the power of human stupidity RAH

            1 Reply Last reply
            0
            • P Pascal Ganaye

              I am trying to pack four address lines, to leave no blank between the lines and no null values. The code below is so horrid, it could qualify quite well to the coding horror forum. This is also a question so I post it here. The question is : - How would you write this more elegantly ? It would be fun to compare various other ways, I will perhaps post some

              DECLARE @line1 AS VARCHAR(255)
              DECLARE @line2 AS VARCHAR(255)
              DECLARE @line3 AS VARCHAR(255)
              DECLARE @line4 AS VARCHAR(255)

              SET @line1 = '12 orchard st '
              SET @line2 = NULL
              SET @line3 = ' '
              SET @line4 = 'BATH'

              SET @line1 = RTRIM(LTRIM(COALESCE(@line1,'')))
              SET @line2 = RTRIM(LTRIM(COALESCE(@line2,'')))
              SET @line3 = RTRIM(LTRIM(COALESCE(@line3,'')))
              SET @line4 = RTRIM(LTRIM(COALESCE(@line4,'')))

              DECLARE @cpt INTEGER

              WHILE @line1 = '' AND @cpt<3
              BEGIN
              SET @line1 = @line2
              SET @line2 = @line3
              SET @line3 = @line4
              SET @line4 = ''
              SET @cpt = @cpt + 1
              END

              SET @cpt = 0
              WHILE @line2 = '' AND @cpt<2
              BEGIN
              SET @line2 = @line3
              SET @line3 = @line4
              SET @line4 = ''
              SET @cpt = @cpt + 1
              END

              IF @line3 = ''
              BEGIN
              SET @line3 = @line4
              SET @line4 = ''
              END

              PRINT 'Line 1:' + @line1
              PRINT 'Line 2:' + @line2
              PRINT 'Line 3:' + @line3
              PRINT 'Line 4:' + @line4

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              a bubble-sort approach could solve that; in pseudo-code:

              do {
              bool more=false;
              if (line1==empty && line2!=empty) {line1=line2; line2=empty; more=true;}
              if (line2==empty && line3!=empty) {line2=line3; line3=empty; more=true;}
              if (line3==empty && line4!=empty) {line3=line4; line4=empty; more=true;}
              while(more);

              :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

              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