Data Connectivity
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Data Connectivity
Message Icon Topic: Date Parameter to String in SQL Post Reply Post New Topic
Author Message
kali
Newbie
Newbie


Joined: 05 May 2010
Online Status: Offline
Posts: 6
Quote kali Replybullet Topic: Date Parameter to String in SQL
    Posted: 05 May 2010 at 11:56am
Hi - I am new to the whole CR 2008 design and dev and have done OK so far.....
 
One thing that has got me down is as follows
 
I need to extract out data from a database table where dates are stored as 17 digit characters like:
 
20100428171456013 - the key chars are the first 8 which denote YYYY MM DD
 
Basically I want to run the following SQL when the report is run:
 
SELECT A.A_ACCID,
  CASE E.A_TRDSDE
   WHEN 'B' THEN 'BUY'
   WHEN 'S' THEN 'SELL'
  END AS TRDSDE,
  C.A_CTRPTYID,
  I.A_SEDCDE,
  SUBSTRING(E.A_ADDDTETME,0, 9) AS ADDDTE,
  SUBSTRING(E.A_STTDTE,0, 9) AS STTDTE,
  SUBSTRING(E.A_ADDDTETME,9, 4) AS ADDTME,
  E.A_QTY,
  E.A_STTCCYCDE,
  E.A_PRC, 
  E.A_GRSCSD,
  E.A_CMSAMT,
  ((E.A_GRSCSD - E.A_CMSAMT) / E.A_QTY) AS NETPRC,
  E.A_INSNAM,
  ISNULL(E.A_GIVUP, 'N') AS IS_CFD, 
  'ML' AS GIVEUP_BROKER,
  ISNULL(I.A_RICCDE, 'N/A'),
  ISNULL(E.A_LCLFEE,0) AS LOCAL_FEE,
  ISNULL(E.A_LCLTAX,0) AS LOCAL_TAX
FROM T_ATH_EXE E
JOIN T_ATH_ACC A ON E.A_ACCID = A.A_ACCID
JOIN T_ATH_CTRPTY C ON C.A_CTRPTYID = E.A_CTRPTYID
JOIN T_ATH_INS I ON I.A_INSID = E.A_INSID
WHERE E.A_ADDDTETME BETWEEN '{?startDate}' AND '{?endDate}'
AND A.A_ACCID = '{?Portfolio}'
 
{?Portfolio} is a dynamic parameter which works fine as a dropdown liist of values
 
{?startDate}' and '{?endDate}' are date parameters which take the form 28/5/2010 etc
 
It is this value  that needs to be converted to the database format I require above when it runs
 
Conversely, if I specify {?startDate}' and '{?endDate}' as a string parameter and paste in values such as 20100428171456013 during run time the report runs fine
 
What is the best way of solving this
 

 
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 05 May 2010 at 12:05pm

Based on your SQL above, I assume you're using a command instead of tables, correct?

If so, you can change your SQL to convert the applicable part of E.A_ADDDTETME to a date.  In SQL Server, this would look something like this:
 
cast((substring(E.A_ADDDTETME, 5, 2) + '/' + substring(E.A_ADDDTETME, 7, 2) + '/' + left(E.A_ADDDTETME, 4)) as datetime)
 
If you're using tables instead of a command, you can create a SQL Expression that will do this same thing and then use that in your selection criteria.
 
-Dell
IP IP Logged
kali
Newbie
Newbie


Joined: 05 May 2010
Online Status: Offline
Posts: 6
Quote kali Replybullet Posted: 05 May 2010 at 12:13pm
Tongue
 
 
Yes I am using commands and not tables (is that a bad thing?)
 
How do I wrap the below SQL around my date parameters in the existing SQL WHERE CLAUSE

WHERE E.A_ADDDTETME BETWEEN '{?startDate}' AND '{?endDate}'

Basically I want {?startDate} which has a value of '5/1/2010' to be passed in as 20100105000000000
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 06 May 2010 at 3:31am
A command is good - it's about the only way you're going to be able to do this.
 
My suggestion is to think about it the other way around - instead of reformatting your parameters, reformat the field in the where clause - you can keep it as is in the select.  You still haven't told me what type of database you're using - the syntax for this is different in different databases.  If you're on SQL Server, you're going to change your where clause to the following:
 
WHERE cast((substring(E.A_ADDDTETME, 5, 2) + '/' + substring(E.A_ADDDTETME, 7, 2) + '/' + left(E.A_ADDDTETME, 4)) as datetime) BETWEEN '{?startDate}' AND '{?endDate}'
 
-Dell


Edited by hilfy - 06 May 2010 at 3:31am
IP IP Logged
kali
Newbie
Newbie


Joined: 05 May 2010
Online Status: Offline
Posts: 6
Quote kali Replybullet Posted: 06 May 2010 at 11:10am
Hi - Thanks for this. Yes the SQL runs fine in query analyser but I get an error when I try to run this report in Crystal Reports i get an error saying incorrect syntax SQL STate 4200 Native Error 102
 
My parameters are of type date
IP IP Logged
kali
Newbie
Newbie


Joined: 05 May 2010
Online Status: Offline
Posts: 6
Quote kali Replybullet Posted: 06 May 2010 at 11:46am
OK - I ran the report with SQL profiler in the background and found the problem to be that i was tsill putting quotes around the date params!
IP IP Logged
Post Reply Post New Topic
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



This page was generated in 0.047 seconds.