Results 1 to 8 of 8

Thread: [RESOLVED] Replacing Left of a character

  1. #1

    Thread Starter
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Resolved [RESOLVED] Replacing Left of a character

    Hi,

    MSSQL 2000 question:

    I need an SQL statement that would take e.g. "TAG/ 123456" and delete everything left of the forward slash giving me a result of "123456". I can not give a count as there are many values that may vary.

    Select Replace(partname????????) from orders

    Thanks
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

  2. #2
    Addicted Member
    Join Date
    Feb 2008
    Location
    Hamburg
    Posts
    138

    Re: Replacing Left of a character


  3. #3
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Replacing Left of a character

    MSSQL has a Right() function you could use.

    It goes as follows: RIGHT ( character_expression , integer_expression )

    So something like right(TAG/123456, 6) would work.

    Again, depending on which language you're using, you could use a function to get the location of the /, and use the right function to grab anything before that character.

    Or you could use a split function to split on "/" and grab the second index.

  4. #4

    Thread Starter
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Re: Replacing Left of a character

    Like I said, I can not set a count. Too many variables in the size of the string right of the forward slash. Same goes for the left side also.
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

  5. #5
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Replacing Left of a character

    Execute this code in the database you are using. It will create a function that will remove everything from the string except numbers.
    sql Code:
    1. CREATE FUNCTION [dbo].[fn_GetNumbers](@Str NVARCHAR(MAX))
    2. RETURNS NVARCHAR(MAX)
    3. AS
    4. BEGIN
    5.     -- SELECT dbo.fn_GetNumbers('testing.\.234...77..116/ghj (&(*&()((')
    6.    
    7.     DECLARE @RetVal NVARCHAR(MAX)
    8.     DECLARE @I INT
    9.    
    10.     SET @RetVal = @Str
    11.     SET @I = 1
    12.    
    13.     WHILE @I <= LEN(@RetVal) BEGIN
    14.         IF CHARINDEX(SUBSTRING(@RetVal, @I, 1), '0123456789') = 0 BEGIN
    15.             SET @RetVal = REPLACE(@RetVal, SUBSTRING(@RetVal, @I, 1), '')
    16.             SET @I = @I - 1
    17.         END
    18.        
    19.         SET @I = @I +1
    20.     END
    21.    
    22.     RETURN @RetVal
    23. END

  6. #6

    Thread Starter
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Re: Replacing Left of a character

    I need everything to the left of the forward slash and the slash it self. I will not rule out the the string may be TAG/ 123AB23 or TAG/ WE4051 or AB422/ 1234567890
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

  7. #7
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Replacing Left of a character

    You are contradicting yourself, in the first post you said you need the numbers, now you say you want everything to the left of slash....

    But I think you mean everything AFTER the slash ? right ?
    Something like this maybe ?
    sql Code:
    1. SELECT LTRIM(SUBSTRING('AB422/ 1234567890', CHARINDEX('/', 'AB422/ 1234567890') + 1, 1000))

  8. #8

    Thread Starter
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Resolved Re: Replacing Left of a character

    That it!!!!!

    Thank you!!!!
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width