# OData

### Statements <a href="#default" id="default"></a>

A SELECT statement can consist of the following basic clauses.

* SELECT
* INTO
* FROM
* JOIN
* WHERE
* GROUP BY
* HAVING
* UNION
* ORDER BY
* LIMIT

SELECT Syntax

The following syntax diagram outlines the syntax supported by the SQL engine of the provider:<br>

\| <p><code>SELECT</code> <code>{</code></p><p>  <code>\[ TOP</code> <code>\<numeric\_literal> | DISTINCT</code> <code>]</code></p><p>  <code>{</code></p><p>    <code>*</code></p><p>    <code>| {</code></p><p>        <code>\<expression> \[ \[ AS</code> <code>] \<column\_reference> ]</code></p><p>        <code>| { \<table\_name> | \<correlation\_name> } .*</code></p><p>      <code>} \[ , ... ]</code></p><p>  <code>}</code></p><p>  <code>\[ INTO</code> <code>csv:// \[ filename= ] \<file\_path> \[ ;delimiter=tab ] ]</code></p><p>  <code>{</code></p><p>    <code>FROM</code> <code>\<table\_reference> \[ \[ AS</code> <code>] \<identifier> ]</code></p><p>  <code>} \[ , ... ]</code></p><p>  <code>\[ \[</code> </p><p>      <code>INNER</code> <code>| { { LEFT</code> <code>| RIGHT</code> <code>| FULL</code> <code>} \[ OUTER</code> <code>] }</code></p><p>    <code>] JOIN</code> <code>\<table\_reference> \[ ON</code> <code>\<search\_condition> ] \[ \[ AS</code> <code>] \<identifier> ]</code></p><p>  <code>] \[ ... ]</code></p><p>  <code>\[ WHERE</code> <code>\<search\_condition> ]</code></p><p>  <code>\[ GROUP</code> <code>BY</code> <code>\<column\_reference> \[ , ... ]</code></p><p>  <code>\[ HAVING</code> <code>\<search\_condition> ]</code></p><p>  <code>\[ UNION</code> <code>\[ ALL</code> <code>] \<select\_statement> ]</code></p><p>  <code>\[</code></p><p>    <code>ORDER</code> <code>BY</code></p><p>    <code>\<column\_reference> \[ ASC</code> <code>| DESC</code> <code>] \[ NULLS FIRST</code> <code>| NULLS LAST</code> <code>]</code></p><p>  <code>]</code></p><p>  <code>\[</code></p><p>    <code>LIMIT \<expression></code></p><p>    <code>\[</code></p><p>      <code>{ OFFSET | , }</code></p><p>      <code>\<expression></code></p><p>    <code>]</code></p><p>  <code>]</code></p><p><code>} | SCOPE\_IDENTITY()</code></p><p> </p><p><code>\<expression> ::=</code></p><p>  <code>| \<column\_reference></code></p><p>  <code>| @ \<parameter></code></p><p>  <code>| ?</code></p><p>  <code>| COUNT( \* | { \[ DISTINCT</code> <code>] \<expression> } )</code></p><p>  <code>| { AVG</code> <code>| MAX</code> <code>| MIN</code> <code>| SUM</code> <code>| COUNT</code> <code>} ( \<expression> )</code></p><p>  <code>| NULLIF</code> <code>( \<expression> , \<expression> )</code></p><p>  <code>| COALESCE</code> <code>( \<expression> , ... )</code></p><p>  <code>| CASE</code> <code>\<expression></code></p><p>      <code>WHEN</code> <code>{ \<expression> | \<search\_condition> } THEN</code> <code>{ \<expression> | NULL</code> <code>} \[ ... ]</code></p><p>    <code>\[ ELSE</code> <code>{ \<expression> | NULL</code> <code>} ]</code></p><p>    <code>END</code></p><p>  <code>| \<literal></code></p><p>  <code>| \<sql\_function></code></p><p> </p><p><code>\<search\_condition> ::=</code></p><p>  <code>{</code></p><p>    <code>\<expression> { = | > | < | >= | <= | <> | != | LIKE</code> <code>| NOT</code> <code>LIKE</code> <code>| IN</code> <code>| NOT</code> <code>IN</code> <code>| IS</code> <code>NULL</code> <code>| IS</code> <code>NOT</code> <code>NULL</code> <code>| AND</code> <code>| OR</code> <code>| CONTAINS</code> <code>| BETWEEN</code> <code>} \[ \<expression> ]</code></p><p>  <code>} \[ { AND</code> <code>| OR</code> <code>} ... ]</code></p> |
\| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

#### Examples

1. Return all columns:<br>

   | `SELECT * FROM Lead` |
   | -------------------- |
2. Rename a column:<br>

   | `SELECT [FullName] AS MY_FullName FROM Lead` |
   | -------------------------------------------- |
3. Cast a column's data as a different data type:<br>

   | `SELECT CAST(AnnualRevenue AS VARCHAR) AS Str_AnnualRevenue FROM Lead` |
   | ---------------------------------------------------------------------- |
4. Search data:<br>

   | `SELECT * FROM Lead WHERE FirstName <> 'Bartholomew'` |
   | ----------------------------------------------------- |
5. Return the number of items matching the query criteria:<br>

   | `SELECT COUNT(*) AS MyCount FROM Lead` |
   | -------------------------------------- |
6. Return the number of unique items matching the query criteria:<br>

   | `SELECT COUNT(DISTINCT FullName) FROM Lead` |
   | ------------------------------------------- |
7. Return the unique items matching the query criteria:<br>

   | `SELECT DISTINCT FullName FROM Lead` |
   | ------------------------------------ |
8. Summarize data:<br>

   | `SELECT FullName, MAX(AnnualRevenue) FROM Lead GROUP BY FullName` |
   | ----------------------------------------------------------------- |

   See Aggregate Functions below for details.
9. Retrieve data from multiple tables.<br>

   | `SELECT Customers.ContactName, Orders.OrderDate FROM Customers, Orders WHERE Customers.CustomerId=Orders.CustomerId` |
   | -------------------------------------------------------------------------------------------------------------------- |

   See JOIN Queries below for details.
10. Sort a result set in ascending order:<br>

    | `SELECT Id, FullName FROM Lead  ORDER BY FullName ASC` |
    | ------------------------------------------------------ |
11. Restrict a result set to the specified number of rows:<br>

    | `SELECT Id, FullName FROM Lead LIMIT 10` |
    | ---------------------------------------- |
12. Parameterize a query to pass in inputs at execution time. This enables you to create prepared statements and mitigate SQL injection attacks.<br>

    | `SELECT * FROM Lead WHERE FirstName = @param` |
    | --------------------------------------------- |

### JOIN Queries <a href="#default" id="default"></a>

The CData ADO.NET Provider for OData supports standard SQL joins like the following examples.

#### Inner Join

An inner join selects only rows from both tables that match the join condition:<br>

| `SELECT Customers.ContactName, Orders.OrderDate FROM Customers, Orders WHERE Customers.CustomerId=Orders.CustomerId` |
| -------------------------------------------------------------------------------------------------------------------- |

#### Left Join

A left join selects all rows in the FROM table and only matching rows in the JOIN table:<br>

| `SELECT Customers.ContactName, Orders.OrderDate FROM Customers LEFT OUTER JOIN Orders ON Customers.CustomerId=Orders.CustomerId` |
| -------------------------------------------------------------------------------------------------------------------------------- |

### Date Literal Functions <a href="#default" id="default"></a>

The following date literal functions can be used to filter date fields using relative intervals. Note that while the <, >, and = operators are supported for these functions, <= and >= are not.

#### L\_TODAY() <a href="#ltoday" id="ltoday"></a>

The current day.<br>

| `SELECT * FROM MyTable WHERE MyDateField = L_TODAY()` |
| ----------------------------------------------------- |

#### L\_YESTERDAY() <a href="#lyesterday" id="lyesterday"></a>

The previous day.<br>

| `SELECT * FROM MyTable WHERE MyDateField = L_YESTERDAY()` |
| --------------------------------------------------------- |

#### L\_TOMORROW() <a href="#ltomorrow" id="ltomorrow"></a>

The following day.<br>

| `SELECT * FROM MyTable WHERE MyDateField = L_TOMORROW()` |
| -------------------------------------------------------- |

#### L\_LAST\_WEEK() <a href="#llastweek" id="llastweek"></a>

Every day in the preceding week.<br>

| `SELECT * FROM MyTable WHERE MyDateField = L_LAST_WEEK()` |
| --------------------------------------------------------- |

#### L\_THIS\_WEEK() <a href="#lthisweek" id="lthisweek"></a>

Every day in the current week.<br>

| `SELECT * FROM MyTable WHERE MyDateField = L_THIS_WEEK()` |
| --------------------------------------------------------- |

#### L\_NEXT\_WEEK() <a href="#lnextweek" id="lnextweek"></a>

Every day in the following week.<br>

| `SELECT * FROM MyTable WHERE MyDateField = L_NEXT_WEEK()` |
| --------------------------------------------------------- |

Also available:

* L\_LAST/L\_THIS/L\_NEXT MONTH
* L\_LAST/L\_THIS/L\_NEXT QUARTER
* L\_LAST/L\_THIS/L\_NEXT YEAR

#### L\_LAST\_N\_DAYS(n) <a href="#llastndaysn" id="llastndaysn"></a>

The previous n days, excluding the current day.<br>

| `SELECT * FROM MyTable WHERE MyDateField = L_LAST_N_DAYS(3)` |
| ------------------------------------------------------------ |

#### L\_NEXT\_N\_DAYS(n) <a href="#lnextndaysn" id="lnextndaysn"></a>

The following n days, including the current day.<br>

| `SELECT * FROM MyTable WHERE MyDateField = L_NEXT_N_DAYS(3)` |
| ------------------------------------------------------------ |

Also available:

* L\_LAST/L\_NEXT\_90\_DAYS

#### L\_LAST\_N\_WEEKS(n) <a href="#llastnweeksn" id="llastnweeksn"></a>

Every day in every week, starting n weeks before current week, and ending in the previous week.<br>

| `SELECT * FROM MyTable WHERE MyDateField = L_LAST_N_WEEKS(3)` |
| ------------------------------------------------------------- |

#### L\_NEXT\_N\_WEEKS(n) <a href="#lnextnweeksn" id="lnextnweeksn"></a>

Every day in every week, starting the following week, and ending n weeks in the future.<br>

| `SELECT * FROM MyTable WHERE MyDateField = L_NEXT_N_WEEKS(3)` |
| ------------------------------------------------------------- |

Also available:

* L\_LAST/L\_NEXT\_N\_MONTHS(n)
* L\_LAST/L\_NEXT\_N\_QUARTERS(n)
* L\_LAST/L\_NEXT\_N\_YEARS(n)

### Predicate Functions <a href="#default" id="default"></a>

#### CEILING(value) <a href="#ceilingvalue" id="ceilingvalue"></a>

Returns the value rounded up to the nearest whole number (no decimal component).

* expression: The value to round.

#### CONCAT(string\_expr1, string\_expr2) <a href="#concatstringexpr1-stringexpr2" id="concatstringexpr1-stringexpr2"></a>

Returns the string that is the concatenation of string\_expr1 and string\_expr2.

* string\_expr1: The first string to be concatenated.
* string\_expr2: The second string to be concatenated.

#### CONTAINS(string\_expression, string\_search) <a href="#containsstringexpression-stringsearch" id="containsstringexpression-stringsearch"></a>

Returns true if string\_expression contains string\_expression, otherwise returns false.

* string\_expression: The string expression to search within.
* string\_search: The value to search for.

#### DATE(datetime\_offset) <a href="#datedatetimeoffset" id="datedatetimeoffset"></a>

Returns the current date using the specified datetime\_offset.

* datetime\_offset: The datetime offset to use when retrieving the current date.

#### DAY(datetime\_date) <a href="#daydatetimedate" id="daydatetimedate"></a>

Returns the integer that specifies the day component of the specified date.

* datetime\_date: The datetime string that specifies the date.

#### ENDSWITH(string\_expression, string\_suffix) <a href="#endswithstringexpression-stringsuffix" id="endswithstringexpression-stringsuffix"></a>

Returns true if string\_expression ends with string\_suffix, otherwise returns false.

* string\_expression: The string expression to search within.
* string\_suffix: The string suffix to search for.

#### FLOOR(value) <a href="#floorvalue" id="floorvalue"></a>

Returns the value rounded down to the nearest whole number (no decimal component).

* value: The value to round.

#### FRACTIONALSECONDS(datetime\_time) <a href="#fractionalsecondsdatetimetime" id="fractionalsecondsdatetimetime"></a>

Returns the decimal value that specifies the fractional seconds component of the specified time.

* datetime\_time: The datetime string that specifies the time.

#### HOUR(datetime\_time) <a href="#hourdatetimetime" id="hourdatetimetime"></a>

Returns the integer that specifies the hour component of the specified time.

* datetime\_time: The datetime string that specifies the time.

#### INDEXOF(string\_expression, string\_search) <a href="#indexofstringexpression-stringsearch" id="indexofstringexpression-stringsearch"></a>

Returns the index location where string\_search is contained within string\_expression.

* string\_expression: The string expression to search within.
* string\_search: The search value to locate within string\_expression.

#### ISOF(string\_expression, string\_type) <a href="#isofstringexpression-stringtype" id="isofstringexpression-stringtype"></a>

Returns true if the string\_expression is assignable to type string\_type, otherwise returns false.

* string\_expression: The string expression to check the type of.
* string\_type: The name of the type.

#### LENGTH(string\_expression) <a href="#lengthstringexpression" id="lengthstringexpression"></a>

Returns the number of characters of the specified string expression.

* string\_expression: The string expression.

#### MAXDATETIME() <a href="#maxdatetime" id="maxdatetime"></a>

Returns the latest possible datetime.

#### MINDATETIME() <a href="#mindatetime" id="mindatetime"></a>

Returns the earliest possible datetime.

#### MINUTE(datetime\_time) <a href="#minutedatetimetime" id="minutedatetimetime"></a>

Returns the integer that specifies the minute component of the specified time.

* datetime\_time: The datetime string that specifies the time.

#### MONTH(datetime\_date) <a href="#monthdatetimedate" id="monthdatetimedate"></a>

Returns the integer that specifies the month component of the specified date.

* datetime\_date: The datetime string that specifies the date.

#### NOW() <a href="#now" id="now"></a>

Returns the current datetime.

#### REPLACE(string\_expression, string\_search, string\_replace) <a href="#replacestringexpression-stringsearch-stringreplace" id="replacestringexpression-stringsearch-stringreplace"></a>

Returns the string after replacing any found string\_search values with string\_replace.

* string\_expression: The string expression to perform a replace on.
* string\_search: The string value to find within string\_expression.
* string\_replace: The string value replace and string\_search instances found.

#### ROUND(value) <a href="#roundvalue" id="roundvalue"></a>

Returns the value to the nearest whole number (no decimal component).

* value: The value to round.

#### SECOND(datetime\_time) <a href="#seconddatetimetime" id="seconddatetimetime"></a>

Returns the integer that specifies the second component of the specified time.

* datetime\_time: The datetime string that specifies the time.

#### STARTSWITH(string\_expression, string\_prefix) <a href="#startswithstringexpression-stringprefix" id="startswithstringexpression-stringprefix"></a>

Returns true if string\_expression starts with string\_prefix, otherwise returns false.

* string\_expression: The string expression to search within.
* string\_prefix: The string prefix to search for.

#### SUBSTRING(string\_expression, integer\_start \[,integer\_length]) <a href="#substringstringexpression-integerstart-integerlength" id="substringstringexpression-integerstart-integerlength"></a>

Returns the part of the string with the specified length; starts at the specified index.

* expression: The character string.
* start: The positive integer that specifies the start index of characters to return.
* length: The positive integer that specifies how many characters will be returned.

#### SUBSTRINGOF(string\_expression, string\_search) <a href="#substringofstringexpression-stringsearch" id="substringofstringexpression-stringsearch"></a>

Returns true if string\_expression contains string\_expression, otherwise returns false.

* string\_expression: The string expression to search within.
* string\_search: The value to search for.

#### TIME(datetime\_offset) <a href="#timedatetimeoffset" id="timedatetimeoffset"></a>

Returns the current time using datetime\_offset.

* datetime\_offset: The datetime offset.

#### TOLOWER(string\_expression) <a href="#tolowerstringexpression" id="tolowerstringexpression"></a>

Returns the string\_expression with the uppercase character data converted to lowercase.

* string\_expression: The string expression to lowercase.

#### TOTALOFFSETMINUTES(datetime\_date) <a href="#totaloffsetminutesdatetimedate" id="totaloffsetminutesdatetimedate"></a>

Returns the integer that specifies the offset minutes component of the specified date.

* datetime\_date: The datetime string that specifies the date.

#### TOTALSECONDS(duration) <a href="#totalsecondsduration" id="totalsecondsduration"></a>

Returns the duration value in total seconds.

* string\_duration: The duration.

#### TOUPPER(string\_expression) <a href="#toupperstringexpression" id="toupperstringexpression"></a>

Returns the string\_expression with the lowercase character data converted to uppercase.

* string\_expression: The string expression to uppercase.

#### TRIM(string\_expression) <a href="#trimstringexpression" id="trimstringexpression"></a>

Returns the string\_expression with the leading and trailing whitespace removed.

* string\_expression: The string expression to trim.

#### YEAR(datetime\_date) <a href="#yeardatetimedate" id="yeardatetimedate"></a>

Returns the integer that specifies the year component of the specified date.

* datetime\_date: The datetime string that specifies the date.
