Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9199747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:36:22+00:00 2026-06-17T22:36:22+00:00

I am trying to write a stored procedure in sql server 2008,I need to

  • 0

I am trying to write a stored procedure in sql server 2008,I need to remove unwanted spaces in the entries of my table.I categorized the entries in my table to 3 types.My store procedure should remove the spaces around single letter,like,

  • A G M words to AGM words
  • words A G M words to words AGM words
  • A G words to AG words

I tried following stored procedure.

CREATE proc At1 @name nvarchar(100)
as
declare @start int
declare @temp1 nvarchar(100)
declare @temp nvarchar(100)
declare @NthPosition int
declare @N int
set @N=LEN(@name)
set @start=1
set @temp1=''
set @temp=''


    set @NthPosition=charindex(' ',@name,@start)
    if(@NthPosition<>0)
    begin
    while (@NthPosition<>0 and @N<>0)
    begin
    set @temp1=SUBSTRING(@name,@start,@NthPosition-1)
    if(@temp<>'')
    begin
        if(len(@temp1)=1)
        begin
            set @temp=(@temp+@temp1)
        end
        else
        begin
            set @temp=(@temp+' '+@temp1)
        end
    end
    else
    begin
        set @temp=@temp1
    end
    set @start=@NthPosition+1
    set @N=@N-@NthPosition
    set @NthPosition=0
    set @NthPosition=CHARINDEX(' ',@name,@start)
    end
    end
    else
    begin 
            select @name
    end
          select @temp
        GO

and i used ,

    exec At1 'apple A G M mango' 

My expected result: apple AGM mango

But my actual result:apple

I am unable to figure out where the error is..Any suggestions in this regard is more helpful.
I tried to use computed column that would clear the space and i was able to find solution only for pattern #3.I am unable to frame a computed column definition suitable for all the 3 patterns….. Please share your thoughts that will be helpful to me

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:36:23+00:00Added an answer on June 17, 2026 at 10:36 pm

    I think this covers all the cases:

    CREATE proc At1 @Name nvarchar(100)
    as
    declare @New nvarchar(100)
    declare @SpacePos int
    declare @Single bit
    select @New = '',@Single = 0
    select @Name = LTRIM(@Name)
    
    while LEN(@name) > 0
    begin
        set @SpacePos = CHARINDEX(' ',@Name)
        if @SpacePos = 0 --No more spaces in the string
        begin
            select @New = @New + CASE WHEN @Single = 1 and LEN(@Name) > 1 THEN ' ' ELSE '' END + @Name,
            @Name = ''
        end
        else if @SpacePos = 2 --Single character "word"
        begin
            select @New = @New + SUBSTRING(@Name,1,1),
                @Name = SUBSTRING(@Name,3,100),
                @Single = 1
        end
        else --Multi-character word
        begin
            select @New = @New + CASE WHEN @Single = 1 THEN ' ' ELSE '' END + SUBSTRING(@Name,1,@SpacePos),
                @Name = SUBSTRING(@Name,@SpacePos+1,100),
                @Single = 0
        end
    end
    select @New
    go
    

    And the examples:

    exec At1 'apple A G M mango' 
    exec At1 'A G M words'
    exec At1 'words A G M'
    

    Produces:

    apple AGM mango
    AGM words
    words AGM
    

    (As a simplifying assumption, I assumed I was okay to remove any leading spaces from the original string. I also assume there are no double spaces in the string. If neither of those assumptions is accurate, a bit more work is required)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a fairly simple stored procedure in sql server. If the
I'm trying to write this as part of stored procedure on SQL Server 2000.
I am trying to write a stored procedure in sql server 2005 which will
I'm trying to write a stored procedure in SQL Server that will eliminate some
I am trying to write a Stored Procedure in SQL Server (2005) to do
The Problem I'm trying to write a stored procedure in SQL Server to find
I'm trying to write a stored procedure [SQL 2008] with a dynamic, auto incrementing
I'm first time trying to write a stored procedure in sql server and the
I am trying to write a stored procedure ( using SQL Server Management Studio
I am trying to write some integration tests for some SQL server stored procedures

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.