database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
ㆍMS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
MS-SQL Q&A 2058 게시물 읽기
No. 2058
오늘날짜를 기준으로 지난달의 시작일과 끝일 구하기
작성자
석이
작성일
2005-06-27 14:25
조회수
5,240

 

declare @lastmonthStartday datetime
declare @lastmonthEndday datetime
declare @nowdate datetime
select @nowdate = replace(convert(varchar(20),getdate(),102),'.','')
-- for debug
select @nowdate = '20041201'
declare @year char(4),@month char(2),@day char(2)
declare @nextmonth char(2)
declare @lastmonth char(2)
set @month = datepart(mm,@nowdate)

if @month = 1
begin
set @year = datepart(yy,@nowdate)
set @year = @year - 1
end
else set @year = datepart(yy,@nowdate)


select @lastmonth = @month - 1
if @lastmonth = 0 set @lastmonth = 12
select @nextmonth = @month + 1
set @day = datepart(dd,@nowdate)
if (select len(@month)) = 1 select @month = '0' + @month
if (select len(@lastmonth)) = 1 select @lastmonth = '0' + @lastmonth
if (select len(@nextmonth)) = 1 select @nextmonth = '0' + @nextmonth

select @lastmonthStartday = @year + @lastmonth + '01'
set @year = datepart(yy,@nowdate)
select @lastmonthEndday = @year + @month + '01'
select @lastmonthEndday = @lastmonthEndday - 1

select @lastmonthStartday
select @lastmonthEndday

이 글에 대한 댓글이 총 7건 있습니다.

구하긴 구했는데 더 좋은 방법이 있을런지요?

 

석이님이 2005-06-27 14:26에 작성한 댓글입니다. Edit

declare @nowDate varchar(10)
,  @lastmonthDayS varchar(10)
,  @lastmonthDayE  varchar(10)
,  @sdayFlag int


select @nowDate = convert(varchar(10), getDate(), 111 )

select @sDayFlag = cast(right(@nowdate, 2) as int)
if @sDayFlag >= 2
 select @lastMonthDayS = convert(varchar(10), dateAdd(day, -@sDayFlag+1, dateAdd(month, -1, cast(@nowDate as datetime))) , 111)
else
 select @lastmonthDayS = convert(varchar(10), dateAdd(month, -1,cast(@nowDate as datetime)), 111)


select @lastMonthDayE = convert(varchar(10), dateAdd(day, -1, dateAdd(month, 1, cast(@lastMonthDayS as datetime))), 111)

select @lastMonthDays
select @lastMonthDayE

 


 

 

여리님이 2005-06-27 14:48에 작성한 댓글입니다. Edit

역쉬 최고 입니다. 더 좋은거 빨빨 내놔들 보세요 ^-------^

석이님이 2005-06-27 15:01에 작성한 댓글입니다. Edit

2탄

그럼 지난주시작일과 끝일은?

 


-- -------------------------------
-- 지난주 수익률을 계산하기 위한것
-- -------------------------------
-- 날짜연산을 위한 변수 지정
declare @nowdate datetime
declare @lastWeekStartday datetime
declare @lastWeekEndday datetime
declare @thisWeekStartday datetime
declare @thisWeekEndday datetime


declare @whatday int

-- 오늘날짜 입력
select @nowdate = replace(convert(varchar(20),getdate(),102),'.','')
-- DEBUG
 set @nowdate = '20050624'

-- 저번주 시작일 끝일 찾기
-- 월요일 부터 금요일 까지
select @whatday = datepart(dw,@nowdate)
-- select @whatday
set @lastWeekStartday = @nowdate - (5 + @whatday)
set @lastWeekEndday = @nowdate - (@whatday+1)
set @thisWeekStartday = @nowdate - (@whatday - 2)
set @thisWeekEndday = @nowdate


select @thisWeekStartday
select @thisWeekEndday
select @lastWeekStartday
select @lastWeekEndday

석이님이 2005-06-27 15:18에 작성한 댓글입니다.
이 댓글은 2005-06-27 15:20에 마지막으로 수정되었습니다. Edit

3탄 !!!

 

this is asp ^-^

 

 

<%
Dim LastDateF, LastDateL

LastDateF = DateSerial(Year(now()),Month(now())-1,1)
LastDateL = DateSerial(Year(now()),Month(now()),1-1)

response.write "시작날짜 = " & LastDateF & "<br>끝날짜 = " & LastDateL & "."
%>

석이님이 2005-06-27 15:26에 작성한 댓글입니다. Edit

 MS-SQL에서만 이용을 하다면 아래와 같이 해도 좋을 것 같네요..

 

DECLARE @PreMonth char(7)
              ,@firstDay datetime
              ,@endDay datetime


SELECT @PreMonth= CONVERT(CHAR(7), DATEADD(mm,-1,GETDATE()),121)

SELECT @firstDay = CONVERT(datetime, @PreMonth+'-01')
SELECT @endDay = DATEADD(d,-1, DATEADD(mm,1, @firstDay))
SELECT @firstDay,@endDay

 

가을남자님이 2005-07-02 14:48에 작성한 댓글입니다. Edit

멋지십니다.

 

아주 간단하고 심플합니다. 또한 원리를 잘 알고 있는 것

같군요 ^-^

석이님이 2005-07-02 17:34에 작성한 댓글입니다. Edit
[Top]
No.
제목
작성자
작성일
조회
2061필드의 코멘트를 알수 있을까요 [2]
peter
2005-06-28
2683
2060정기 작업을 만들려는데 변환좀 해주세요...^^;; [3]
peter
2005-06-28
2201
2059컴퓨터가 껐다 켜지면 로그인이 안됩니다_수정 [1]
이상합니다
2005-06-27
2273
2058오늘날짜를 기준으로 지난달의 시작일과 끝일 구하기 [7]
석이
2005-06-27
5240
2056이것이 검색인수로 인식되나요? 비검색인수로 되나요? [1]
고영훈
2005-06-27
1905
2055ms access limit 쿼리... [1]
박기훈
2005-06-27
3568
2054문자를일정한 규칙으로 자를려고.. [4]
도와주세요
2005-06-27
3093
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.021초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다