June 4, 2011 at 10:26 pm
Craig Farrell (6/4/2011)
If Jeff is willing to put his name on the line, something he considers quite valuable, to prove a point with numbers... it's worth listening to.
+1000.
+ more if you actually follow his advice, and not just listen.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
June 5, 2011 at 9:56 am
WayneS (6/4/2011)
Craig Farrell (6/4/2011)
If Jeff is willing to put his name on the line, something he considers quite valuable, to prove a point with numbers... it's worth listening to.+1000.
+ more if you actually follow his advice, and not just listen.
Heh... Ok. After it gets dark tonight, face in the direction of Detroit. See the Red glare in the sky? That's me blushing. :blush: You folks are too kind. Thank you for the gift of confidence.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 6, 2011 at 12:46 pm
If I understand you correctly, use the dateadd function and subtract 1 from the number of months you wish to add.
DECLARE @date DATETIME, @number INT
SET @date = '2011-01-06 00:00:00'
SET @number = 12
select DATEADD(month , @number-1, @date)
gives '2011-12-06 00:00:00'
June 7, 2011 at 7:14 am
m_cg (6/6/2011)
If I understand you correctly, use the dateadd function and subtract 1 from the number of months you wish to add.DECLARE @date DATETIME, @number INT
SET @date = '2011-01-06 00:00:00'
SET @number = 12
select DATEADD(month , @number-1, @date)
gives '2011-12-06 00:00:00'
Absolutely correct. Now do it for the 12 months requested and try to avoid the loop which is nearly as important as the question itself. If you can't quite figure it out, post back and I'll show you a couple of ways to do it without the penalty of recursion.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 7, 2011 at 9:32 am
Jeff,
Still not sure of the format, but gave it 2 ways. Does this work for you?
DECLARE @date DATETIME, @number INT
SET @date = '2010-01-01 00:00:00'
--SET @number = 12
drop table monthcounter
create table monthcounter(monthid int not null)
insert into monthcounter(monthid)
values
(1),
(2),
(3),
(4),
(5),
(6),
(7),
(8),
(9),
(10),
(11),
(12)
-- if each month year combination can be in its own row
selectcast(DATEADD(month , monthid-1, @date) as varchar(3)) + ' ' + Right(cast(year(DATEADD(month , monthid-1, @date)) as varchar(4)), 2) IncludedMonYrs
frommonthcounter
order by monthid
-- If you want them as columns
SELECT 'Months to Include' AS month_order,
[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11]
FROM
(SELECT monthid-1 monthid2, cast(DATEADD(month , monthid-1, @date) as varchar(3)) + ' ' + Right(cast(year(DATEADD(month , monthid-1, @date)) as varchar(4)), 2) IncludedMonYrs
FROM monthcounter) AS SourceTable
PIVOT
(
min(IncludedMonYrs)
FOR monthid2 IN ([0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11])
) AS PivotTable;
June 7, 2011 at 10:00 am
m_cg (6/7/2011)
Still not sure of the format, but gave it 2 ways. Does this work for you?[...]
insert into monthcounter(monthid)
values
(1),
(2),
(3),
(4),
(5),
(6),
(7),
(8),
(9),
(10),
(11),
(12)
Since this syntax was introduced in SQL 2008 and this is a SQL 2005 forum, NO, this does not work.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
June 8, 2011 at 9:05 am
m_cg (6/7/2011)
Jeff,Still not sure of the format, but gave it 2 ways. Does this work for you?
What Jeff's talking about is using a Tally table.
e.g.
DECLARE @date DATETIME, @number INT
SET @date = '2011-06-06 00:00:00'
SET @number = 12
SELECT CAST(Datediff(DAY, 0, Dateadd(MONTH, n - 1, @date)) AS DATETIME)
FROM operations.tally
WHERE n <= @number
Which is far superior to the answer I suggested previously (thanks Jeff and Drew for the lesson).
June 8, 2011 at 10:47 am
jeff article, or one of them, on Tally tables is at URL: http://www.sqlservercentral.com/articles/T-SQL/62867/
I am look forward to going through it. This looks like a nice solution.
June 9, 2011 at 5:56 am
skcadavre (6/8/2011)
m_cg (6/7/2011)
Jeff,Still not sure of the format, but gave it 2 ways. Does this work for you?
What Jeff's talking about is using a Tally table.
e.g.
DECLARE @date DATETIME, @number INT
SET @date = '2011-06-06 00:00:00'
SET @number = 12
SELECT CAST(Datediff(DAY, 0, Dateadd(MONTH, n - 1, @date)) AS DATETIME)
FROM operations.tally
WHERE n <= @number
Which is far superior to the answer I suggested previously (thanks Jeff and Drew for the lesson).
Exactly. Well done! 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
August 30, 2019 at 7:58 am
hi all,
can any one help me to write query for subtracting 7 days from date,
in that column all date are same in every row , upto 53 times.
i want to subtract 7 days from first row of date upto 53 times.
eg: id date
1 2019-08-02
2 2019-08-02
upto 53 times
expected result :
id date
1 2019-08-02
2 2019-07-26
3 2019-07-19
53 2018-08-02
please help
Viewing 10 posts - 16 through 24 (of 24 total)
You must be logged in to reply to this topic. Login to reply