• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 642
  • Last Modified:

Need some help to convert CString into DATETIME

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
Thomas Stockbruegger
Asked:
Thomas Stockbruegger
1 Solution
 
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
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
 
Thomas StockbrueggerAuthor Commented:
I got the error ToString is not a Element of COleDateTime
0
 
mayank_joshiCommented:

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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
AndyAinscowFreelance programmer / ConsultantCommented:
Use .Format instead of .ToString
0
 
mayank_joshiCommented:
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
 
Thomas StockbrueggerAuthor Commented:
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
 
Thomas StockbrueggerAuthor Commented:
thank you for your help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now