question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sql.DateTime fails on SQL 2005

See original GitHub issue
 days = Math.floor((parameter.value.getTime() - UTC_EPOCH_DATE.getTim
                                    ^
 TypeError: undefined is not a function

In data-type.js you have missed to convert date string to object as you have done for DateTime2

       if (parameter.value != null) {
      parameter.value = new Date(+parameter.value);  // this line is missing -  line 261
       if (options.useUTC) {

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

1reaction
fontztercommented, Nov 10, 2017

Yes, @agarstang it works. What I meant is that you pass/use a VarChar in your query/proc instead of a DateTime, so it never needs to be converted. SQL does the conversion when it processes the query; and, as long as the varchar string holding the date is formatted in an acceptable way, the implicit conversion works fine. So, something like this works for me…

new sql.Request()
  .input('an_id', sql.Int, some_js_id)
  .input('a_js_date_formated_as_a_string', sql.VarChar, dateFormat(a_js_date_object, 'yyyy-mm-dd HH:MM:ss'))
  .query(`update some_table
            set an_actual_sql_DateTime_field = @a_js_date_formated_as_a_string
            where the_id = @an_id`
  ).then(...

I’m using 4.1.0

1reaction
atsagecommented, Aug 22, 2017

As stated above, the fix is simple. Please change the if statement on line 300 to

  if (parameter.value != null) {
    var days = void 0,
        dstDiff = void 0,
        milliseconds = void 0,
        seconds = void 0,
        threeHundredthsOfSecond = void 0,
        time = new Date(+parameter.value);
    if (options.useUTC) {
      days = Math.floor((time.getTime() - UTC_EPOCH_DATE.getTime()) / (1000 * 60 * 60 * 24));
      seconds = time.getUTCHours() * 60 * 60;
      seconds += time.getUTCMinutes() * 60;
      seconds += time.getUTCSeconds();
      milliseconds = seconds * 1000 + time.getUTCMilliseconds();
    } else {
      dstDiff = -(time.getTimezoneOffset() - EPOCH_DATE.getTimezoneOffset()) * 60 * 1000;
      days = Math.floor((time.getTime() - EPOCH_DATE.getTime() + dstDiff) / (1000 * 60 * 60 * 24));
      seconds = time.getHours() * 60 * 60;
      seconds += time.getMinutes() * 60;
      seconds += time.getSeconds();
      milliseconds = seconds * 1000 + time.getMilliseconds();
    }

    threeHundredthsOfSecond = milliseconds / (3 + 1 / 3);
    threeHundredthsOfSecond = Math.round(threeHundredthsOfSecond);

    buffer.writeUInt8(8);
    buffer.writeInt32LE(days);

    return buffer.writeUInt32LE(threeHundredthsOfSecond);
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

EF and datetime in SQL Server 2005 - Stack Overflow
I am testing an application on server with SQL Server 2005 and I am getting the following error (the application runs fine with...
Read more >
Salesforce to SQL Server 2005 Datetime Error
I am trying to updte a SQL Server table when a new record is inserted into Salesforce. The flow works perfectly but the...
Read more >
How to format datetime & date in Sql Server 2005
First we start with the conversion options available for sql datetime formats with century (YYYY or CCYY format). Subtracting 100 from the Style ......
Read more >
SQL Server Error Messages - Msg 242
Causes: This error occurs when trying to convert a string date value into a DATETIME data type but the date value contains an...
Read more >
Backup of misconfigured SQL Server DB fails due to unknown ...
Backup fails with the error below when using SQL Server 2008 or newer database configured as SQL Server 2005 database. The error is:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found