Click here to Skip to main content
15,917,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

I have a table having column like below

name
month-> It says 3 for match
year-> It saves year


Now I need a query to get records between April 2010 to March 2011. If we pass 2010 as input..

Can you please provide stored procedure or query?
Posted

Try this
CREATE PROCEDURE myProc
    @monthStart INT,
    @yearStart  INT,
    @monthEnd   INT,
    @yearEnd    INT
AS
BEGIN
-- check params
IF  (@yearStart > @yearEnd) OR (@yearStart = @yearEnd AND @monthStart > @monthEnd)
    BEGIN
    RAISERROR('Invalid Parameters',16,1)
    RETURN 1
    END
SELECT  *
FROM    MyTable
WHERE   [year] BETWEEN @yearStart AND @yearEnd
        AND
        CASE
            WHEN [year] = @yearStart AND [month] < @monthStart  THEN 0
            WHEN [year] = @yearEnd AND [month] > @monthEnd      THEN 0
            ELSE 1
        END = 1
RETURN 0
END
GO
 
Share this answer
 
See here[^]. It may help you.
 
Share this answer
 
Check the SQL UNION
SELECT [column name]
FROM [table name]
WHERE month > 3 
AND year = 2010 
UNION
SELECT [column name]
FROM [table name]
WHERE month < 4
AND year = 2011
 
Share this answer
 
select name, month, year from [tablenane]
where (month = 4) and (year = 2010)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900