tags:

views:

41

answers:

5

Trying to construct a date:

CAST('9/1/' + YEAR(GETDATE()) AS Datetime) AS test2

But it doesnt work?

Would like to get something like '9/1/2010'?

+4  A: 

you can't concatenate the string '9/1' with the number: YEAR(GETDATE()), so try this:

select CAST('9/1/' + CONVERT(varchar(4),YEAR(GETDATE())) AS Datetime) AS test2

KM
ooooh, makes sense...
A: 

You can use a string formatted YYYYMMDD.

Kees de Kooter
+1  A: 
SELECT 
 CAST( '9/1/' + CAST( YEAR(GETDATE()) AS VARCHAR ) AS Datetime) AS test2 

You need to cast the YEAR (integer) to a VARCHAR before you can append it.

Nissan Fan
A: 

Where is the date coming from?

There are easier ways to build dates using DATEADD

gbn
A: 

try this:

Select DateAdd(month, 
           dateDiff(month, 0, getdate()) + 9 - MONTH(getdate()),
           0)
Charles Bretana