0

I want to make a new variable of type Date by passing the Month and Year

I don't know the syntax, but maybe something like:

DECLARE @Date Date
SET @Month(Date) = @Month 
SET @Year(Date) = @Year 

The Month and Year are some parameters of my application.

James Z
  • 12,209
  • 10
  • 24
  • 44
cristi.gherghina
  • 311
  • 1
  • 6
  • 18
  • possible duplicate of [How to return the date part only from a SQL Server datetime datatype](http://stackoverflow.com/questions/113045/how-to-return-the-date-part-only-from-a-sql-server-datetime-datatype) – StarShine Sep 10 '15 at 14:49

2 Answers2

2

Do you mean DATEFROMPARTS ?

DECLARE @Date Date = DATEFROMPARTS ( @Year, @Month, @Day )

EDIT: This function only available after SQL Server 2012. For SQL Server 2008. You may use the following

SET @Date = CAST(CONVERT(VARCHAR, @year) + '-' + CONVERT(VARCHAR, @month) + '-' + CONVERT(VARCHAR, @day) AS DATE)
EricZ
  • 6,065
  • 1
  • 30
  • 30
0

you can do a number of things to accomplish this task... an easy way is to just declare the variables, then in the select statement where clause you can add them. so:

declare @year as int (or whatever data type you need to use)
as
select * from yourtable 
where year = @year and month = @month
jswan
  • 90
  • 7