简体   繁体   English

转换为日期时间不起作用 sql 查询

[英]convert to datetime not working sql query

i have this:我有这个:

let startDateRange = loadYear + '-' + loadMonth + '-01'
let endDateRange = loadYear + '-' + loadMonth + '-31'
var getschedule = "select * from SCHEDULE where DATE BETWEEN '" + CONVERT(DATETIME, startDateRange) + "' AND '" + CONVERT(DATETIME, endDateRange) + "'"

and in startDateRange and endDateRange , it is formatted in a string like so:startDateRangeendDateRange中,它被格式化为一个字符串,如下所示:

'2021-Dec-01'

It is giving the error:它给出了错误:

ReferenceError: CONVERT is not defined

how can i fix this?我怎样才能解决这个问题?

You're missing some fundamentals here, I think.我认为你在这里遗漏了一些基础知识。

var getschedule = "select * from SCHEDULE where DATE BETWEEN '" + CONVERT(DATETIME, startDateRange) + "' AND '" + CONVERT(DATETIME, endDateRange) + "'"`

Javascript thinks CONVERT is a function that you've defined in your Javascript code rather than a SQL command. Javascript 认为CONVERT是您在 Javascript 命令中定义的 function,而不是 Z97708840A067410CB037。 Though you didn't provide a ton of context, what you're probably trying to do is construct a complete SQL query.尽管您没有提供大量上下文,但您可能正在尝试构建一个完整的 SQL 查询。 If you want to interpolate the values you've calculated in your Javascript to this query you're going to give to your SQL database, you need to construct a complete string.如果您想将您在 Javascript 中计算的值插入到您将提供给 SQL 数据库的此查询中,您需要构造一个完整的字符串。

It might be easier (or more readable) to use template literals :使用模板文字可能更容易(或更易读):

const getschedule = `select * from SCHEDULE where DATE BETWEEN 'CONVERT(DATETIME, ${startDateRange})' AND 'CONVERT(DATETIME, ${endDateRange})'`

Or you can concatenate strings as you tried the first time:或者您可以在第一次尝试时连接字符串:

const getschedule = "select * from SCHEDULE where DATE BETWEEN 'CONVERT(DATETIME, " + startDateRange + ")' AND 'CONVERT(DATETIME, " + endDateRange + ")'";

Full disclosure: I have no idea whether or not this SQL query will be valid for you, it's just clear that you're thinking the wrong way about Javascript functions and how to construct strings.全面披露:我不知道这个 SQL 查询是否对您有效,很明显您对 Javascript 函数以及如何构造字符串的想法是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM