How do I generate an SQL query that contains dates with Database Toolbox?
1 次查看(过去 30 天)
显示 更早的评论
I would like to include dates in my SQL query. For example, I might want to select items from a table where the date in a column is later than a certain date.
采纳的回答
MathWorks Support Team
2009-6-27
Different databases require you to specify dates in different formats. Some databases accept dates in a standard string format ('19-Jan-2002') while others require special characters in the string to identify the string as a date.
In general, consult your database documentation for the proper format for dates in SQL queries. Below is some specific information for a couple of databases:
Microsoft Access:
With Microsoft Access, use a # character before and after the date string. Below is an example, where "conn" is a database object in MATLAB as returned by the DATABASE command. The table name is "myDates", and the column name is "DateJoined". In order to select all records where the DateJoined field is after 3/5/05, use the following:
curs = exec(conn,'select * from myDates where DateJoined > #03/05/2005#');
d = fetch(curs);
data = d.Data
Oracle:
With Oracle databases, the date string must be preceded by a data type identifier and enclosed in brackets. Below is an example, where "conn" is a database object in MATLAB as returned by the DATABASE command. The table name is "myDates", and the column name is "DateJoined". In order to select all records where the DateJoined field is after 3/5/05, use one of the following examples. The first statement uses the TIMESTAMP data type:
curs = exec(conn,'select * from myDates where DateJoined > {ts ''2005-03-05 00:00:00''}');
d = fetch(curs);
data = d.Data
The second statement uses the DATE data type:
curs = exec(conn,'select * from myDates where DateJoined > {d ''2005-03-05''}');
d = fetch(curs);
data = d.Data
0 个评论
更多回答(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!