0

I have string:

010-234-336-.

and I want to return only number like:

010234336. 

Does anyone know how to format this string?

3
  • You could replace '-' with '' Commented May 3, 2017 at 5:08
  • Possible duplicate of Query to get only numbers from a string Commented May 3, 2017 at 5:11
  • Also you can, SELECT REPLACE('010-234-336', '-', '') * 1 Commented May 3, 2017 at 5:27

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

1 Comment

It is still a string, you have to convert it to int
0
SELECT CONVERT (INT,REPLACE('010-234-336-.','-',''))

1 Comment

Some explaination or a link to the documentation might improve the quality of this answer further.
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 

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.