0

I have string:

010-234-336-.

and I want to return only number like:

010234336. 

Does anyone know how to format this string?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Chhornkimchheng
  • 197
  • 2
  • 12

4 Answers4

3

Try this

Select Replace('010-234-336-', '-', '')

In case you have other string and want to only numeric portion, then try below code.

Declare @strAlphaNumeric varchar(256) = '010-abnasd234-336-'

DECLARE @intAlpha INT
SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric)

WHILE @intAlpha > 0
 BEGIN
    SET @strAlphaNumeric = STUFF(@strAlphaNumeric, @intAlpha, 1, '' )
    SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric )
 END

Select ISNULL(@strAlphaNumeric,0)

I got above solution :- Query to get only numbers from a string

Community
  • 1
  • 1
Ajay2707
  • 5,690
  • 6
  • 40
  • 58
0
SELECT CONVERT (INT,REPLACE('010-234-336-.','-',''))
Norbert Forgacs
  • 605
  • 1
  • 8
  • 27
0
SELECT Replace('010-234-336-', '-', '')

You can also cast or convert to integer if desired too.

SELECT CAST(Replace('010-234-336-', '-', '') AS INT) as value

SELECT CONVERT(INT, Replace('010-234-336-', '-', '')) as value 
Ajay2707
  • 5,690
  • 6
  • 40
  • 58
milltonion
  • 43
  • 1
  • 9
0

Here's a function I had made to let me remove all non-numeric characters from a string, but leave the result as a string. This was so I didn't lose any zeros from the beginning of the entry.

CREATE FUNCTION dbo.fnsStripToNumber
(
    @inString   varchar(500) = NULL
)
RETURNS varchar(500) AS
BEGIN

    /***********************************************************************
    Object      :   User Defined Scalar Function [dbo].[fnsStripToNumber]
    Script Date :   <date: 2016-04-20>
    Author      :   Just Me
    Description :   Remove all non-numeric characters from a string.
    Updates     :   2016-04-20 - JustMe
                        Updated the script for speed.
                        Moved the function to follow coding standards.
    Test Script :   
                    SELECT
                        ODS.dbo.fnsStripToNumber('ts45my12fnc098.     ') AS strNumber

    ***********************************************************************/

    DECLARE
        @strReturn  varchar(500) = '',
        @intEnPos   int          = 1
    --
    --  Loop through the string from beginning to end looking for any numbers.
    --
    WHILE @intEnPos > 0
    BEGIN
        --
        --  This is like a progressive update SELECT statement.
        --  The position of the next space is first found.
        --  This updated value is used in the next variable assignment to parse the
        --  string unit.
        --  The starting position for the next string unit is updated.
        --  Modify the string unit and update the return string.
        --
        SELECT
            @intEnPos = PATINDEX('%[0-9]%', @inString),
            @strReturn +=   (
                                CASE
                                    WHEN @intEnPos > 0 THEN SUBSTRING(@inString, @intEnPos, 1)
                                    ELSE ''
                                END
                            ),
            @inString = RIGHT(@inString, DATALENGTH(@inString) - @intEnPos)
    END
    --
    --  Return the proper cased string.
    --
    RETURN @strReturn

END;

GO