How to write database query using sprintf command?
古いコメントを表示
I have a POSTGRESQL database from which has a column named 'referencetime' which is a timestamp. (e.g. '04/06/2022 12:41:46.5'). I am trying to fetch all data for date 04/06/2022. I am using the following command:
referencetime = char(subjectData.referencetime);
date = referencetime(1:10);
% Select data of same date
dataOnDateQuery = sprintf("SELECT subjectid, trialname, referencetime " + ...
"FROM live_table WHERE referencetime = '%s';",date);
But it is not working for me. It works if I hard code it. for example
dataOnDateQuery = "SELECT subjectid FROM live_table WHERE referencetime ILIKE '04/06/2022%';";
But I need the date query to be generalized by '%s' to use in a n algorithm. How can implement both %s and LIKE in the same query? I have attached live_table and the .m file for your reference.
4 件のコメント
the cyclist
2022 年 11 月 23 日
It can be a little tricky to get the syntax of these just right. Can you upload the data? Ideally upload subjectData, so that we can run your whole code, but just knowing date should be enough.
As an aside, it is a bad idea to name a variable "date", because that is a MATLAB function. That can come back and bite you. But I don't think that is the issue here.
Atanu
2022 年 11 月 23 日
Jan
2022 年 11 月 23 日
This line from your question:
dataOnDateQuery = sprintf("SELECT subjectid, trialname, referencetime " + ...
"FROM live_table WHERE referencetime = '%s';",date);
does not occur in the attache M-file. Similar lines from the file:
dateQuery = sprintf("SELECT subjectid, trialname, referencetime " + ...
"FROM live_table WHERE id = %d;", id);
dataOnDateQuery = sprintf("SELECT subjectid, trialname, referencetime " + ...
"FROM live_table WHERE referencetime ILIKE '%s';",date);
It is confusing, if you post different versions of the code, because it is not clear, which one causes which problem.
dataOnDateQuery = "SELECT subjectid FROM live_table WHERE referencetime ILIKE '04/06/2022%';";
Are you sure, that there is a % in the date?
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で PostgreSQL Native Interface についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!