Results 1 to 14 of 14

Thread: SQL insert date problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    SLOVENIA, Europe
    Posts
    110

    Post

    I have SQL on Access database like this:

    insert into customer (id, name, date) values (12,'Max', #12/12/1999#)

    Actually I have more complicated SQL string but my problem is here.

    With this SQL string I want to insert record with date is null. So I generate next sql sentence:

    insert into customer (id, name, date) values (12,'Max', ##) which (of course) return error.

    What should I do?

    Thnx, Ermin


  2. #2
    Hyperactive Member
    Join Date
    Feb 2000
    Posts
    284

    Post

    Have you tried using empty single quotes or even the keyword NULL?

    I'm not sure but one of these might work.

  3. #3
    Lively Member
    Join Date
    Dec 1999
    Location
    Karlsruhe, Germany
    Posts
    122

    Post

    Keyword NULL works (at least with Access).

    Roger

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    SLOVENIA, Europe
    Posts
    110

    Post

    Yes, null works.

    My SQL looks like this:

    "insert into table (ID, dat) value (12, #" & myDate & "#)"

    So I changed myDate string:

    if len(mydate)=0 then
    myDate="null"
    else
    mydate="#" & mydate & "#"
    end if

    an sql string looks like this:

    "insert into table (ID, dat) value (12," & myDate & ")"

    and it works.

    Thanx anyway!

    Ermin

  5. #5
    Lively Member
    Join Date
    Jul 1999
    Posts
    78

    Post

    You do realize that you're passing a text string named null and not an actual null value?

    Just calling your attention to it; it might not matter for you in these circumstances.

    If you want to do it properly, I think you would need the statement to read:

    myDate = Null

  6. #6
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482

    Post

    Perhaps I'm missing something; does not "insert into customer (id, name) values (12,'Max')" leave unreferenced fields empty?


  7. #7
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105

    Post

    Depends on the DBMS. Access, for instance, will assign default values for those fields that have them...

  8. #8
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Post

    why not using the VbNull.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    SLOVENIA, Europe
    Posts
    110

    Post

    That wasn't really my problem, which I already solved.

    I had very, very long insert sql sentence and I yust set string

    if len(mydate)=0 then
    myDate="null"
    else
    mydate="#" & mydate & "#"
    end if

    because sometimes I need date and sometimes I don't and I have to use only one sql statement.

    Finally my sql loks like this:

    insert into table (id, dat) values (12," & myDate & ")"


    If myDate is null:

    insert into table (id, dat) values (12, null)

    if myDate is OK:

    insert into table (id, dat) values (12, #12-jan-2000#)


    [This message has been edited by ermingut (edited 02-21-2000).]

    [This message has been edited by ermingut (edited 02-21-2000).]

  10. #10
    Lively Member
    Join Date
    Jan 1999
    Location
    Gloucester, UK
    Posts
    78

    Post

    The best answer is to build your SQL statement in an appropriate manner, setting variables as necessary.

    ie

    Dim strSQL as string
    Dim myDate as date

    IF <whatever your criteria is to set myDate> then
    mydate = <your value>
    endif

    strSQL = "INSERT INTO <mytable> (id, dat) VALUES ("
    strSQL = strSQL & lngCustomerID
    strSQL = strSQL & ", " & iif(myDate > nothing, "#" & mydate & "#","NULL")
    strsql = strsql & ")"

    dbMyDatabase.execute(strsql, dbfailonerror)

    Hope this helps..
    VorTechS

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    SLOVENIA, Europe
    Posts
    110

    Post

    I don't know how could I miss IIF command. I didn't try it jet, but as I can see in your code, it is exactly command I've been looking for.

    Thanks, Ermin

  12. #12
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105

    Post

    Use IIF sparingly, if at all. It is significantly slower than any alternative, it always evaluates the entire statement and if any part of the statement fails, the whole statement will fail (even if the part that fails isn't being used).

    I would do Vortech's code, without using IIF, like:
    Code:
    Dim strSQL as string
    Dim myDate as string
    
    myDate=vbNull
    
    IF <whatever your criteria is to set myDate> then
       myDate = <your value>
    endif
    
    
    strSQL = "INSERT INTO <mytable> (id, dat) VALUES ("
    strSQL = strSQL & lngCustomerID
    strSQL = strSQL & ", " & 
    if isnull(myDate) then
       strSQL = strSQL & "NULL"
    else
      strSQL = strSQL & "#" & mydate & "#"
    endif
    strsql = strsql & ")"

  13. #13
    Lively Member
    Join Date
    Jul 1999
    Posts
    78

    Post

    As an FYI:

    In order to use Null in VB, the variables must be declared as a Variants.

    Pg. 109 in Microsoft Visual Basic 6.0 Programmer's Guide.

  14. #14
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105

    Post Re: SQL insert date problem


    As an FYI:

    In order to use Null in VB, the variables must be declared as a Variants.

    Pg. 109 in Microsoft Visual Basic 6.0 Programmer's Guide.

    Same behavior in VB5. The use of the constant vbNull sets the string value to "1", which is not what was intended (although the code and SQL should work. Instead of the date field having null as a value you'd have "1" or a valid date value).

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