Question

I would lake to specify a specific datetime variable to be used. The code would look something like this:

Declare @Year = int
Declare @JanDate = Datetime

Set @JanDate = ???

I would like to set @JanDate to Month=1, Day =25, Year = @Year. Not sure what to put in for that?

Was it helpful?

Solution 4

Declare @Year int;
SET @Year = 2013;
Declare @JanDate  Datetime;

SET @JanDate = CAST(
                CAST(@Year AS NVARCHAR(4)) + CAST('01' AS NVARCHAR(2)) +  CAST('25' AS NVARCHAR(2))
                AS DATETIME)
SELECT @JanDate


RESULT: 2013-01-25 00:00:00.000

Or simply

Declare @Year int;
SET @Year = 2013;
Declare @JanDate  Datetime;

SET @JanDate = CAST(
                CAST(@Year AS NVARCHAR(4)) + '01' +  '25'
                AS DATETIME)
SELECT @JanDate 

RESULT: 2013-01-25 00:00:00.000

OTHER TIPS

For those who are using SQL Server 2012, use one of the DATEFROMPARTS, DATETIMEFROMPARTS, DATETIME2FROMPARTS, OR SMALLDATETIMEFROMPARTS functions. You are on SQL 2005, so these functions are not available, but I am including them anyway in case someone on 2012 or higher finds this question in the future.

For example:

declare @Year int = 2014
declare @JanDate datetime

select @JanDate = DATETIMEFROMPARTS(@Year, 1, 25, 0, 0, 0, 0)

For the poster and those of us still using SQL Server 2005, you can concatenate a string together and convert that to a date:

declare @Year int = 2014
declare @JanDate datetime

select @JanDate = CAST(CAST(@Year as varchar) + '-1-25' as varchar)

Or, see the answer to this question for a possibly more efficient way using multiple DATEADD functions.

I don't like casting strings to dates. Neither should you.

If you using SQL Server 2012 or later then you can use the answersuggested by Paul Williams i.e. DateFromParts or similar functions.

For earlier versions here's the method I would use:

DECLARE @Year    int;
DECLARE @JanDate datetime;

SET @Year = 2014;

SET @JanDate = DateAdd(dd, 25 - 1, DateAdd(yy, @Year - 1900, '1900-01-01'));

SELECT @JanDate;

It might seem overly complicated to some, but the way in which it performs the action is much more pleasing [to me] as it uses nothing but dates and integers.

Starting at SQL Servers base date 1900-01-01 we add on the number of years supplied (correcting by 1900 years ;-))

Then we add on the number of days (again, a correction of 1 day is required as the date we're working with is already the 1st of the month so adding 25 = 26, not the 25 we want).


Here's some supplementary code that you might find useful. Supply it any year, month and day it it will work it out for you.

If a user supplies "invalid" values (e.g. 2014-02-31 - there is no 31st of February) then the function will not fail, unlike a text to date casting. Instead it will provide the result 2014-03-03 which is 31 days after 1st Feb.

This logic might not please some people but it has always worked for me.

Enough waffling, here's the code:

DECLARE @y int
      , @m int
      , @d int;

SET @y = 2014;
SET @m = 6;
SET @d = 22;

SELECT DateAdd(dd, @d - 1, DateAdd(mm, @m - 1, DateAdd(yy, @y - 1900, '1900-01-01')));

-End transmission-

Your DECLARE statements may be incorrect depending on your version of SQL. The following should work (you'll just need to change @Year for the year you want):

DECLARE @Year int 
DECLARE @JanDate Datetime 
SET @Year=2013
SET @JanDate='1/25/'+CAST(@Year AS VARCHAR)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top