%% Customize Options When Importing Logical Data
% Customize import options when importing logical data from a database table. Control the import options by creating an SQLImportOptions object. Then, customize the import options for database columns that contain logical data. Import data using the sqlread function.
% This example uses the airlinesmall_subset.xls spreadsheet, which contains the column Cancelled. Also, the example uses a Microsoft® SQL Server® Version 11.00.2100 database and the Microsoft SQL Server Driver 11.00.5058.
%
% Create a database connection to a Microsoft SQL Server database with Windows® authentication. Specify a blank user name and password.

datasource = 'MS SQL Server Auth';
conn = database(datasource,'','');

%
% Load flight information in the MATLAB® workspace.

flights = readtable('airlinesmall_subset.xlsx');

% Create the flights database table using the flight information.

tablename = 'flights';
sqlwrite(conn,tablename,flights)

%
% Create an SQLImportOptions object using the flights database table and the databaseImportOptions function.

opts = databaseImportOptions(conn,'flights');

%
% Retrieve the default import options for the Cancelled variable.

varnames = 'Cancelled';
varOpts = getoptions(opts,varnames)

%
% Set the import options for the data type of the specified variable to logical. Also, set the import options to replace missing data in the specified variable with the fill value true.

opts = setoptions(opts,varnames,'Type','logical', ...
    'FillValue',true);

%
% Import the logical data in the selected variable and display a summary of the data. The imported data shows that the variable has the logical data type.

opts.SelectedVariableNames = varnames;
T = sqlread(conn,tablename,opts);
summary(T)

%
% Delete the flights database table using the execute function.

sqlquery = 'DROP TABLE flights';
execute(conn,sqlquery)

% Close the database connection.

close(conn)