Category

마메의여행기 (252)
사진 (84)
여행 (11)
등산 (30)
정보 (91)
개발 (36)

Search

Blog Menu

[예제로 배우는 MS-SQL] 전각문자 공백제거 - REPLACE, LTRIM, RTRIM

DROP FUNCTION ToBothTrim
GO

/*************************************************/
/*                  文字列の両方Trimする関数                                    */
/*************************************************/
/*        引数 : @STR      → 文字列                                                */
/*               @DIVISION → half(半角), full(全角)                            */
/*                                                                                             */
/* ※ 結果                                                                                  */
/*    △→半角スペース、□→全角スペース                                      */
/*                                                                                             */
/*    ① 半角Trim                                                                         */
/*       引数 : △△A△B△C△△, half                                             */
/*       結果 : A△B△C                                                                  */
/*    ① 全角Trim                                                                          */
/*       引数 : △□文△字□列△□, full                                            */
/*       結果 : 文□字□列                                                               */
/*                                                                                              */
/*************************************************/

CREATE FUNCTION ToBothTrim(@STR VARCHAR(1200), @DIVISION VARCHAR(4))
 RETURNS VARCHAR(1200)
AS
BEGIN

 SELECT @STR = (
       CASE @DIVISION
        WHEN 'half' THEN  LTRIM(RTRIM(@STR))
  WHEN 'full' THEN  REPLACE(LTRIM(RTRIM(REPLACE(@STR, ' ', ' '))), ' ', ' ')
        ELSE @STR
  END
 )
  
 RETURN @STR
END

GO

GRANT EXECUTE ON ToBothTrim TO af_user
GO