Link to home
Start Free TrialLog in
Avatar of Thomas Stockbruegger
Thomas StockbrueggerFlag for Germany

asked on

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
Avatar of Imran Javed Zia
Imran Javed Zia
Flag of Pakistan image

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
Avatar of Thomas Stockbruegger

ASKER

I got the error ToString is not a Element of COleDateTime

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


Use .Format instead of .ToString
ASKER CERTIFIED SOLUTION
Avatar of mayank_joshi
mayank_joshi
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
thank you for your help