문자열내 숫자가 아닌 문자 제거
페이지 정보
작성자 서방님 댓글 0건 조회 127회 작성일 07-05-23 10:57본문
출처 : http://swynk.com
내용 : SQL2000의 UDF를 이용해 문자열내 데이터중 숫자가 아닌 문자들을
제거하는 사용자 정의 함수.
아마도 유용하게 쓰실 겁니다.
전화번호는 물론이고 여러가지 응용도 가능하실 겁니다.
--Phone컬럼의 자료를 본다.
SELECT CustomerID, Phone FROM Customers
-- "-" 기호와 ".", "()" 등의 숫자가 아닌 문자열 데이터가 있다.
--사용자 정의 함수를 생성한다.
CREATE FUNCTION sp_numericOnlyString (@InString VARCHAR(20))
RETURNS VARCHAR(20)
AS
BEGIN
DECLARE @cleanString VARCHAR(20)
--initialize String to not null
SET @cleanString = ''
--create a variable to use in looping thru string
DECLARE @position INT
SET @position = 1
--get length of input string
DECLARE @strLength INT
SET @strLength = LEN(@InString)
--start looping
WHILE @position <= @strLength
BEGIN
--get ascii table number and find out if is a number
IF ASCII(SUBSTRING(@InString, @position, 1))BETWEEN 48 AND 57
BEGIN
SET @cleanString = @cleanString +
SUBSTRING(@InString, @position, 1)
END
--increment to next character
SET @position = @position + 1
END
RETURN (@cleanString)
END
--사용자 정의 함수를 이용해 다시 데이터를 조회해 본다.
SELECT CustomerID, dbo.sp_numericOnlyString(Phone) AS Phone FROM Customers
댓글목록
등록된 댓글이 없습니다.