get value from parameter

  • Hi,

    I am passing parameter in my store procedure.

    declare @year datetime= '2019'

    select * from table where sportyear=@year

    How can I extract where if i pass in '2019', my where condition sportyear will get last year which is 2018.

  • 2019 - 1 = 2018, so what wrong with WHERE sportyear = @year - 1; here?

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • girl_bj wrote:

    Hi, I am passing parameter in my store procedure. declare @year datetime= '2019' select * from table where sportyear=@year How can I extract where if i pass in '2019', my where condition sportyear will get last year which is 2018.

     

    What is the datatype of sportyear in the table?

    If it is datetime, then your select will only bring back results where sportyear = '2019-01-01 00:00:00.000'.  Any other values in the year will be filtered out

  • Thom A wrote:

    2019 - 1 = 2018, so what wrong with WHERE sportyear = @year - 1; here?

    With @year being defined as an Integer rather than a datetime.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • select * from table where sportyear=DATEADD(YEAR, -1, @year)

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply