query data filtered by dynamic dates?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (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!