我有一些从客户端接收的日期字段。基本上,我想在我的SQL数据库中搜索这个日期。我该如何在查询中传递年,月和日期?我只想用从客户端收到的新日期替换日期。
如何使用mssql驱动程序实现此目的? https://www.npmjs.com/package/mssql
的node.js
console.log(date.year);//2017
console.log(date.month);//11
console.log(date.day);//28
const pool = new sql.ConnectionPool(config, err => {
if (err)
console.log(err);
else
console.log("Connection successful");
pool.request()
.query(`SELECT * FROM testdB WHERE DateCreated >= '11-28-2017 12:55:00' and DateCreated <='11-28-2017 12:57:00'
ORDER BY DateCreated DESC`
updated1
var d_lower = new Date(date.year, date.month, date.day, 7,55,0);
var d_upper = new Date(date.year, date.month, date.day, 8,10,0);
pool.request()
.input('dateParam_lower', sql.Date, d_lower).input('dateParam_upper', sql.Date,d_upper )
.query('SELECT * from testdB WHERE DateCreated BETWEEN @dateParam_lower and @dateParam_upper', (err, result) => {
if (err)
console.log(err);
else
console.log(d_lower);
console.log(d_upper);
console.log(result);
答案 0 :(得分:1)
根据文档,你应该使用输入函数将你的javascript日期数据类型映射到sql日期数据类型
https://www.npmjs.com/package/mssql#input-name-type-value
这里有一个非常明显的例子
https://www.npmjs.com/package/mssql#promises
return pool.request()
.input('my_date_param', sql.Date, date_param_variable_name)
.query('select * from mytable where created_at > @my_date_param')