Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Report Design
Message Icon Topic: string convert to date problem Post Reply Post New Topic
Author Message
johnwsun
Senior Member
Senior Member


Joined: 28 May 2008
Location: Australia
Online Status: Offline
Posts: 179
Quote johnwsun Replybullet Topic: string convert to date problem
     Posted: 09 May 2009 at 11:00pm
Hi,
 
I have a formula where I am going to use by grouping on years between current year and the publication year and calculate the difference:
 
// grouping by publication years
if datediff("yyyy", cdate{DTE.DATE1}, currentdate) in [1, 2, 3, 4, 5]
    then "1 - 5 Years"
else
if datediff("yyyy",  cdate({DTE.DATE1}), currentdate) in [6, 7, 8, 9, 10]
    then "6 - 10 Years"
else
     "11 Years and over"
DTE.DATE1 is a nvarchar type string, stored as 1995, 1996, etc.
I was trying to conver DTE.DATE1 to date type, there was an error - bad string, or just using {DTE.DATE1} will not work eigher obviously as datediff needs two date type arguments.
Could somebody advise how to make it work when there is a string type field as an argument for datediff? Thanks in advance.
 
John
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 10 May 2009 at 8:14am
if datepart("yyyy",currentdate)-tonumber(dte.date1) in 1 to 5 then "1-5 years"
//note this omits anything done the current year - you may want to change it to 0 to 5
else
if datepart("yyyy",currentdate)-tonumber(dte.date1) in 6 to 10 then "6-10 years" else ...
IP IP Logged
johnwsun
Senior Member
Senior Member


Joined: 28 May 2008
Location: Australia
Online Status: Offline
Posts: 179
Quote johnwsun Replybullet Posted: 24 May 2009 at 4:41am

Hi DBlank,

Datepart is better than datediff in this query!

I used this in the crosstab where I found some incorrect percentage as against total, so I create a normal report with two groups: one group use this formula as advised.

Thans DBlank,
 
John
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum