November 13, 2009 at 11:57 pm
my statement as follow,
declare @yyyymm as varchar(6)
set @yyyymm='200910'
i want my resultset as follow,
Prev yyyymm | Next yyyymm
----------------------------------
200909 | 200911
How my SQL look's like?
-- yyyymm format is 200902, 200905, 200911, 201002, 201011 and so on
November 14, 2009 at 12:54 am
Here you go:
declare @yyyymm as varchar(6)
set @yyyymm='200910'
select
CONVERT(varchar(6), DATEADD(mm, CAST(right(@yyyymm,2) as int) - 2, DATEADD(yy, left(@yyyymm,4) - 1900, 0)), 112),
CONVERT(varchar(6), DATEADD(mm, CAST(right(@yyyymm,2) as int), DATEADD(yy, left(@yyyymm,4) - 1900, 0)), 112)
November 14, 2009 at 5:14 am
tq very much sir
November 14, 2009 at 1:26 pm
Just another slant on this...
declare @yyyymm as varchar(6)
set @yyyymm='200901'
SELECT CONVERT(CHAR(6),CAST(@yyyymm+'01' AS DATETIME) -1,112) AS [Prev yyyymm],
CONVERT(CHAR(6),CAST(@yyyymm+'01' AS DATETIME)+32,112) AS [Next yyyymm]
My question would be two-fold... 1) why are you formatting dates in SQL instead of the GUI and 2) why are you doing it just one row at a time?
--Jeff Moden
Change is inevitable... Change for the better is not.
November 14, 2009 at 1:35 pm
Hi
First, to pay my bill for entering this thread 😀
SELECT
CONVERT(CHAR(6), DATEADD(MONTH, -1, CONVERT(DATETIME, @yyyymm + '01', 112)), 112)
,CONVERT(CHAR(6), DATEADD(MONTH, 1, CONVERT(DATETIME, @yyyymm + '01', 112)), 112)
Second, a question - and the more important part of my post:
Why do you save date values as text?
Greets
Flo
November 14, 2009 at 11:10 pm
Jeff Moden (11/14/2009)
Just another slant on this...
declare @yyyymm as varchar(6)
set @yyyymm='200901'
SELECT CONVERT(CHAR(6),CAST(@yyyymm+'01' AS DATETIME) -1,112) AS [Prev yyyymm],
CONVERT(CHAR(6),CAST(@yyyymm+'01' AS DATETIME)+32,112) AS [Next yyyymm]
My question would be two-fold... 1) why are you formatting dates in SQL instead of the GUI and 2) why are you doing it just one row at a time?
Me partition table followed by tbl_YYYYMM. When user passing the date, my program need to seek which table.
Me partition table into tbl_YYYYMM because of a i dont want to store a lot of row (million row) in a one table. -- me still looking the best pactice for this
November 14, 2009 at 11:11 pm
Florian Reischl (11/14/2009)
HiFirst, to pay my bill for entering this thread 😀
SELECT
CONVERT(CHAR(6), DATEADD(MONTH, -1, CONVERT(DATETIME, @yyyymm + '01', 112)), 112)
,CONVERT(CHAR(6), DATEADD(MONTH, 1, CONVERT(DATETIME, @yyyymm + '01', 112)), 112)
Second, a question - and the more important part of my post:
Why do you save date values as text?
Greets
Flo
me partition table into tbl_yyyyMM
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply