주어진 날짜의 이전 달 마지막 날짜를 알려주는 SQL 스크립트 > db

본문 바로가기
사이트 내 전체검색

db

주어진 날짜의 이전 달 마지막 날짜를 알려주는 SQL 스크립트

페이지 정보

작성자 서방님 댓글 0건 조회 154회 작성일 06-09-13 11:15

본문

이 SQL 스크립트는 SQL 서버 2000에서 작동하는 사용자 정의 함수이다. SQL 서버에서 새로운 사용자 정의 함수를 등록한 후 사용하면 된다. 이 스크립트는 파라미터로 넘긴 날짜의 바로 이전 달 마지막 날짜를 알려 준다.

만일 오늘 날짜가 2004년 4월 20일이라면,

select dbo.LastDayofPreviousMonth(getdate())
--결과 2004-03-31 00:00:00.000

select dbo.LastDayofPreviousMonth('01/12/2004')
--결과 2003-12-31 00:00:00.000

select dbo.LastDayofPreviousMonth('03/04/2004')
--결과 2004-02-29 00:00:00.000

이 SQL 스크립트 소스 코드는 다음과 같다.

create function dbo.LastDayofPreviousMonth (@date datetime)
returns datetime
as
begin
declare @returndate datetime
set @returndate =convert(datetime,right('00'+convert(varchar(2),
  month(@date)),2)+'/01/'+convert(varchar(4),year(@date)))-1
return @returndate
 
end

자료출처 : http://korea.internet.com/

댓글목록

등록된 댓글이 없습니다.

회원로그인

접속자집계

오늘
129
어제
225
최대
1,347
전체
154,888
Latest Crypto Fear & Greed Index

그누보드5
Copyright © 서방님.kr All rights reserved.