compare date in sql statement

久未见 提交于 2019-12-25 00:50:33

问题


I am making an alert tool using web sql.

I want to selects all events that occur in a specific day. when I insert the event I insert the date as a date object with this format "2011-10-14" , What should I write in select statement to select all events that occur today ?

I make like this

 cur =new Date();
alert(cur);
db.transaction(function(tx){
    tx.executeSql("select NOTE_DESC,NOTE_DATE,NOTE_TIME,ALERT_TIME,NOTES_MORE_DETAILS from NOTES where NOTE_DATE = "+cur+" order by NOTE_TIME asc limit 0,1",[], alert("yes"),onError);
});

In which my query is:

select NOTE_DESC, NOTE_DATE, NOTE_TIME, ALERT_TIME, NOTES_MORE_DETAILS 
from NOTES 
where NOTE_DATE = "+cur+" 
order by NOTE_TIME asc 
limit 0,1

but an error message occur which is "near Oct: syntax error"

How should I make the date comparison in sql ??


回答1:


I believe you want:

NOTE_DATE = '"+cur.getFullYear()+'-'+(cur.getMonth()+1)+'-'+cur.getDate()+"'

Note the ' wrapping the value for the query variable, and calling the cur date object's methods to construct the date.

http://jsfiddle.net/yXgDw/

Better yet, use a prepared query:

http://www.w3.org/TR/webdatabase/#dom-sqltransaction-executesql

http://www.w3.org/TR/webdatabase/#introduction



来源:https://stackoverflow.com/questions/7748205/compare-date-in-sql-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!