Insert a date into a sql query

3 ビュー (過去 30 日間)
Alexandre
Alexandre 2012 年 4 月 4 日
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
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
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 件のコメント
Alexandre
Alexandre 2012 年 4 月 4 日
Thanks Titus, it rocks ;) !
Titus Edelhofer
Titus Edelhofer 2012 年 4 月 4 日
Your welcome. You might mark the question as answered if you like...

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDatabase Toolbox についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by