Solved

dynamic field name in sql

Posted on 2009-07-02
16
436 Views
Last Modified: 2012-05-07
I have a SPROC that displays data from a table. This table has a column for each month (Jan, Feb, Mar,...)
The information within the DB is displayed on our MOSS2007 site via a data grid view. 3 months are displayed...Last Month, 2 months prior, and 3 months prior.
For example in July the SPROC and the webpage displays Apr, May, and June and change automatically every month. No work required on our end.
As far as the data goes this works fine, however the column names are displayed as whatever I name them I need them to dynamically change when the month chages.

My question is can you declare and set a variable and name the column the variable you declare? I have attached the SPROC below for you to look at.

Thanks for your help...
Chris.
AS
BEGIN
Select  Department, Description, 
(case 
when month(current_timestamp) = 1 then Dec
when month(current_timestamp) = 2 then Jan
when month(current_timestamp) = 3 then Feb
when month(current_timestamp) = 4 then Mar
when month(current_timestamp) = 5 then Apr
when month(current_timestamp) = 6 then May
when month(current_timestamp) = 7 then Jun
when month(current_timestamp) = 8 then Jul
when month(current_timestamp) = 9 then Aug
when month(current_timestamp) = 10 then Sept
when month(current_timestamp) = 11 then Oct
when month(current_timestamp) = 12 then Nov
end
) as Last_Month,
 
(case 
when month(current_timestamp) = 1 then Nov
when month(current_timestamp) = 2 then Dec
when month(current_timestamp) = 3 then Jan
when month(current_timestamp) = 4 then Feb
when month(current_timestamp) = 5 then Mar
when month(current_timestamp) = 6 then Apr
when month(current_timestamp) = 7 then May
when month(current_timestamp) = 8 then Jun
when month(current_timestamp) = 9 then Jul
when month(current_timestamp) = 10 then Aug
when month(current_timestamp) = 11 then Sept
when month(current_timestamp) = 12 then Oct
end
) as Two_Months_Prior,
 
(case 
when month(current_timestamp) = 1 then Oct
when month(current_timestamp) = 2 then Nov
when month(current_timestamp) = 3 then Dec
when month(current_timestamp) = 4 then Jan
when month(current_timestamp) = 5 then Feb
when month(current_timestamp) = 6 then Mar
when month(current_timestamp) = 7 then Apr
when month(current_timestamp) = 8 then May
when month(current_timestamp) = 9 then Jun
when month(current_timestamp) = 10 then Jul
when month(current_timestamp) = 11 then Aug
when month(current_timestamp) = 12 then Sept
end
) as Three_months_Prior, Owner from dbo.tablename
where year = '2009'
END

Open in new window

0
Comment
Question by:gsfc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
  • +1
16 Comments
 

Author Comment

by:gsfc
ID: 24764051
Additionally I have tried the below code but get the following error...
I added a Declare @MYMONTH and a set @MYMONTH statement and tried to use "AS @MYMONTH"

Msg 102, Level 15, State 1, Procedure SPUnit_WIGs10, Line 35
Incorrect syntax near '@MYMONTH'.
USE [GSFC_Wigs]
GO
/****** Object:  StoredProcedure [dbo].[SPUnit_WIGs10]    Script Date: 07/02/2009 10:41:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
 
 
 
 
 
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[SPUnit_WIGs10]
 
AS
BEGIN
 
DECLARE @MYMONTH char(4)
SET @MYMONTH = month (CURRENT_TIMESTAMP -30)
 
	Select  Department, Description, 
(case 
when month(current_timestamp) = 1 then Dec
when month(current_timestamp) = 2 then Jan
when month(current_timestamp) = 3 then Feb
when month(current_timestamp) = 4 then Mar
when month(current_timestamp) = 5 then Apr
when month(current_timestamp) = 6 then May
when month(current_timestamp) = 7 then Jun
when month(current_timestamp) = 8 then Jul
when month(current_timestamp) = 9 then Aug
when month(current_timestamp) = 10 then Sept
when month(current_timestamp) = 11 then Oct
when month(current_timestamp) = 12 then Nov
end
) as @MYMONTH,
 
(case 
when month(current_timestamp) = 1 then Nov
when month(current_timestamp) = 2 then Dec
when month(current_timestamp) = 3 then Jan
when month(current_timestamp) = 4 then Feb
when month(current_timestamp) = 5 then Mar
when month(current_timestamp) = 6 then Apr
when month(current_timestamp) = 7 then May
when month(current_timestamp) = 8 then Jun
when month(current_timestamp) = 9 then Jul
when month(current_timestamp) = 10 then Aug
when month(current_timestamp) = 11 then Sept
when month(current_timestamp) = 12 then Oct
end
) as Two_Months_Prior,
 
(case 
when month(current_timestamp) = 1 then Oct
when month(current_timestamp) = 2 then Nov
when month(current_timestamp) = 3 then Dec
when month(current_timestamp) = 4 then Jan
when month(current_timestamp) = 5 then Feb
when month(current_timestamp) = 6 then Mar
when month(current_timestamp) = 7 then Apr
when month(current_timestamp) = 8 then May
when month(current_timestamp) = 9 then Jun
when month(current_timestamp) = 10 then Jul
when month(current_timestamp) = 11 then Aug
when month(current_timestamp) = 12 then Sept
end
) as Three_months_Prior, Owner from dbo.Unit_WIGS2
where year = '2009'
END

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24764272
I would do that more like this (see code snippet).  You are getting error currently because you have the variable @MYMONTH as the name of the column which is not allowed.
USE [GSFC_Wigs]
GO
/****** Object:  StoredProcedure [dbo].[SPUnit_WIGs10]    Script Date: 07/02/2009 10:41:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
 
 
 
 
 
-- =============================================
-- Author:              <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[SPUnit_WIGs10]
 
AS
BEGIN
 
Select  Department, Description, 
(case MONTH(CURRENT_TIMESTAMP - 30)
when 1 then Dec
when 2 then Jan
when 3 then Feb
when 4 then Mar
when 5 then Apr
when 6 then May
when 7 then Jun
when 8 then Jul
when 9 then Aug
when 10 then Sept
when 11 then Oct
when 12 then Nov
end
) as One_Month_Prior,
 
(case MONTH(CURRENT_TIMESTAMP - 60)
when 1 then Nov
when 2 then Dec
when 3 then Jan
when 4 then Feb
when 5 then Mar
when 6 then Apr
when 7 then May
when 8 then Jun
when 9 then Jul
when 10 then Aug
when 11 then Sept
when 12 then Oct
end
) as Two_Months_Prior,
 
(case MONTH(CURRENT_TIMESTAMP - 90)
when 1 then Oct
when 2 then Nov
when 3 then Dec
when 4 then Jan
when 5 then Feb
when 6 then Mar
when 7 then Apr
when 8 then May
when 9 then Jun
when 10 then Jul
when 11 then Aug
when 12 then Sept
end
) as Three_months_Prior, Owner from dbo.Unit_WIGS2
where year = '2009'
END

Open in new window

0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24764291
You can use dynamic SQL to do that. Check this out:
USE [GSFC_Wigs]
GO
/****** Object:  StoredProcedure [dbo].[SPUnit_WIGs10]    Script Date: 07/02/2009 10:41:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
 
 
 
 
 
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[SPUnit_WIGs10]
 
AS
BEGIN
 
DECLARE @MYMONTH char(4)
SET @MYMONTH = Month(CURRENT_TIMESTAMP -60)
 
EXEC 
('Select  Department, Description, 
(case 
when month(current_timestamp) = 1 then Dec
when month(current_timestamp) = 2 then Jan
when month(current_timestamp) = 3 then Feb
when month(current_timestamp) = 4 then Mar
when month(current_timestamp) = 5 then Apr
when month(current_timestamp) = 6 then May
when month(current_timestamp) = 7 then Jun
when month(current_timestamp) = 8 then Jul
when month(current_timestamp) = 9 then Aug
when month(current_timestamp) = 10 then Sept
when month(current_timestamp) = 11 then Oct
when month(current_timestamp) = 12 then Nov
end
) as '+@MYMONTH+',
 
(case 
when month(current_timestamp) = 1 then Nov
when month(current_timestamp) = 2 then Dec
when month(current_timestamp) = 3 then Jan
when month(current_timestamp) = 4 then Feb
when month(current_timestamp) = 5 then Mar
when month(current_timestamp) = 6 then Apr
when month(current_timestamp) = 7 then May
when month(current_timestamp) = 8 then Jun
when month(current_timestamp) = 9 then Jul
when month(current_timestamp) = 10 then Aug
when month(current_timestamp) = 11 then Sept
when month(current_timestamp) = 12 then Oct
end
) as TwoMonthPrior,
 
(case 
when month(current_timestamp) = 1 then Oct
when month(current_timestamp) = 2 then Nov
when month(current_timestamp) = 3 then Dec
when month(current_timestamp) = 4 then Jan
when month(current_timestamp) = 5 then Feb
when month(current_timestamp) = 6 then Mar
when month(current_timestamp) = 7 then Apr
when month(current_timestamp) = 8 then May
when month(current_timestamp) = 9 then Jun
when month(current_timestamp) = 10 then Jul
when month(current_timestamp) = 11 then Aug
when month(current_timestamp) = 12 then Sept
end
) as ThreeMonthsPrior, 
Owner 
From dbo.Unit_WIGS2
where year = ''2009''
END'
)

Open in new window

0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 42

Expert Comment

by:dqmq
ID: 24764341
To change the columns dynamically, you have two choices:

1. Maintain 12 versions of the SQL and run the appropriate one.   Probably not what you are asking for.

2. Use dynamic SQL.  Following is an example of how to change the code. The example only shows 1 column, but I think you can extrapolate to the others:

DECLARE @SQL VARCHAR(4000)

SET @SQL = 'Select  Department, Description,
(case
when month(current_timestamp) = 1 then Dec
when month(current_timestamp) = 2 then Jan
when month(current_timestamp) = 3 then Feb
when month(current_timestamp) = 4 then Mar
when month(current_timestamp) = 5 then Apr
when month(current_timestamp) = 6 then May
when month(current_timestamp) = 7 then Jun
when month(current_timestamp) = 8 then Jul
when month(current_timestamp) = 9 then Aug
when month(current_timestamp) = 10 then Sept
when month(current_timestamp) = 11 then Oct
when month(current_timestamp) = 12 then Nov
end
) as  '


--folllowing is the part that generates the column name dynamically
SET @SQL= @SQL +
case
when month(current_timestamp) = 1 then 'Dec'
when month(current_timestamp) = 2 then 'Jan'
when month(current_timestamp) = 3 then 'Feb'
when month(current_timestamp) = 4 then 'Mar'
when month(current_timestamp) = 5 then 'Apr'
when month(current_timestamp) = 6 then 'May'
when month(current_timestamp) = 7 then 'Jun'
when month(current_timestamp) = 8 then 'Jul'
when month(current_timestamp) = 9 then 'Aug'
when month(current_timestamp) = 10 then 'Sept'
when month(current_timestamp) = 11 then 'Oct'
when month(current_timestamp) = 12 then 'Nov'
end
--

SET @SQL = @SQL + ', Owner from dbo.tablename
where year = ''2009'''

EXEC (@SQL)
END




0
 

Author Comment

by:gsfc
ID: 24765100
Thanks dqmq that seems to work for the single month...but I am still new to SQL and a little confused on how to display the three months and make sure that all 12 months will work as we go thru the year.

Do I need to recreate your code above for EACH month? I tried to copy your code and change the months but I still only get the one column.

Thanks again and sorry that you have to hold my hand thru this...:)
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24765203
Why not just use the method I showed without dynamic SQL?
http:#24764272
0
 

Author Comment

by:gsfc
ID: 24765235
The example code that you have does not dynamically change the field names. on your code I would stll call the fields "as One_Month_Prior" I need them to be the name of the month that is actually being displayed.

Sorry
0
 

Author Comment

by:gsfc
ID: 24765292
I tried it this way and the SPROC executes fine, but when I run it I get...

Msg 102, Level 15, State 1, Line 14
Incorrect syntax near ','.

(1 row(s) affected)
USE [GSFC_Wigs]
GO
/****** Object:  StoredProcedure [dbo].[SPUnit_WIGs10_test]    Script Date: 07/02/2009 12:37:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
 
 
 
 
 
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[SPUnit_WIGs10_test]
 
AS
BEGIN
 
DECLARE @SQL VARCHAR(4000)
 
SET @SQL = 'Select  Department, Description, 
(case 
when month(current_timestamp) = 1 then Dec
when month(current_timestamp) = 2 then Jan
when month(current_timestamp) = 3 then Feb
when month(current_timestamp) = 4 then Mar
when month(current_timestamp) = 5 then Apr
when month(current_timestamp) = 6 then May
when month(current_timestamp) = 7 then Jun
when month(current_timestamp) = 8 then Jul
when month(current_timestamp) = 9 then Aug
when month(current_timestamp) = 10 then Sept
when month(current_timestamp) = 11 then Oct
when month(current_timestamp) = 12 then Nov,
 
(case 
when month(current_timestamp) = 1 then Nov
when month(current_timestamp) = 2 then Dec
when month(current_timestamp) = 3 then Jan
when month(current_timestamp) = 4 then Feb
when month(current_timestamp) = 5 then Mar
when month(current_timestamp) = 6 then Apr
when month(current_timestamp) = 7 then May
when month(current_timestamp) = 8 then Jun
when month(current_timestamp) = 9 then Jul
when month(current_timestamp) = 10 then Aug
when month(current_timestamp) = 11 then Sept
when month(current_timestamp) = 12 then Oct,
 
(case 
when month(current_timestamp) = 1 then Oct
when month(current_timestamp) = 2 then Nov
when month(current_timestamp) = 3 then Dec
when month(current_timestamp) = 4 then Jan
when month(current_timestamp) = 5 then Feb
when month(current_timestamp) = 6 then Mar
when month(current_timestamp) = 7 then Apr
when month(current_timestamp) = 8 then May
when month(current_timestamp) = 9 then Jun
when month(current_timestamp) = 10 then Jul
when month(current_timestamp) = 11 then Aug
when month(current_timestamp) = 12 then Sept
end
) as  '
 
 
--folllowing is the part that generates the column name dynamically
 
 
SET @SQL= @SQL + 
case 
when month(current_timestamp) = 1 then 'Dec'
when month(current_timestamp) = 2 then 'Jan'
when month(current_timestamp) = 3 then 'Feb'
when month(current_timestamp) = 4 then 'Mar'
when month(current_timestamp) = 5 then 'Apr'
when month(current_timestamp) = 6 then 'May'
when month(current_timestamp) = 7 then 'Jun'
when month(current_timestamp) = 8 then 'Jul'
when month(current_timestamp) = 9 then 'Aug'
when month(current_timestamp) = 10 then 'Sept'
when month(current_timestamp) = 11 then 'Oct'
when month(current_timestamp) = 12 then 'Nov'
end
 
SET @SQL= @SQL + 
case 
when month(current_timestamp) = 1 then 'Nov'
when month(current_timestamp) = 2 then 'Dec'
when month(current_timestamp) = 3 then 'Jan'
when month(current_timestamp) = 4 then 'Feb'
when month(current_timestamp) = 5 then 'Mar'
when month(current_timestamp) = 6 then 'Apr'
when month(current_timestamp) = 7 then 'May'
when month(current_timestamp) = 8 then 'Jun'
when month(current_timestamp) = 9 then 'Jul'
when month(current_timestamp) = 10 then 'Aug'
when month(current_timestamp) = 11 then 'Sept'
when month(current_timestamp) = 12 then 'Oct'
end
 
SET @SQL= @SQL + 
case 
when month(current_timestamp) = 1 then 'Oct'
when month(current_timestamp) = 2 then 'Nov'
when month(current_timestamp) = 3 then 'Dec'
when month(current_timestamp) = 4 then 'Jan'
when month(current_timestamp) = 5 then 'Feb'
when month(current_timestamp) = 6 then 'Mar'
when month(current_timestamp) = 7 then 'Apr'
when month(current_timestamp) = 8 then 'May'
when month(current_timestamp) = 9 then 'Jun'
when month(current_timestamp) = 10 then 'Jul'
when month(current_timestamp) = 11 then 'Aug'
when month(current_timestamp) = 12 then 'Sept'
end
--
 
SET @SQL = @SQL + ', Owner from dbo.Unit_WIGs2
where year = ''2009'''
 
EXEC (@SQL)
END

Open in new window

0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24765297
In Dynamic SQL you can do something like this. This will change the column names dynamically for all columns
ALTER PROCEDURE [dbo].[SPUnit_WIGs10]
 
AS
BEGIN
 
DECLARE @MYMONTH char(4)
SET @MYMONTH = Month(CURRENT_TIMESTAMP -60)
 
EXEC 
('Select  Department, Description, 
(case 
when month(current_timestamp) = 1 then Dec
when month(current_timestamp) = 2 then Jan
when month(current_timestamp) = 3 then Feb
when month(current_timestamp) = 4 then Mar
when month(current_timestamp) = 5 then Apr
when month(current_timestamp) = 6 then May
when month(current_timestamp) = 7 then Jun
when month(current_timestamp) = 8 then Jul
when month(current_timestamp) = 9 then Aug
when month(current_timestamp) = 10 then Sept
when month(current_timestamp) = 11 then Oct
when month(current_timestamp) = 12 then Nov
end
) as '+DateName(Month,CURRENT_TIMESTAMP-30)+', 
(case 
when month(current_timestamp) = 1 then Nov
when month(current_timestamp) = 2 then Dec
when month(current_timestamp) = 3 then Jan
when month(current_timestamp) = 4 then Feb
when month(current_timestamp) = 5 then Mar
when month(current_timestamp) = 6 then Apr
when month(current_timestamp) = 7 then May
when month(current_timestamp) = 8 then Jun
when month(current_timestamp) = 9 then Jul
when month(current_timestamp) = 10 then Aug
when month(current_timestamp) = 11 then Sept
when month(current_timestamp) = 12 then Oct
end
) as '+DateName(Month,CURRENT_TIMESTAMP-60)+',
 
(case 
when month(current_timestamp) = 1 then Oct
when month(current_timestamp) = 2 then Nov
when month(current_timestamp) = 3 then Dec
when month(current_timestamp) = 4 then Jan
when month(current_timestamp) = 5 then Feb
when month(current_timestamp) = 6 then Mar
when month(current_timestamp) = 7 then Apr
when month(current_timestamp) = 8 then May
when month(current_timestamp) = 9 then Jun
when month(current_timestamp) = 10 then Jul
when month(current_timestamp) = 11 then Aug
when month(current_timestamp) = 12 then Sept
end
) as '+DateName(Month,CURRENT_TIMESTAMP-90)+', 
Owner 
From dbo.Unit_WIGS2
where year = ''2009''
END'
)

Open in new window

0
 

Author Comment

by:gsfc
ID: 24765333
pssandhu,

I get Msg 102, Level 15, State 1, Procedure SPUnit_WIGs10_test2, Line 37
Incorrect syntax near 'DateName'.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24765352
Sorry I must have glossed over that point in the question.  I see it now.  Try it like this:
USE [GSFC_Wigs]
GO
/****** Object:  StoredProcedure [dbo].[SPUnit_WIGs10]    Script Date: 07/02/2009 10:41:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[SPUnit_WIGs10]
 
AS
BEGIN
 
DECLARE @sql NVARCHAR(MAX)
SET @sql = 'Select  Department, Description' 
	+ ', ' + LEFT(DATENAME(m, CURRENT_TIMESTAMP-30), 3)
	+ ', ' + LEFT(DATENAME(m, CURRENT_TIMESTAMP-60), 3)
	+ ', ' + LEFT(DATENAME(m, CURRENT_TIMESTAMP-90), 3)
	+ ', Owner From dbo.Unit_WIGS2 where year = 2009'
EXEC(@sql)

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24765383
This will result in the following SQL being executed:
Select  Department, Description, Jun, May, Apr, Owner From dbo.Unit_WIGS2 where year = 2009

Next month, this will adjust automatically.  If your column names are not all 3 characters, as it looks like september is sept, then you can do this:


USE [GSFC_Wigs]
GO
/****** Object:  StoredProcedure [dbo].[SPUnit_WIGs10]    Script Date: 07/02/2009 10:41:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[SPUnit_WIGs10]
 
AS
BEGIN
 
DECLARE @sql NVARCHAR(MAX)
SET @sql = 'Select  Department, Description' 
	+ ', ' + LEFT(DATENAME(m, CURRENT_TIMESTAMP-30), CASE MONTH(CURRENT_TIMESTAMP-30) WHEN 9 THEN 4 ELSE 3 END)
	+ ', ' + LEFT(DATENAME(m, CURRENT_TIMESTAMP-60), CASE MONTH(CURRENT_TIMESTAMP-60) WHEN 9 THEN 4 ELSE 3 END)
	+ ', ' + LEFT(DATENAME(m, CURRENT_TIMESTAMP-90), CASE MONTH(CURRENT_TIMESTAMP-90) WHEN 9 THEN 4 ELSE 3 END)
	+ ', Owner From dbo.Unit_WIGS2 where year = 2009'
EXEC(@sql)

Open in new window

0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24765392
Oh, thats right! I wasn't even looking at the sql. You do not need the case staement since we are using DATENAME formula to get the Month name.
mwvisa1's solution should be what you are looking for.
P.
0
 
LVL 42

Expert Comment

by:dqmq
ID: 24766225
We all agree: use dynamic SQL.  We all agree to generate the month name column headers from the current_timestamp.  

The difference is whether to use a case statement to generate the month name or to use the datename function (note: you could also use the convert function).   The case statement is more cumbersome, but it gives you complete control of the month names.  For example, in your question you have "Aug" for August and "Sept" for September.   The case statement can be used to reproduce those names, whereas the datename function is not quite so flexible, always returning 3 characters in the solutions provided.  

While I proposed the case, I would go with the less cumbersome approach if the resulting month names are acceptable.  

>Do I need to recreate your code above for EACH month?
For the CASE approach, you need to add the other two columns into the SQL you are constructing.  It's basically, a copy/paste of the prior month column and then subtracting more days from the other month columns.  

One more thing, VERY IMPORTANT: note that in all the solutions you are subtracting 30-60-90 DAYS from current_timestamp to derive the month. That is not a good general solution--for example, will not work well when 28, 29, or 31 day months are involved.  You really should subtract months.  Instead of:

Current_Timestamp - 30
Current_Timestamp - 60
Current_Timestamp - 90

use this:

DateAdd (M,-1,Current_Timestamp)
DateAdd (M,-2,Current_Timestamp)
DateAdd (M,-3,Current_Timestamp)








 
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 24766346
I agree with dgmg.  You can think of March as a good argument against subtracting 30 to get previous month as you could end up in January since February only has 28-9 days.  Use the DateAdd as shown.

Note you can pull the 2009 from the current timestamp as well.

USE [GSFC_Wigs]
GO
/****** Object:  StoredProcedure [dbo].[SPUnit_WIGs10]    Script Date: 07/02/2009 10:41:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[SPUnit_WIGs10]
 
AS
BEGIN
 
DECLARE @sql NVARCHAR(MAX)
DECLARE @m1 DATETIME, @m2 DATETIME, @m3 DATETIME
 
SELECT @m1 = DATEADD(m, -1, CURRENT_TIMESTAMP)
, @m2 = DATEADD(m, -2, CURRENT_TIMESTAMP)
, @m3 = DATEADD(m, -3, CURRENT_TIMESTAMP)
 
SET @sql = 'Select  Department, Description' 
	+ ', ' + LEFT(DATENAME(m, @m1), CASE MONTH(@m1) WHEN 9 THEN 4 ELSE 3 END)
	+ ', ' + LEFT(DATENAME(m, @m2), CASE MONTH(@m2) WHEN 9 THEN 4 ELSE 3 END)
	+ ', ' + LEFT(DATENAME(m, @m3), CASE MONTH(@m3) WHEN 9 THEN 4 ELSE 3 END)
	+ ', Owner From dbo.Unit_WIGS2 where year = ' + CAST(YEAR(CURRENT_TIMESTAMP) AS NVARCHAR(4))
EXEC(@sql)
END

Open in new window

0
 

Author Closing Comment

by:gsfc
ID: 31599212
Thanks for EVERYONE's help....mwvisa1:worked great!
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question