숫자와 문자 각각 추출


설명 : strAlphaNumeric 에 있는 표현 값을 strAlpha(문자)와 strAlphaNumeric(숫자)로 나눠서 출력


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
DECLARE @strAlphaNumeric varchar(10)
DECLARE @intAlpha INT 
DECLARE @strAlpha varchar(10)

SET @strAlphaNumeric = 'Ww00004'

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

SET @strAlpha = ''

BEGIN 
    WHILE @intAlpha > 0 
    BEGIN 
        Set @strAlpha = @strAlpha + SUBSTRING(@strAlphaNumeric,@intAlpha,1)
        SET @strAlphaNumeric = Stuff(@strAlphaNumeric, @intAlpha, 1''
        SET @intAlpha = Patindex('%[^0-9]%', @strAlphaNumeric) 
    END 
    Set @strAlphaNumeric = MAX(@strAlphaNumeric)
    SELECT @strAlpha ,@strAlphaNumeric
END 
cs



MSSQL에서 제공해주는 내장함수

Patindex : 유효한 text 및 character 데이터 형식에 패턴에 대한 시작 위치를 반환


구문

PATINDEX ( '%pattern%', expression)


예 )

 Select Patindex('%[^0-9]%', 'D0001')  as '숫자가 아닌 글자 인덱스'


패턴 ('%pattern%') 종류 : 



Stuff  :   문자열(charater_expression)을 다른 문자열로 추가한다. 문자열은 시작점(Start position)에서 문자열안에 명시된 (length)만큼 삭제한 다음 두번째 문자열(replaceWith_expression)을 시작 위치의 첫 번째 문자열에 삽입합니다.


구문 

STUFF (character_expression , start , length , replaceWith_expression )


예)

SELECT STUFF('안녕하세요' ,1,1,'')


SUBSTRING : 문자열이나, 바이너리, 텍스트 또는 이미지 표현에 대한 부분값을 리턴합니다.


구문

SUBSTRING ( expression ,start , length )


예 )

SELECT SUBSTRING('안녕하세요',2,1)




Posted by Hoya0415
,