Insert a date into a sql query
3 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a problem with a query that I realised today. I can't integrate an input in the query.
This exec works :
e = exec(conn,'SELECT date_publication,to_days(horodatage)-to_days(date_publication), horodatage, valeur FROM point_courbe_ref p, mesure_ref m WHERE p.FK_mesure_ref = m.ID_mesure_ref AND m.FK_filiere_production = 6 AND m.FK_origine_mesure =10 AND horodatage BETWEEN {ts "2012-01-01 00:00:00"} AND {ts "2012-01-01 23:59:00"} AND to_days(horodatage)-to_days(date_publication)= 1 ');
e = fetch(e)
close(e)
but this one doesn't :
date_debut = input('Entrer la date de debut au format : aaaa-mm-dd -> ','s')
date_debut = datestr(date_debut,'yyyy-mm-dd' );
date_fin = input ('Entrer la date de fin au format : aaaa-mm-dd -> ','s');
date_fin = datestr(date_fin,'yyyy-mm-dd' );
e = exec(conn,'SELECT date_publication,to_days(horodatage)-to_days(date_publication), horodatage, valeur FROM point_courbe_ref p, mesure_ref m WHERE p.FK_mesure_ref = m.ID_mesure_ref AND m.FK_filiere_production = 6 AND m.FK_origine_mesure =10 AND horodatage BETWEEN {ts "date_debut"} AND {ts "date_fin"} AND to_days(horodatage)-to_days(date_publication)= 1 ');
e = fetch(e)
close(e)
I tried several ways, but I don't know how to solve it ! Thanks :) !
1 个评论
Marc Freed
2013-1-8
I have just solved this problem with some help from Matlab support.
>> mydata = fetch(conn,... ['SELECT colnames'... ' FROM tablename'... ' WHERE Date=''2010-12-31''']); >>
It's all about the correct placement of the single quotation marks and the spaces.
You must place two single quotation marks before the date and two after it with no spaces between them. If the "WHERE Date = ''yyyy-mm-dd'' is the end of your query, then you will need a third single quotation mark.
Also, you must not have a space at the end of each line. ' ... is incorrect. '... is correct.
You must have a space at the start of each line between the first single quotation mark and the first term. 'FROM ... is incorrect. ' FROM ... is correct.
Good luck.
采纳的回答
Titus Edelhofer
2012-4-4
Hi,
you want to take the variables date_debut and date_fin, not the string itself, so:
e = exec(conn,['SELECT date_publication,to_days(horodatage)-to_days(date_publication), horodatage, valeur FROM point_courbe_ref p, mesure_ref m WHERE p.FK_mesure_ref = m.ID_mesure_ref AND m.FK_filiere_production = 6 AND m.FK_origine_mesure =10 AND horodatage BETWEEN {ts "' date_debut '"} AND {ts "' date_fin '"} AND to_days(horodatage)-to_days(date_publication)= 1 ']);
Titus
2 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Database Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!