insert date into table

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default insert date into table

    having some trouble inserting a date into a smalldatetime field in a MS SQL
    table. I get this as an error ODBC Error Code = 22008 (Datetime field
    overflow) 22008 [Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error
    converting datetime from character string. here is what I use to get the
    date. #DateFormat(CreateODBCDateTime(now()), 'm/d/yy')# I have also tried
    #CreateODBCDateTime(Now())# and I get the same error

    JoeyTMann Guest

  2. Similar Questions and Discussions

    1. How to insert date into db?
      I've been fighting with this simple (?) issue all weekend long and have gotten nowhere. I want to have the current date and time inserted into the...
    2. How Do you insert a table in Previous Table
      How Do you insert a table in Previous Table that has an image, in a web page
    3. Login - multi table insert for registrant; subsquent login insert page requests into joined 'Selection' Table
      Question regards insert and updates in sql server for a simple login script that requires registration the first time and only "email address" upon...
    4. ASP SQL Insert NULL Date Value
      Hello, I've been pulling my hair out trying to figure this out. Thank you in advance for taking the time to look at this. I'm trying to...
    5. Insert date
      Leave the Control Source set to the field in the table that you want to store the date in. In the Form's BeforeUpdate event, set the value of this...
  3. #2

    Default Re: insert date into table

    Instead of #now()#, you might try using getdate() directly, since it returns
    the current date and time as a datetime object (unless your SQL Server is on a
    different time than your ColdFusion server).

    FYI, I tested inserting a datetime and smalldatetime in a SQL Server database
    and did not get any errors using #now()#, #CreateODBCDateTime(Now())#, or
    getdate(), so I'm not sure why you are getting errors.

    Phil

    paross1 Guest

  4. #3

    Default Re: insert date into table

    The getdate() function is not a ColdFusion function, but a SQL Server one, so
    you should be able to use it in a query with enclosing it in any # characters.
    (For Oracle, you could use sysdate(), in Access use now() for datetime or
    date() for date only, and it looks like MySQL can use sysdate(), now(), or
    current_timestamp).


    paross1 Guest

  5. #4

    Default Re: insert date into table

    OH ok. Stil not sure on this one. I can use the CreateODBCDateTime(now()) only
    if I have the column datatype to be varchar. When I pick timestamp, datetime,
    or smalldatetime I can't change the length. I am wondering since
    CreateODBC...outputs something like {ts '2005-03-21 12:54:08'} which is a lot
    longer than the 4-8 data lenghts of the datatypes. Should I have two seperate
    columns one called date with 03/21/05 and another called time with 12:00 in it?

    JoeyTMann Guest

  6. #5

    Default Re: insert date into table

    Don't confuse what is displayed whn you select a datetime column with what is
    actually stored there. They are NOT the same thing. Each relational database
    has its own way of storing date or datetime datatypes internally, which are
    "converted" to something resembling a text date and time value when selected
    and displayed. In order to insert a date into a datetime column, you either
    need to insert a datetime object, or convert a "text" date to one using a
    function, such as CreateODBCDateTime() when using an ODBC datasource. If your
    column is VARCHAR then you shouldn't use CreateODBCDateTime(), as a text value
    for a date should be stored without error, but I don't know how your particular
    datrabase is configured. If you are using CFQUERYPARAM or CFPROCPARAM the
    CFSQLtype would be CF_SQL_TIMESTAMP

    From SQL Server BOL:

    datetime
    Date and time data from January 1, 1753 through December 31, 9999, to an
    accuracy of one three-hundredth of a second (equivalent to 3.33 milliseconds or
    0.00333 seconds). Values are rounded to increments of .000, .003, or .007
    seconds, as shown in the table.

    smalldatetime
    Date and time data from January 1, 1900, through June 6, 2079, with accuracy
    to the minute. smalldatetime values with 29.998 seconds or lower are rounded
    down to the nearest minute; values with 29.999 seconds or higher are rounded up
    to the nearest minute.

    Values with the datetime data type are stored internally by Microsoft SQL
    Server as two 4-byte integers. The first 4 bytes store the number of days
    before or after the base date, January 1, 1900. The base date is the system
    reference date. Values for datetime earlier than January 1, 1753, are not
    permitted. The other 4 bytes store the time of day represented as the number of
    milliseconds after midnight.

    The smalldatetime data type stores dates and times of day with less precision
    than datetime. SQL Server stores smalldatetime values as two 2-byte integers.
    The first 2 bytes store the number of days after January 1, 1900. The other 2
    bytes store the number of minutes since midnight. Dates range from January 1,
    1900, through June 6, 2079, with accuracy to the minute.

    Phil

    paross1 Guest

  7. #6

    Default Re: insert date into table

    OK Important things to know. What kind of things should I look for since
    #CreateODBCDateTime(Now())# throws me the original message. CF_SQL_Type should
    TIMESTAMP right? Then if I used smalldatetime as the datatype of the column
    that should work ok?

    JoeyTMann Guest

  8. #7

    Default Re: insert date into table

    Since you didn't include the actual code you are using in your page, it is hard
    to give you specifics, only generalities. You included only the error output
    and one little piece of code where you were calling CreateODBCDateTime. By the
    way, what version of ColdFusion are you using?

    Phil

    paross1 Guest

  9. #8

    Default Re: insert date into table

    Sorry, I am using CF5. Here is the query block. <cfquery name='insertticket'
    datasource='helpdesk'> insert into tickets (username,
    comp_type,category,problem,create_date,building,so ftware,phone,room) values
    (<cfqueryparam cfsqltype='cf_sql_varchar' value='#form.username#'>,
    <cfqueryparam cfsqltype='cf_sql_varchar' value='#form.comptype#'>,
    <cfqueryparam cfsqltype='cf_sql_varchar' value='#form.category#'>,
    <cfqueryparam cfsqltype='cf_sql_varchar' value='#form.problem#'>, <cfqueryparam
    cfsqltype='CF_SQL_VARCHAR' value='CreateODBCDateTime(now())'>, <cfqueryparam
    cfsqltype='cf_sql_varchar' value='#form.building#'>, <cfqueryparam
    cfsqltype='cf_sql_varchar' value='#form.software#'>, <cfqueryparam
    cfsqltype='cf_sql_varchar' value='#form.phone#'>, <cfqueryparam
    cfsqltype='cf_sql_varchar' value='#form.room#'>) </cfquery> If I change the
    cfsqltype to cf_sql_timestamp thats when I get the error. The only way I have
    gotten a date into the field is by using varchar as the cfsqltype and the
    column is varchar.

    JoeyTMann Guest

  10. #9

    Default Re: insert date into table

    This is the result of some testing that I did with various changes to the VALUE
    parameter and CFSQLTYPE, against a datetime column, and a smalldatetime column
    in a SQL Server 2000 database using MX 6.1:

    <cfqueryparam cfsqltype="CF_SQL_varchar" value="CreateODBCDateTime(now())">
    error: [Macromedia][SQLServer JDBC Driver][SQLServer]Syntax error converting
    datetime from character string.

    <cfqueryparam cfsqltype="CF_SQL_timestamp" value="CreateODBCDateTime(now())">
    error; The cause of this output exception was that:
    coldfusion.runtime.Cast$DateStringConversionExcept ion: The value
    "CreateODBCDateTime(now())" could not be converted to a date..

    <cfqueryparam cfsqltype="CF_SQL_timestamp" value="#CreateODBCDateTime(now())#">
    Worked OK

    <cfqueryparam cfsqltype="CF_SQL_timestamp" value="#now()#">
    Also worked OK

    Phil

    paross1 Guest

  11. #10

    Default Re: insert date into table

    <cfqueryparam cfsqltype='CF_SQL_timestamp' value='#CreateODBCDateTime(now())#'>

    Yeah that worked. Not sure why i didn't have the ## in there...thanks for the help Phil.
    JoeyTMann Guest

  12. #11

    Default Re: insert date into table

    <cfqueryparam cfsqltype='CF_SQL_timestamp' value='#CreateODBCDateTime(now())#'>

    Yeah that worked. Not sure why i didn't have the ## in there...thanks for the help Phil.
    JoeyTMann Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139