query data filtered by dynamic dates?

1 次查看(过去 30 天)
Vanessa
Vanessa 2018-6-28
Hello everyone,
I have created a query to select certain columns from a database table and among them is a date column (format yyyy-mm-dd HH:MM:SS). I want to query only the data of the last two days. How can I achieve this?
My query is like below in the where clause but it doesn't work. I'm sure that the error stays at the quotes.
%Set preferences with setdbprefs.
setdbprefs('DataReturnFormat', 'dataset');
setdbprefs('NullNumberRead', 'NaN');
setdbprefs('NullStringRead', 'null');
%Make connection to database. Note that the password has been omitted.
%Using ODBC driver.
conn = database('', '', '');
%Read data from database.
curs = exec(conn, ['SELECT "Flat"."SPEED_LOG"'...
' , "Flat"."ME_OUTPUT"'...
' , "Flat"."ME_REVOLUTION"'...
' , "Flat"."ME_FUEL_OIL_CONSUMPTION"'...
' , "Flat"."TIMESTAMP_UTC"'...
' , "Flat"."VESSEL_UNIQUEID"'...
' , "Flat"."GPS_LAT"'...
' , "Flat"."GPS_LON"'...
' , "Flat"."ME_FO_FLOW"'...
' , "Flat"."GE_FO_FLOW"'...
' , "Flat"."COMP_BLR_FUEL_OIL_FLOW"'...
' , "Flat"."AUX_BLR_FUEL_OIL_FLOW"'...
' , "Flat"."ME_FUEL_INDEX"'...
' , "Flat"."ME_SCAV_AIR_PRESS"'...
' , "Flat"."ME_LO_TOTALIZER"'...
' , "Flat"."DG_1_ELECTRIC_POWER"'...
' , "Flat"."DG_2_ELECTRIC_POWER"'...
' , "Flat"."DG_3_ELECTRIC_POWER"'...
' FROM "setel_data"."public"."Flat" '...
' WHERE "Flat"."TIMESTAMP_UTC" >= ''datetime('now','Format','yyyy-MM-dd')-2'' AND ("VESSEL_UNIQUEID"=''2537'' OR "VESSEL_UNIQUEID"=''5537'')']);
curs = fetch(curs);
close(curs);
%Assign data to output variable
Setel = curs.Data;
%Close database connection.
close(conn);
%Clear variables
clear curs conn
Thanks

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Database Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by