Importing data from Microsoft Access w/ SQL query
2 views (last 30 days)
Show older comments
I have the database toolbox and have had no issues exporting data from MATLAB into Access using the ODBC connection process. However, it's taken me awhile to figure out how to import data into MATLAB from my Access database using SQL queries in MATLAB. I'm trying to understand why this code doesn't work (I don't have the connection code below but that's not the issue...MATLAB establishes the connection just fine):
A='MKT';
curs = exec(conn,['select Period,Ticker from MonthlyReturns where Ticker = ',A]);
curs = fetch(curs)
While this code does:
A='MKT';
curs = exec(conn, ['select Period,Return from MonthlyReturns where Ticker = ''', A, ''''])
curs = fetch(curs)
Just not clear to me exactly why all the additional apostrophes are needed.
Thanks,
JK
0 Comments
Accepted Answer
Geoff Hayes
on 29 Nov 2014
Jared - look at the two queries (as strings)
>> A = 'MKT';
>> query1 = ['select Period,Ticker from MonthlyReturns where Ticker = ',A];
>> query2 = ['select Period,Return from MonthlyReturns where Ticker = ''', A, ''''];
query1 =
select Period,Ticker from MonthlyReturns where Ticker = MKT
query2 =
select Period,Return from MonthlyReturns where Ticker = 'MKT'
Your Ticker field data type is char/string, so you need to wrap the condition for it (in your where clause) in single quotes. This is just usual SQL syntax (and is not MATLAB related).
More Answers (0)
See Also
Categories
Find more on Database Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!