?
Solved

Need some help to convert CString into DATETIME

Posted on 2011-05-04
7
Medium Priority
?
608 Views
Last Modified: 2012-05-11
Hello,
I would like to convert a CString into DATETIME.

In my SQL Table the row Lieferscheindatum has the DATETIME format.
The user can fill 2 Editboxen with the start date and stop date.

f.e.
start date                 stop date
01.03.2011               04.05.2011  (f.e.todays date)

So I have 2 strings
CString start_date="01.03.2011"
CString stop_date="04.05.2011"



I tried the following...but it will not work
 COleDateTime start;
start.ParseDateTime(start_date);

COleDateTime stop;
stop.ParseDateTime(stop_date);


sql.Format("SELECT * FROM ViewLieferscheine WHERE \
                         Lieferscheindatum  BETWEEN %d AND %d \
                        ORDER BY CAST(LieferscheinNr AS BIGINT)DESC",start,stop);




This will work....but I need the dates from the Editboxen, so I have to convert the CString
sql.Format("SELECT *FROM ViewLieferscheine
WHERE Lieferscheindatum BETWEEN '20110301' AND '20110504'\
ORDER BY CAST(LieferscheinNr AS BIGINT)DESC");

500 points with a solution.
Please help.
Best regards,
Thomas
a.BMP
0
Comment
Question by:tsp2002
7 Comments
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 35689227
Please try it as following:

sql.Format("SELECT * FROM ViewLieferscheine WHERE
                         Lieferscheindatum  BETWEEN {0} AND {1} \
                        ORDER BY CAST(LieferscheinNr AS BIGINT)DESC",start.ToString("yyyyMMdd"),stop.ToString("yyyyMMdd"));


Thanks
0
 

Author Comment

by:tsp2002
ID: 35689269
I got the error ToString is not a Element of COleDateTime
0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35689472

framing the query like this may help:-

SELECT * FROM ViewLieferscheine
WHERE Lieferscheindatum BETWEEN  convert(datetime,'01.03.2011',103)  AND
convert(datetime,'04.05.2011',103)
ORDER BY CAST(LieferscheinNr AS BIGINT)DESC

Open in new window


0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 35689495
Use .Format instead of .ToString
0
 
LVL 9

Accepted Solution

by:
mayank_joshi earned 2000 total points
ID: 35689575
what i mean to say is:-

sql.Format("SELECT * FROM ViewLieferscheine WHERE
 Lieferscheindatum  BETWEEN convert(datetime,'{0}',103) AND convert(datetime, '{1}' ,103)
   ORDER BY CAST(LieferscheinNr AS BIGINT)DESC",start,stop);

Open in new window

0
 

Author Comment

by:tsp2002
ID: 35689587
yes....thank you convert will work, here is the complete solution

CString start="01.03.2011";
CString stop="01.04.2011";

             sql.Format("SELECT * FROM ViewLieferscheine WHERE \
                           Lieferscheindatum BETWEEN CONVERT (datetime,'%s',103) AND  
                                                                   CONVERT(datetime,'%s',103) \
                           ORDER BY CAST(LieferscheinNr AS BIGINT)DESC",start,stop);
0
 

Author Closing Comment

by:tsp2002
ID: 35689602
thank you for your help
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question