본문 바로가기
MS-SQL/CLR-UserDefinedFunctions

[SQL CLR] 특정 문자 구분(Split) 형식으로 문자열 나누기

by Hwoarang757 2013. 11. 5.

using System;

using System.Data;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using Microsoft.SqlServer.Server;

 

public partial class UserDefinedFunctions

{

    [Microsoft.SqlServer.Server.SqlFunction]

    public static SqlString UDF_SPLIT_STRING(SqlString strSplit , SqlString strGubunJa, SqlInt16 intindex)

    {

       

        if(string.IsNullOrEmpty(strSplit.ToString()) || string.IsNullOrEmpty(strGubunJa.ToString()) || string.IsNullOrEmpty(intindex.ToString()))

        {

            return new SqlString("값이 잘못되었습니다");

        }

 

        string strSplitString = strSplit.ToString();

        char[] strSplitGubunJa = strGubunJa.ToString().ToCharArray(0,1);

        string[] StrResult = new string[64];

 

 

        if (strSplitString.Contains(strSplitGubunJa.GetValue(0).ToString()))

        {

            StrResult = strSplitString.Split(strSplitGubunJa);

        }

        else

        {

            return new SqlString("");

        }

 

        if (int.Parse(intindex.ToString()) >= StrResult.Length)

        {

            return new SqlString("인덱스가 잘못되었습니다.");

        }

 

        return new SqlString(StrResult.GetValue(int.Parse(intindex.ToString())).ToString());

    }

}

;

 

허접 하지만 간단한 예제입니다 ;;;;