Author |
Topic |
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2014-02-14 : 08:23:06
|
HI Guys I need some help Aim – Calculate the number of days between the [CreatedDate] and getdate, however if stage name = ‘Live Transactions’ then Calculate the number of days between [CreatedDate] & [CloseDate]This is my query so far SELECT [CreatedDate] ,[StageName] ,[CloseDate] ,DATEDIFF(dd,CONVERT(datetime,[CreatedDate]),GETDATE()) as Age FROM [FDMS].[Dan].[Raz_Reporting_LCS]Which produces the followingCreatedDate 2012-12-17StageName Live TransactionsCloseDate 2012-12-31Age 424When in fact the age should be 14days Really looking forward to your help |
|
VeeranjaneyuluAnnapureddy
Posting Yak Master
169 Posts |
Posted - 2014-02-14 : 08:32:58
|
SELECT [CreatedDate],[StageName],[CloseDate],DATEDIFF(dd,[CreatedDate],[CloseDate]) as AgeFROM [FDMS].[Dan].[Raz_Reporting_LCS]Veera |
|
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2014-02-14 : 08:46:42
|
Hi VeeranjaneyuluAnnapureddyThank you for providing your logic, However i need to do it as a case statment, Because depending on the stagename is depenant on whether the getdate or closedate is used |
|
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2014-02-14 : 08:46:49
|
Hi VeeranjaneyuluAnnapureddyThank you for providing your logic, However i need to do it as a case statment, Because depending on the stagename is depenant on whether the getdate or closedate is used |
|
|
ahmeds08
Aged Yak Warrior
737 Posts |
Posted - 2014-02-14 : 08:54:46
|
SELECT [CreatedDate],[StageName],[CloseDate],CASE WHEN stagename = ‘Live Transactions’ THEN DATEDIFF(dd,[CreatedDate],[CloseDate]) ELSE [CreatedDate] END as AgeFROM [FDMS].[Dan].[Raz_Reporting_LCS]Javeed Ahmed |
|
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2014-02-14 : 09:15:35
|
HI ahmeds08 Createddate is presented in following format "2011-06-21T15:38:51.000Z"Datetype :varchar(50)When try running the following query SELECT [CreatedDate],[StageName],[CloseDate],CASE WHEN [StageName] = 'Live Transactions' THEN DATEDIFF(dd,[CreatedDate],[CloseDate]) ELSE [CreatedDate] end as AgeFROM [FDMS].[Dan].[Raz_Reporting_LCS]i get the following message Msg 245, Level 16, State 1, Line 1Conversion failed when converting the varchar value '2011-06-21T15:38:51.000Z' to data type int.Any ideas ? |
|
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2014-02-14 : 09:39:14
|
I believe i need to do the following CONVERT(varchar(10),[CreatedDate],102)but where do i put that into the case statement ? |
|
|
mandm
Posting Yak Master
120 Posts |
Posted - 2014-02-14 : 11:24:26
|
It's because the datediff returns an integer and the CreatedDate by itself is a date type. Don't you need this?SELECT [CreatedDate],[StageName],[CloseDate],CASE WHEN [StageName] = 'Live Transactions' THEN DATEDIFF(dd,[CreatedDate],[CloseDate]) ELSE DATEDIFF(dd,CONVERT(datetime,[CreatedDate]),GETDATE()) end as AgeFROM [FDMS].[Dan].[Raz_Reporting_LCS] |
|
|
|