pgunston
pgunston

Reputation: 302

Is it possible to use a SQL date with a dynamic year?

Is it possible to set a variable where the year is a variable?

For example,

Instead of

SET @Date1 = '2014-01-25 00:00:00.000';  

I want

SET @Year = 2014 int;
SET @Date1 = '@Year-01-25 00:00:00.000';

Upvotes: 0

Views: 109

Answers (2)

Alireza
Alireza

Reputation: 5056

Try

SET @Date1 = CONVERT(datetime, CONVERT(varchar(4), @Year) + '-01-25', 102)

That number 102 as the 3rd parameter is the style used to convert. You can see a complete list of styles here: http://msdn.microsoft.com/en-us/library/ms187928.aspx scroll down to Date and Time styles

Upvotes: 1

Old fart
Old fart

Reputation: 600

Yes

Many different ways including DATEFROMPARTS ( year, month, day )

@Date1 = DATEFROMPARTS (2014,01,25)

Upvotes: 1

Related Questions