Page 1 of 1

Working with datetime variables

Posted: Thu Dec 08, 2011 8:31 am
by tynar
Hello,

In datasets I would like to assign a date start of year to variable.

I wrote following SQL:

DECLARE @startOfYear datetime

select @startOfYear = cast(cast(YEAR(@EndDatePar) AS VARCHAR(4)) + '0101' AS datetime) --DATEADD(YEAR ,DATEDIFF(YEAR ,0 ,@EndDatePar) ,0)

but I receive error:
"Conversion failed when converting date and/or time from character string." on clicking Receive Columns.

@EndDatePar is a parameter associated with a variable which user enters and I'm working in Designer.

Would much appreciate your help.

Working with datetime variables

Posted: Fri Dec 09, 2011 1:48 am
by HighAley
Hello.
tynar wrote:In datasets I would like to assign a date start of year to variable.

I wrote following SQL:

DECLARE @startOfYear datetime

select @startOfYear = cast(cast(YEAR(@EndDatePar) AS VARCHAR(4)) + '0101' AS datetime) --DATEADD(YEAR ,DATEDIFF(YEAR ,0 ,@EndDatePar) ,0)

but I receive error:
"Conversion failed when converting date and/or time from character string." on clicking Receive Columns.

@EndDatePar is a parameter associated with a variable which user enters and I'm working in Designer.

Would much appreciate your help.
Please, try to use next expression:

Code: Select all

select @startOfYear = cast((cast(YEAR(@EndDatePar) AS VARCHAR(4)) + '0101') AS datetime) --DATEADD(YEAR ,DATEDIFF(YEAR ,0 ,@EndDatePar) ,0)
Otherwise, please send us your report template with data for analysis.

Thank you.

Working with datetime variables

Posted: Fri Dec 09, 2011 5:38 am
by tynar
I tried your suggestion without success.

Please find in attachments example report file.

Working with datetime variables

Posted: Fri Dec 09, 2011 7:10 am
by HighAley
Hello.

Please, try to use next query:

Code: Select all

declare @startOfYear datetime

select cast(cast(YEAR(@nowDatePar) AS VARCHAR(4)) + '0101' AS datetime) col1
Thank you.