9

I have a Web Api using express and Tedious to store some data on Azure SQL database. With nvarchar types and int types it works well, but when I try to save DateTime value I get an error message:

Insert into Proxy (Ip, RequisitionDate) 
values ('1', '2016-05-18 3:32:21' )

Error:

RequestError: Validation failed for parameter 'RequisitionDate'. Invalid date.] message: 'Validation failed for parameter \'RequisitionDate\'. Invalid date.', code: 'EPARAM' }

Well, the interesting thing is that

Insert into Proxy (Ip, RequisitionDate) 
values ('1', '2016-05-18 3:32:21')

is the query that i execute in node.js api:

var query = "Insert into Proxy (Ip,RequisitionDate) values ( '"+ ip + "', '"+ date + "' )";
console.log(query); // Insert into Proxy (Ip,RequisitionDate) values ( '1', '2016-05-18 3:32:21' )

request = new Request(query, function(err) {
    if (err) {
        console.log(err);}
    });

    request.addParameter('Ip', TYPES.NVarChar,'SQL Server Express 2014');
    request.addParameter('RequisitionDate', TYPES.DateTime , 'SQLEXPRESS2014');

    connection.execSql(request);
}

If I execute the query direct on the SqlManager Studio, it works ok.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
gog
  • 11,788
  • 23
  • 67
  • 129

3 Answers3

9

It seems that you haven't set the correct datetime value in addParameter function. According the API reference, the function is used as request.addParameter(name, type, value, [options]).

Please try the following code:

var query = "Insert into Proxy (Ip,RequisitionDate) values ( @ip , @date)";

request = new Request(query, function(err) {
    if (err) {
        console.log(err);}
    });

    request.addParameter('ip', TYPES.NVarChar,'<ip value>');
    request.addParameter('date', TYPES.DateTime , new Date());
// or the set the special date, refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
//  request.addParameter('RequisitionDate', TYPES.DateTime , new Date(2016,05,19,3,32,21))
    connection.execSql(request);
}

Any further concern, please feel free to let me know.

Gary Liu
  • 13,758
  • 1
  • 17
  • 32
1

A lot of people are having this issue. This is how I normally solve it. Firstly, we need to understand that SQL tries to convert whatever date you send from your NodeJS code to UTC. So, how does SQL know how much difference should it add to make your date to UTC?

The answer is :

SHOW VARIABLES LIKE '%time_zone%';

if you write these command in sql query, it will show you system_time_zone and time_zone of your sql configuration. In cases, where you simply send SQL datetime without mentioning the timezone from NodeJS, it will use this time_zone to convert your date to UTC. Now, what I recommend developers is to let SQL use UTC to store date and time and only convert the date to users timezone in frontend. What I personally do is, I send datetime to SQL along with the timezone.

moment.utc().format('YYYY-MM-DD HH:mm:ss Z')

Here Z is the timezone. The date given by this code is

2022-02-18 06:56:23 +00:00

Since, I used moment.utc(), the timezone is automatically set as +0:00. If I send this format to SQL to store date, it will no longer try to convert the date to UTC format. But if I do

moment.utc().format('YYYY-MM-DD HH:mm:ss');

SQL will convert this datetime using the system timezone offset, even if its already in UTC. I recommend you not to use dialectOptions in your database connection module, since in future it will not be supported. This is from my opinion a good way to store the date and time in sql. Now, as for showing the stored date to users. You can easily convert the UTC date stored in database to local syntax by using

moment(<database UTC datetime>).local().format('YYYY-MM-DD HH:mm:ss');

Good Luck, Have Fun Coding.

ReflexEcho
  • 66
  • 1
  • 9
0

Very quickly, you can use SQL server CONVERT function to convert your date string to be compatible with SQL server. Below is an example:

Insert into Proxy (Ip, RequisitionDate) values ('1', CONVERT(VARCHAR(10), '2020-8-24'))

For more info, you can check here: http://www-db.deis.unibo.it/courses/TW/DOCS/w3schools/sql/func_convert.asp.html

Sameeksha Kumari
  • 1,158
  • 2
  • 16
  • 21