Results 1 to 18 of 18

Thread: [RESOLVED] Cdate Data Type Mismatch

  1. #1

    Thread Starter
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Resolved [RESOLVED] Cdate Data Type Mismatch

    VB6 SP6

    I am try to convert a date string to date and insert it into a table (modDate Date). I am getting a data mis-match error but in debug, it appears that the data has been converted.

    Code:
    Dim ModDate1 As Date
    ModDate1 = CDate(Format(flDrawings.ModDate, "MM/DD/YYYY"))

    "VALUES ('" & IIf(IsNull(Name$), "a", RTrim(Name$)) & "','" & IIf(IsNull(Revision$), Null, RTrim(Revision$)) & "','" & IIf(IsNull(Descr$), Null, RTrim(Descr$)) & "'," & ModDate1 & ")"

    Immediate Results
    INSERT INTO engfiles(drwName,Rev,Descr,ModDt)VALUES ('WIDGETII','a','',7/12/2007)
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Cdate Data Type Mismatch

    why would you format the date data then convert it to a date as the formatting will be lost and the date will be as per you systems date format
    which part of the code gives the error?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Cdate Data Type Mismatch

    As westconn1 correctly said, the Date data type does not store formatting - only the value (and even tho it seems to be displayed as text, it is certainly not stored that way).

    Depending on what format you are using (and the system date format) this is likely to just change the date. 7/12/2007 could be July 12th or Dec 7th, and after running that code you cannot be sure which will be stored in your variable.

    If you want to store the formatted value to a variable, store it to a String variable.


    Next up, to use a date in an SQL statement you need to put some sort of delimiter around the value (otherwise you are basically using the mathmatical calculation (7/12)/2007 , which is 2.9e-4 !).

    To see which delimiter you need, read the article How do I use values (numbers, strings, dates) in SQL statements? from our Database Development FAQs/Tutorials (at the top of the Database Development forum)

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Cdate Data Type Mismatch

    What is the backe end DB. If Access then the date value (7/12/2007) needs to be surrounded by pould signs (#7/12/2007#). If SQL Server then surround with single qoutes ('7/12/2007'). Oracle needs the To_Date Function.

    MS Access
    Code:
    VALUES ('" & IIf(IsNull(Name$), "a", RTrim(Name$)) & "','" & IIf(IsNull(Revision$), Null, RTrim(Revision$)) & "','" & IIf(IsNull(Descr$), Null, RTrim(Descr$)) & "',#" & ModDate1 & "#)"
    SQL Server
    Code:
    VALUES ('" & IIf(IsNull(Name$), "a", RTrim(Name$)) & "','" & IIf(IsNull(Revision$), Null, RTrim(Revision$)) & "','" & IIf(IsNull(Descr$), Null, RTrim(Descr$)) & "','" & ModDate1 & "')"
    Oracle
    Code:
    VALUES ('" & IIf(IsNull(Name$), "a", RTrim(Name$)) & "','" & IIf(IsNull(Revision$), Null, RTrim(Revision$)) & "','" & IIf(IsNull(Descr$), Null, RTrim(Descr$)) & ",To_Date('" & ModDate1 & "','MM/DD/YYYY'))"
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Re: Cdate Data Type Mismatch

    The data source for the date is from a fixed length file (ModDate As String * 10) which has been entered in as e.g. 8-22-2007.

    The source DB is a VFP9 free table (modDate Date)
    Code:
    Dim ModDate1 as Date
    ModDate1 = flDrawings.ModDate

    "VALUES ('" & IIf(IsNull(Name$), "a", RTrim(Name$)) & "','" & IIf(IsNull(Revision$), Null, RTrim(Revision$)) & "','" & IIf(IsNull(Descr$), Null, RTrim(Descr$)) & "'," & ModDate1 & ")"

    This is what I get on debug.print, it appears to be correct but errors.
    INSERT INTO engfiles(drwName,Rev,Descr,ModDt)VALUES ('WIDGETII','a','',7/12/2007)
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

  6. #6

    Thread Starter
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Re: Cdate Data Type Mismatch

    Also tried Gary's post with single quote

    INSERT INTO engfiles(drwName,Rev,Descr,ModDt)VALUES ('WIDGETII','a','','7/12/2007')
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Cdate Data Type Mismatch

    For VFP, I would guess that date delimiter would be the same as Access (using #).

    it appears to be correct but errors.
    INSERT INTO engfiles(drwName,Rev,Descr,ModDt)VALUES ('WIDGETII','a','',7/12/2007)
    As I explained before, that is definitely wrong - you need delimiters of some sort (otherwise you will be storing tiny numbers, rather than dates).

    You also need to format date values before/whilst putting them into an SQL statement (either format them and store them to a string, or use the format function while appending them to the SQL).

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Cdate Data Type Mismatch

    Quote Originally Posted by si_the_geek
    You also need to format date values before/whilst putting them into an SQL statement (either format them and store them to a string, or use the format function while appending them to the SQL).
    Or store them as dates (so that things like BETWEEN work) and format them after retrieval.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  9. #9

    Thread Starter
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Re: Cdate Data Type Mismatch

    Like this? Could you give me an example?

    Code:
    Dim ModDate1$
    
    ModDate1$ = flDrawings.ModDate
    '" & Format(ModDate1$, "MM/DD/YYYY") & "')
    I tried
    Code:
    #" & Format(ModDate1$, "MM/DD/YYYY") & "#)
    but got syntax error.
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Cdate Data Type Mismatch

    You could use this:
    Code:
    Dim ModDate1 as String
    ModDate1 = Format(flDrawings.ModDate, "MM/DD/YYYY")
    
    ... & "',#" & ModDate1 & "#)"
    or this:
    Code:
    Dim ModDate1 as Date
    ModDate1 = flDrawings.ModDate
    
    ... & "',#" & Format(ModDate1, "MM/DD/YYYY") & "#)"
    or this:
    Code:
    ... & "',#" & Format(flDrawings.ModDate, "MM/DD/YYYY") & "#)"
    (all of these are assuming that the delimiter is # , tho you may need to use ' instead).
    Quote Originally Posted by Al42
    Or store them as dates (so that things like BETWEEN work) and format them after retrieval.
    Unless I read it incorrectly, the table values are Dates ("The source DB is a VFP9 free table (modDate Date) ").

    Inside the SQL statement (which is a string), you cannot have actual dates - only strings that represent dates. To work correctly, they need to be formatted in a way that will be interpreted by the DBMS as you intended (which means formatting them as mm/dd/yyyy, or better yyyy-mm-dd).

    If a command object was being used, using a Date value with a parameter object is correct - as it will basically do the formatting for you.

  11. #11

    Thread Starter
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Re: Cdate Data Type Mismatch

    The source data is a fixed length file, the target is a VFP9 free table. (sorry I had source in there twice as the VFP table should have been target)

    The date has been entered into the file in the format of "08-22-2007"

    Code:
    Public Type drwFile
    Name            As String * 200         'drawing name for the loop
    Revision        As String * 32          'revision value for this drawing
    CrDate          As String * 10          'date the drawing was created
    ModDate         As String * 10          'date the drawing was last changed/overwritten
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Cdate Data Type Mismatch

    Is the modDate field (in the database) a Date data type, or is it a String/Char?

  13. #13

    Thread Starter
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Re: Cdate Data Type Mismatch

    It is a Date type. The source is string/char
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Cdate Data Type Mismatch

    In that case you need to use one of the formats that I mentioned, but you already have that from the source, with something that may or may not be issue (it has - rather than / ).

    Depending on how VFP wants to receive dates, one of these will hopefully work for you:
    ... & "',#" & flDrawings.ModDate & "#)"
    ... & "','" & flDrawings.ModDate & "')"
    ... & "',#" & Replace(flDrawings.ModDate,"-","/") & "#)"
    ... & "','" & Replace(flDrawings.ModDate,"-","/") & "')"

    If not, you should check the documentation to find out what it wants.

  15. #15

    Thread Starter
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Thumbs up Re: Cdate Data Type Mismatch

    Got it! The delimeter is Brackets

    ... "', {" & Replace(LstRefDat$, "-", "/") & "},
    Last edited by Always_Confused; Sep 5th, 2007 at 05:37 PM.
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Cdate Data Type Mismatch

    Excellent, thanks for sharing the solution.

    So that I can add it to the FAQ properly, can you just confirm that by VFP9 you mean MS Visual Foxpro 9?

  17. #17

    Thread Starter
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Re: [RESOLVED] Cdate Data Type Mismatch

    Yes VFP9 is Visual Foxpro v9. This should also be valid back to at least v6 as well.

    For those looking for a client side or server side database far more robust than Access, look into Foxpro! Microsoft is still supporting Foxpro despite of what you hear and has just released Sedna to enhance the technologies to version 9. Version 9 is to be supported to 2015.

    http://www.microsoft.com/downloads/d...ng=en#Overview

    The DB stucture will be around for a long time and now has varchar fields. I have found it perfect for my situation as an intermediate step before taking the application to full blown SQL. And it requires no DB engine files to run in the background on individual installations as does MSDE and others.

    Another plus, the vfpoledb driver is free to download via Microsoft and does not require you to purchase the application to develop the free tables or DB.
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

  18. #18
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Cdate Data Type Mismatch

    Thanks for the confirmation - I'll add it to the FAQ now.

    While I see that VFP could be useful (and I'm sure many people will use it), there is a new version of SQL Server (CE) which does basically the same thing - with the added benefits that the upgrade path to a 'bigger' version of SQL Server is much simpler, and that it presumably runs on more devices (such as PDAs).

    SQL Server CE is also a free download, and there is also has a free management tool for designing tables etc - there are links to both in the SQL Server 2005 thread at the top of the Database forum.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width