김종열
MS SQL Server MVP
엔트리브 소프트
회원을 관리하면서 음력의 생일을 가진 회원을 양력기준으로 메시지를 보내는 경우가 있었나요?
머 특별하게 함수를 만들어 사용하셔도 되겠지만, 이런 간단한 이슈로 음력을 양력으로 변환해봅니다.
머 왜 양력을 음력으로 변환하지 않는가?
아마도 양력을 음력으로 변환하는 것은 특별히 신년운세를 보는 그런 곳에 제가 종사를 해본적이
없기에 필요하신 분들이 직접 만들기를 기원하며..
C#을 아주 끝에만 다루어보지만 함수가 필요해서 msdn을 뒤져보면 이미 정의가 다 되어 있는 참 편리(?)하다는 생각을 해봅니다.
달력과 관련되는 부분을 사용하기 위해서는
"using System.Globalization"
이런 선언을 하고 난뒤 달력 class를 사용하시면 됩니다.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Globalization;
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlDateTime Udf_Lunar2Solar(SqlDateTime sDt)
{
DateTime dt = (DateTime)sDt;
bool bExistLeap = false;
KoreanLunisolarCalendar kr_Lunnar = new KoreanLunisolarCalendar();
int _lunnarYY = kr_Lunnar.GetYear(dt);
int _lunnarMM = kr_Lunnar.GetMonth(dt);
int _lunnarDD = kr_Lunnar.GetDayOfMonth(dt);
if (kr_Lunnar.GetMonthsInYear(_lunnarYY) > 12) //12보다 큰달은 윤달이 있다는.
{
bExistLeap = kr_Lunnar.IsLeapMonth(_lunnarYY, _lunnarMM); // 윤달에 대한 true or false
int intLeap_mm = kr_Lunnar.GetLeapMonth(_lunnarYY); //윤달 추출
if (_lunnarMM >= intLeap_mm)
{
_lunnarMM--;
}
}
return Convert.ToDateTime(_lunnarYY.ToString() + "-" + _lunnarMM.ToString() + "-" + _lunnarDD.ToString());
}
};
그리고 이를 확인하는 소스코드입니다.
with sTB as (
select x = dbo.Udf_Lunar2Solar(cast('1985-03-19 ' as datetime))
, y = dbo.Udf_Lunar2Solar('19850319')
, z = dbo.Udf_Lunar2Solar('1985/03/19 ')
)
select * from sTB
/*
x y z
----------------------- ----------------------- -----------------------
1985-01-28 00:00:00 .000 1985-01-28 00:00:00 .000 1985-01-28 00:00:00 .000
(1개행이영향을받음)
*/
첨부 : Udf_Lunar2Solar.cs
Udf_Lunar2Solar.sql
'연구개발 > CLR' 카테고리의 다른 글
CLR-server와 통신하기 (1) | 2009.06.22 |
---|---|
CLR-User Definded Aggregatiion (0) | 2009.06.22 |
CLR Utf8String - UDT (0) | 2009.06.22 |
CLR - Mail 보내기 (0) | 2009.06.22 |
CLR - FTP download(FTP 2탄) (0) | 2009.06.22 |