Month and year Dropdown

I have a report in SRSS where a user selects a @startdate and @enddate and so produces their required output. The dates selected are always a full calendar month so October would be  1/10/2013 to 31/10/2013. There seems little point in doing this so a drop down of months and another for year would be preferential.  I know I can create parameters and provide values say Jan Feb, Mar and 2012,2013, etc. but how do I format the query

Currently

SELECT
  FA_ACCOUNTS.FA_CODE
  ,FA_ACCOUNTS.FA_NAME
  ,FA_ACCOUNTS.FA_BOUGHT_VAL
  ,FA_ACCOUNTS.FA_BOUGHTDATE
FROM
  FA_ACCOUNTS
WHERE
FA_ACCOUNTS.FA_BOUGHTDATE = ?????

Bought date is date\time

Please help
S
SimonJohnGAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Here's how I pulled this off on a current project.  Modify the UPDATE statement to suit your needs

The SP that feeds the combo box
ALTER PROC rpt_cbo_Month AS

/*
List of current month and 12 previous for 348-Patronage Migration reports.
09-30-13  jhorn  Original
*/


Declare @dt datetime = GETDATE()

-- Grab the first of the current month
Declare @StartDt datetime, @i int
SELECT @StartDt = CAST(CAST(YEAR(@dt) as CHAR(4)) + '-' + RIGHT('0' + CAST(MONTH(@dt) as varchar(2)),2) + '-01' as datetime)
 
IF OBJECT_ID('tempdb..#month') IS NOT NULL
	DROP TABLE #month
	
CREATE TABLE #month (label varchar(7), value datetime) 

SET @i = 0

-- Populate a temp table with the current month plus the 12 previous
WHILE @i <= 12
	begin
	INSERT INTO #month (value)
	VALUES (DATEADD(M, -1 * @i, @StartDt))

	SET @i = @i + 1

	END

-- Cosmetic mm/yyyy to show the user. 
UPDATE #month
SET label = RIGHT('0' + CAST(MONTH(value) as varchar(2)),2) + '/' + CAST(YEAR(value) as CHAR(4))

-- Return set
SELECT * FROM #month ORDER BY value DESC

Open in new window


Then in your SP...
ALTER PROC rpt_YourProc (@MonthNbr datetime) AS
/*

Code comments go here

-- TESTING ONLY
Declare @MonthNbr datetime = GETDATE()


*/

-- Get the month boundaries
Declare @StartDt datetime, @EndDt datetime
SELECT @StartDt = CAST(CAST(YEAR(@MonthNbr) as CHAR(4)) + '-' + RIGHT('0' + CAST(MONTH(@MonthNbr) as varchar(2)),2) + '-01' as datetime)
SELECT @EndDt = DATEADD(M, 1, @StartDt) 

-- The rest of your proc goes here

Open in new window


Hope this helps.
Jim
0
PortletPaulfreelancerCommented:
will both parameters be integers or will the Month actually be passed as 'Jan' 'Feb' etc?

--
while here:
do you happen to use SQL 2012? datefromparts() can make this a bit easier

DATEFROMPARTS ( year, month, day )
---
0
Brian CroweDatabase AdministratorCommented:
Passing the year and month as integers makes the query filtering trivial:

SELECT
  FA_ACCOUNTS.FA_CODE
  ,FA_ACCOUNTS.FA_NAME
  ,FA_ACCOUNTS.FA_BOUGHT_VAL
  ,FA_ACCOUNTS.FA_BOUGHTDATE
FROM
  FA_ACCOUNTS
WHERE
   YEAR(FA_ACCOUNTS.FA_BOUGHTDATE) = @Year
   AND MONTH(FA_ACCOUNTS.FA_BOUGHTDATE) = @Month
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

PortletPaulfreelancerCommented:
Here is a method assuming a varchar(3) month and integer year as parameters.
Note I do NOT use between deliberately:
**Query 1**:

    DECLARE @yr AS int = 2013
    DECLARE @mn AS varchar(3) = 'Jan'
    
    
    DECLARE @startdate AS datetime
    DECLARE @enddate   AS datetime
    
    SET @startdate = convert(datetime, (@mn + ' 1, ' + cast(@yr AS varchar)) ,107)
    SET @enddate   = dateadd(MONTH,1,@startdate)
    
    SELECT
            FA_CODE
          , FA_NAME
          , FA_BOUGHT_VAL
          , convert(varchar,FA_BOUGHTDATE, 121)
    FROM fa_accounts
    WHERE (
            FA_BOUGHTDATE >= @startdate
        AND FA_BOUGHTDATE <  @enddate
          )
    -- and ...
    	

Open in new window

    CREATE TABLE FA_ACCOUNTS
    	([FA_CODE] varchar(2), [FA_NAME] varchar(5), [FA_BOUGHT_VAL] int, [FA_BOUGHTDATE] datetime)
    ;
    	
    INSERT INTO FA_ACCOUNTS
    	([FA_CODE], [FA_NAME], [FA_BOUGHT_VAL], [FA_BOUGHTDATE])
    VALUES
    	('AA', 'Alan', 12, '2012-12-31 23:59:59'), --<< 23:59:59
    	('AA', 'Brian', 13, '2013-01-01 00:00:00'),
    	('AA', 'Alan', 14, '2013-01-04 00:00:00'),
    	('AA', 'Dave', 15, '2013-01-06 00:00:00'),
    	('AA', 'Fred', 16, '2013-01-08 00:00:00'),
    	('AA', 'Jim', 17, '2013-01-10 00:00:00'),
    	('AA', 'Alan', 18, '2013-01-12 00:00:00'),
    	('AA', 'Brian', 19, '2013-01-14 00:00:00'),
    	('AA', 'Phil', 20, '2013-01-16 00:00:00'),
    	('AA', 'Alan', 21, '2013-01-18 00:00:00'),
    	('AA', 'Dave', 22, '2013-01-20 00:00:00'),
    	('AA', 'Fred', 23, '2013-01-22 00:00:00'),
    	('AA', 'Jim', 24, '2013-01-24 00:00:00'),
    	('AA', 'Alan', 25, '2013-01-26 00:00:00'),
    	('AA', 'Brian', 26, '2013-01-28 00:00:00'),
    	('AA', 'Phil', 27, '2013-01-31 23:59:59'), --<< 23:59:59
    	('AA', 'Alan', 28, '2013-02-01 00:00:00'),
    	('AA', 'Dave', 29, '2013-02-03 00:00:00'),
    	('AA', 'John', 30, '2013-02-05 00:00:00'),
    	('AA', 'Fred', 31, '2013-02-07 00:00:00'),
    	('AA', 'Jim', 32, '2013-02-09 00:00:00')
    ;

    	
    

**[Results][2]**:
    
    | FA_CODE | FA_NAME | FA_BOUGHT_VAL |                COLUMN_3 |
    |---------|---------|---------------|-------------------------|
    |      AA |   Brian |            13 | 2013-01-01 00:00:00.000 |
    |      AA |    Alan |            14 | 2013-01-04 00:00:00.000 |
    |      AA |    Dave |            15 | 2013-01-06 00:00:00.000 |
    |      AA |    Fred |            16 | 2013-01-08 00:00:00.000 |
    |      AA |     Jim |            17 | 2013-01-10 00:00:00.000 |
    |      AA |    Alan |            18 | 2013-01-12 00:00:00.000 |
    |      AA |   Brian |            19 | 2013-01-14 00:00:00.000 |
    |      AA |    Phil |            20 | 2013-01-16 00:00:00.000 |
    |      AA |    Alan |            21 | 2013-01-18 00:00:00.000 |
    |      AA |    Dave |            22 | 2013-01-20 00:00:00.000 |
    |      AA |    Fred |            23 | 2013-01-22 00:00:00.000 |
    |      AA |     Jim |            24 | 2013-01-24 00:00:00.000 |
    |      AA |    Alan |            25 | 2013-01-26 00:00:00.000 |
    |      AA |   Brian |            26 | 2013-01-28 00:00:00.000 |
    |      AA |    Phil |            27 | 2013-01-31 23:59:59.000 |


  [1]: http://sqlfiddle.com/#!3/444c6/2

Open in new window

For reasons why I recommend avoiding between please see: "Beware of Between"
also: DATE and TIME ... don't be scared, and do it right (the first time) (E-E, by angelIII)

{+edit}
also note using a date range this way avoids any need for using functions on the data for efficient use of indexes (aka "sargable")
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Some good looking comments from three experts.

SimonJohnG  - How's it goin', eh?
0
SimonJohnGAuthor Commented:
Thanks to all for your comments, all would work great but went with BriCroew as suited me the best. Thank you guys again for all your support
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.