Skip to content

Commit

Permalink
Document SQLite function mappings
Browse files Browse the repository at this point in the history
Part of #504
  • Loading branch information
bricelam committed Oct 6, 2020
1 parent 3f2f4cc commit 5e7b898
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
103 changes: 103 additions & 0 deletions entity-framework/core/providers/sqlite/functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
title: Function Mappings - SQLite Database Provider - EF Core
description: Function Mappings of the SQLite EF Core database provider
author: bricelam
ms.date: 10/05/2020
uid: core/providers/sqlite/built-in-functions
---
# Function Mappings of the SQLite EF Core Provider

This table shows which .NET members are translated into which SQL functions when using the SQLite provider.

## Binary functions

.NET | SQL | Added in
--------------------- | ------------------------------- | --------
bytes.Contains(value) | instr(@bytes, char(@value)) > 0 | EF Core 5.0
bytes.Length | length(@bytes) | EF Core 5.0

## Date and time functions

.NET | SQL | Added in
------------------------------- | ------------------------------------------------------------------------ | --------
DateTime.Now | datetime('now', 'localtime')
DateTime.Today | datetime('now', 'localtime', 'start of day')
DateTime.UtcNow | datetime('now')
dateTime.AddDays(value) | datetime(@dateTime, @value \|\| ' days')
dateTime.AddHours(value) | datetime(@dateTime, @d \|\| ' hours')
dateTime.AddMilliseconds(value) | datetime(@dateTime, (@value / 1000.0) \|\| ' seconds') | EF Core 2.2
dateTime.AddMinutes(value) | datetime(@dateTime, @value \|\| ' minutes')
dateTime.AddMonths(months) | datetime(@dateTime, @months \|\| ' months')
dateTime.AddSeconds(value) | datetime(@dateTime, @value \|\| ' seconds')
dateTime.AddTicks(value) | datetime(@dateTime, (@value / 10000000.0) \|\| ' seconds') | EF Core 2.2
dateTime.AddYears(value) | datetime(@dateTime, @value \|\| ' years')
dateTime.Date | datetime(@dateTime, 'start of day')
dateTime.Day | strftime('%d', @dateTime)
dateTime.DayOfWeek | strftime('%w', @dateTime) | EF Core 2.2
dateTime.DayOfYear | strftime('%j', @dateTime)
dateTime.Hour | strftime('%H', @dateTime)
dateTime.Millisecond | (strftime('%f', @dateTime) * 1000) % 1000
dateTime.Minute | strftime('%M', @dateTime)
dateTime.Month | strftime('%m', @dateTime)
dateTime.Second | strftime('%S', @dateTime)
dateTime.Ticks | (julianday(@dateTime) - julianday('0001-01-01 00:00:00')) * 864000000000 | EF Core 2.2
dateTime.TimeOfDay | time(@dateTime) | EF Core 3.0
dateTime.Year | strftime('%Y', @dateTime)

> [!NOTE]
> Some SQL has been simplified for illustration purposes. The actual SQL is more complex to handle a wider range of values.
## Numeric functions

.NET | SQL | Added in
--------------------- | ------------------------------------ | --------
-decimalValue | ef_negate(@decimalValue) | EF Core 5.0
decimalValue - d | ef_add(@decimalValue, ef_negate(@d)) | EF Core 5.0
decimalValue * d | ef_multiply(@decimalValue, @d) | EF Core 5.0
decimalValue / d | ef_divide(@decimalValue, @d) | EF Core 5.0
decimalValue % d | ef_mod(@decimalValue, @d) | EF Core 5.0
decimalValue + d | ef_add(@decimalValue, @d) | EF Core 5.0
decimalValue < d | ef_compare(@decimalValue, @d) < 0 | EF Core 5.0
decimalValue <= d | ef_compare(@decimalValue, @d) <= 0 | EF Core 5.0
decimalValue > d | ef_compare(@decimalValue, @d) > 0 | EF Core 5.0
decimalValue >= d | ef_compare(@decimalValue, @d) >= 0 | EF Core 5.0
doubleValue % d | ef_mod(@doubleValue, @d) | EF Core 5.0
floatValue % d | ef_mod(@floatValue, @d) | EF Core 5.0
Math.Abs(value) | abs(@value)
Math.Max(val1, val2) | max(@val1, @val2)
Math.Min(val1, val2) | min(@val1, @val2)
Math.Round(d) | round(@d)
Math.Round(d, digits) | round(@d, @digits)

> [!TIP]
> SQL functions prefixed with *ef* are created by EF Core.
## String functions

.NET | SQL | Added in
----------------------------------------- | ---------------------------------------------- | --------
string.IsNullOrWhiteSpace(value) | @value IS NULL OR trim(@value) = ''
stringValue.Contains(value) | instr(@stringValue, @value) > 0
stringValue.EndsWith(value) | @stringValue LIKE '%' \|\| @value
stringValue.FirstOrDefault() | substr(@stringValue, 1, 1) | EF Core 5.0
stringValue.IndexOf(value) | instr(@stringValue, @value) - 1
stringValue.LastOrDefault() | substr(@stringValue, length(@stringValue), 1) | EF Core 5.0
stringValue.Length | length(@stringValue)
stringValue.Replace(oldValue, newValue) | replace(@stringValue, @oldValue, @newValue)
stringValue.StartsWith(value) | @stringValue LIKE @value \|\| '%'
stringValue.Substring(startIndex, length) | substr(@stringValue, @startIndex + 1, @length)
stringValue.ToLower() | lower(@stringValue)
stringValue.ToUpper() | upper(@stringValue)
stringValue.Trim() | trim(@stringValue)
stringValue.Trim(trimChar) | trim(@stringValue, @trimChar)
stringValue.TrimEnd() | rtrim(@stringValue)
stringValue.TrimEnd(trimChar) | rtrim(@stringValue, @trimChar)
stringValue.TrimStart() | ltrim(@stringValue)
stringValue.TrimStart(trimChar) | ltrim(@stringValue, @trimChar)

> [!NOTE]
> Some SQL has been simplified for illustration purposes. The actual SQL is more complex to handle a wider range of values.
## See also

* [Spatial Function Mappings](xref:core/providers/sqlite/spatial#spatial-function-mappings)
2 changes: 1 addition & 1 deletion entity-framework/core/providers/sqlite/spatial.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ modelBuilder.Entity<City>().Property(c => c.Location)
.HasColumnType("POINTZ");
```

## Translated Operations
## Spatial Function Mappings

This table shows which [NetTopologySuite](https://nettopologysuite.github.io/NetTopologySuite/) (NTS) members are translated into which SQL functions.

Expand Down
2 changes: 2 additions & 0 deletions entity-framework/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@
href: core/providers/sqlite/index.md
- name: SQLite limitations
href: core/providers/sqlite/limitations.md
- name: Built-in Functions
href: core/providers/sqlite/built-in-functions.md
- name: Spatial data
displayName: GIS
href: core/providers/sqlite/spatial.md
Expand Down

0 comments on commit 5e7b898

Please sign in to comment.