T-SQL trim   (and other non-alphanumeric characters)

后端 未结 5 1572
谎友^
谎友^ 2020-11-30 09:11

We have some input data that sometimes appears with   characters on the end.

The data comes in from the source system as varchar() and our attempts to cast a

5条回答
  •  攒了一身酷
    2020-11-30 10:09

    This page has a sample of how you can remove non-alphanumeric chars:

    -- Put something like this into a user function:
    DECLARE @cString    VARCHAR(32)
    DECLARE @nPos    INTEGER
    SELECT  @cString = '90$%45623 *6%}~:@'
    SELECT  @nPos = PATINDEX('%[^0-9]%', @cString)
    
    WHILE @nPos > 0
    BEGIN
    SELECT @cString = STUFF(@cString, @nPos, 1, '')
    SELECT  @nPos = PATINDEX('%[^0-9]%', @cString)
    END
    
    SELECT @cString 
    

提交回复
热议问题