SQL Server - YEAR(GETDATE())

Asked By Lokesh M on 24-May-09 07:34 AM
EXEC [SP_name] 'lokesh',YEAR(GETDATE()) - not working

EXEC [SP_name] 'lokesh','2009' - working

May i know the reason?

Thanks

re

Ravenet Rasaiyah replied to Lokesh M on 24-May-09 07:38 AM
Hi

Try this

DATEPART(year,GetDate());

thank you


re

Ravenet Rasaiyah replied to Lokesh M on 24-May-09 07:39 AM
Hi

Small change in last post

SELECT DATEPART(yyyy,GetDate());

thank you

re

Ravenet Rasaiyah replied to Lokesh M on 24-May-09 07:53 AM
hi

i think your sp required char value for year also, so you need do like this way

EXEC [SP_name] 'lokesh',''''+YEAR(GETDATE())+'''';

thank you
Santhosh N replied to Lokesh M on 24-May-09 08:27 AM
The problem could be with the Datatype of the input paramter to the SP
If the second parameter to the SP is any varchar, then you need to modify the SP calling statement to

EXEC [SP_name] 'lokesh',"'" + YEAR(GETDATE()) + "'"

Anyway, the otherway of getting year part is as follows...

DATEPART(YEAR, GETDATE())

ie, your query would be as

EXEC [SP_name] 'lokesh',"'" + DATEPART(YEAR, GETDATE()) + "'"

Ilias replied to Lokesh M on 30-Nov-10 07:38 AM
GetDate() returns a number. Your procedure seems to require a text string ('2009'). Try executing the command without the apostrophes (''), e.g. writing 2009 instead of '2009'.