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. Remove empty spaces in a String in TSQL

Remove empty spaces in a String in TSQL

Scheduled Pinned Locked Moved Database
csharpasp-netsql-servercom
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.
  • V Offline
    V Offline
    Vimalsoft Pty Ltd
    wrote on last edited by
    #1

    Good Day All i have the Following string in a Field

    'This
    is the

    reason that i did this>> '

    i want to remove spaces in TSQL to this

    'This is the reason that i did this>>'

    THanks

    Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

    B P R 3 Replies Last reply
    0
    • V Vimalsoft Pty Ltd

      Good Day All i have the Following string in a Field

      'This
      is the

      reason that i did this>> '

      i want to remove spaces in TSQL to this

      'This is the reason that i did this>>'

      THanks

      Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

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

      here it is

      declare @NewLine char(2)
      set @NewLine=char(13)+char(10)

      select rtrim(ltrim(Replace('This
      is the

      reason that i did this>> ',@NewLine,'')))


      I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

      V P 2 Replies Last reply
      0
      • B Blue_Boy

        here it is

        declare @NewLine char(2)
        set @NewLine=char(13)+char(10)

        select rtrim(ltrim(Replace('This
        is the

        reason that i did this>> ',@NewLine,'')))


        I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

        V Offline
        V Offline
        Vimalsoft Pty Ltd
        wrote on last edited by
        #3

        You are a Star thanks :)

        Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

        B 1 Reply Last reply
        0
        • V Vimalsoft Pty Ltd

          Good Day All i have the Following string in a Field

          'This
          is the

          reason that i did this>> '

          i want to remove spaces in TSQL to this

          'This is the reason that i did this>>'

          THanks

          Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          I wrote a CLR function to do that, compresses whitespace to a single SPACE:

          namespace PIEBALD.Lib.LibExt.Compress
          {
          public static partial class LibExt
          {
          public static string
          Compress
          (
          this string Victim
          )
          {
          System.Text.StringBuilder result = new System.Text.StringBuilder ( Victim.Length ) ;

            bool first = true ;
          
            for ( int i = 0 ; i < Victim.Length ; i++ )
            {
              if ( !System.Char.IsWhiteSpace ( Victim \[ i \] ) )
              {
                result.Append ( Victim \[ i \] ) ;
          
                first = true ;
              }
              else if ( first )
              {
                result.Append ( ' ' ) ;
          
                first = false ;
              }
            }
          
            return ( result.ToString() ) ;
          }
          

          }
          }

          CREATE ASSEMBLY Library FROM 'C:\bin\Library.Sql.dll'
          GO
          CREATE FUNCTION dbo.Compress(@Source NVARCHAR(MAX)) RETURNS NVARCHAR(MAX)
          AS EXTERNAL NAME Library.[PIEBALD.Lib.LibExt.Compress.LibExt].Compress;
          GO

          1 Reply Last reply
          0
          • V Vimalsoft Pty Ltd

            You are a Star thanks :)

            Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

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

            You are welcome. :)


            I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

            1 Reply Last reply
            0
            • B Blue_Boy

              here it is

              declare @NewLine char(2)
              set @NewLine=char(13)+char(10)

              select rtrim(ltrim(Replace('This
              is the

              reason that i did this>> ',@NewLine,'')))


              I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              That might not be what you want if the string is 'hello\r\nworld'.

              1 Reply Last reply
              0
              • V Vimalsoft Pty Ltd

                Good Day All i have the Following string in a Field

                'This
                is the

                reason that i did this>> '

                i want to remove spaces in TSQL to this

                'This is the reason that i did this>>'

                THanks

                Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

                R Offline
                R Offline
                RobLazarus
                wrote on last edited by
                #7

                Here's a generic example:

                create table t (s sysname)
                insert into t select 'this is the reason that I did this. '

                -- convert tabs to spaces
                update	t set s = replace(s, '	',' ')
                where	charindex('	', s) > 0
                
                -- now do the work.
                while 1=1
                begin
                	update t
                	set		s = substring(s, 1, charindex('  ', s, 1)-1) + ' ' + ltrim(substring(s,charindex('  ', s, 1), 8000))
                	where	charindex('  ', s, 1) > 0
                
                	if @@rowcount = 0
                		break
                end
                
                select	s
                from	t
                
                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