Skip to content

Commit

Permalink
Document SQL Server function mappings
Browse files Browse the repository at this point in the history
Part of #504
  • Loading branch information
bricelam committed Oct 8, 2020
1 parent c1e9532 commit d0af197
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 25 deletions.
187 changes: 187 additions & 0 deletions entity-framework/core/providers/sql-server/functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
---
title: Function Mappings - Microsoft SQL Server Database Provider - EF Core
description: Function Mappings of the Microsoft SQL Server database provider
author: bricelam
ms.date: 10/07/2020
uid: core/providers/sql-server/functions
---
# Function Mappings of the Microsoft SQL Server Provider

This page shows which .NET members are translated into which SQL functions when using the SQL Server provider.

## Binary functions

.NET | SQL | Added in
---------------------------- | ----------------------------- | --------
bytes.Contains(value) | CHARINDEX(@value, @bytes) > 0 | EF Core 5.0
bytes.Length | DATALENGTH(@bytes) | EF Core 5.0
bytes.SequenceEqual(second) | @bytes = @second | EF Core 5.0
EF.Functions.DataLength(arg) | DATALENGTH(@arg) | EF Core 5.0

## Conversion functions

.NET | SQL | Added in
------------------------- | -------------------------------------- | --------
bytes.ToString() | CONVERT(VARCHAR(100), @bytes)
byteValue.ToString() | CONVERT(VARCHAR(3), @byteValue)
charValue.ToString() | CONVERT(VARCHAR(1), @charValue)
Convert.ToBoolean(value) | CONVERT(bit, @value) | EF Core 5.0
Convert.ToByte(value) | CONVERT(tinyint, @value)
Convert.ToDecimal(value) | CONVERT(decimal(18, 2), @value)
Convert.ToDouble(value) | CONVERT(float, @value)
Convert.ToInt16(value) | CONVERT(smallint, @value)
Convert.ToInt32(value) | CONVERT(int, @value)
Convert.ToInt64(value) | CONVERT(bigint, @value)
Convert.ToString(value) | CONVERT(nvarchar(max), @value)
dateTime.ToString() | CONVERT(VARCHAR(100), @dateTime)
dateTimeOffset.ToString() | CONVERT(VARCHAR(100), @dateTimeOffset)
decimalValue.ToString() | CONVERT(VARCHAR(100), @decimalValue)
doubleValue.ToString() | CONVERT(VARCHAR(100), @doubleValue)
floatValue.ToString() | CONVERT(VARCHAR(100), @floatValue)
guid.ToString() | CONVERT(VARCHAR(36), @guid)
intValue.ToString() | CONVERT(VARCHAR(11), @intValue)
longValue.ToString() | CONVERT(VARCHAR(20), @longValue)
sbyteValue.ToString() | CONVERT(VARCHAR(4), @sbyteValue)
shortValue.ToString() | CONVERT(VARCHAR(6), @shortValue)
timeSpan.ToString() | CONVERT(VARCHAR(100), @timeSpan)
uintValue.ToString() | CONVERT(VARCHAR(10), @uintValue)
ulongValue.ToString() | CONVERT(VARCHAR(19), @ulongValue)
ushortValue.ToString() | CONVERT(VARCHAR(5), @ushortValue)

## Date and time functions

.NET | SQL | Added in
----------------------------------------------------------- | ---------------------------------------------------- | --------
DateTime.Now | GETDATE()
DateTime.Today | CONVERT(date, GETDATE())
DateTime.UtcNow | GETUTCDATE()
dateTime.AddDays(value) | DATEADD(day, @value, @dateTime)
dateTime.AddHours(value) | DATEADD(hour, @value, @dateTime)
dateTime.AddMilliseconds(value) | DATEADD(millisecond, @value, @dateTime)
dateTime.AddMinutes(value) | DATEADD(minute, @value, @dateTime)
dateTime.AddMonths(months) | DATEADD(month, @months, @dateTime)
dateTime.AddSeconds(value) | DATEADD(second, @value, @dateTime)
dateTime.AddYears(value) | DATEADD(year, @value, @dateTime)
dateTime.Date | CONVERT(date, @dateTime)
dateTime.Day | DATEPART(day, @dateTime)
dateTime.DayOfYear | DATEPART(dayofyear, @dateTime)
dateTime.Hour | DATEPART(hour, @dateTime)
dateTime.Millisecond | DATEPART(millisecond, @dateTime)
dateTime.Minute | DATEPART(minute, @dateTime)
dateTime.Month | DATEPART(month, @dateTime)
dateTime.Second | DATEPART(second, @dateTime)
dateTime.TimeOfDay | CAST(@dateTime AS time) | EF Core 2.2
dateTime.Year | DATEPART(year, @dateTime)
DateTimeOffset.Now | SYSDATETIMEOFFSET()
DateTimeOffset.UtcNow | SYSUTCDATETIME()
dateTimeOffset.AddDays(days) | DATEADD(day, @days, @dateTimeOffset)
dateTimeOffset.AddHours(hours) | DATEADD(hour, @hours, @dateTimeOffset)
dateTimeOffset.AddMilliseconds(milliseconds) | DATEADD(millisecond, @milliseconds, @dateTimeOffset)
dateTimeOffset.AddMinutes(minutes) | DATEADD(minute, @minutes, @dateTimeOffset)
dateTimeOffset.AddMonths(months) | DATEADD(month, @months, @dateTimeOffset)
dateTimeOffset.AddSeconds(seconds) | DATEADD(second, @seconds, @dateTimeOffset)
dateTimeOffset.AddYears(years) | DATEADD(year, @years, @dateTimeOffset)
dateTimeOffset.Date | CONVERT(date, @dateTimeOffset)
dateTimeOffset.Day | DATEPART(day, @dateTimeOffset)
dateTimeOffset.DayOfYear | DATEPART(dayofyear, @dateTimeOffset)
dateTimeOffset.Hour | DATEPART(hour, @dateTimeOffset)
dateTimeOffset.Millisecond | DATEPART(millisecond, @dateTimeOffset)
dateTimeOffset.Minute | DATEPART(minute, @dateTimeOffset)
dateTimeOffset.Month | DATEPART(month, @dateTimeOffset)
dateTimeOffset.Second | DATEPART(second, @dateTimeOffset)
dateTimeOffset.TimeOfDay | CAST(@dateTimeOffset AS time) | EF Core 2.2
dateTimeOffset.Year | DATEPART(year, @dateTimeOffset)
EF.Functions.DateDiffDay(start, end) | DATEDIFF(DAY, @start, @end)
EF.Functions.DateDiffHour(start, end) | DATEDIFF(HOUR, @start, @end)
EF.Functions.DateDiffMicrosecond(start, end) | DATEDIFF(MICROSECOND, @start, @end)
EF.Functions.DateDiffMillisecond(start, end) | DATEDIFF(MILLISECOND, @start, @end)
EF.Functions.DateDiffMinute(start, end) | DATEDIFF(MINUTE, @start, @d2)
EF.Functions.DateDiffMonth(start, end) | DATEDIFF(MONTH, @start, @end)
EF.Functions.DateDiffNanosecond(start, end) | DATEDIFF(NANOSECOND, @start, @end)
EF.Functions.DateDiffSecond(start, end) | DATEDIFF(SECOND, @start, @end)
EF.Functions.DateDiffWeek(start, end) | DATEDIFF(WEEK, @start, @end) | EF Core 5.0
EF.Functions.DateDiffYear(start, end) | DATEDIFF(YEAR, @start, @end)
EF.Functions.DateFromParts(year, month, day) | DATEFROMPARTS(@year, @month, @day) | EF Core 5.0
EF.Functions.DateTime2FromParts(year, month, day, ...) | DATETIME2FROMPARTS(@year, @month, @day, ...) | EF Core 5.0
EF.Functions.DateTimeFromParts(year, month, day, ...) | DATETIMEFROMPARTS(@year, @month, @day, ...) | EF Core 5.0
EF.Functions.DateTimeOffsetFromParts(year, month, day, ...) | DATETIMEOFFSETFROMPARTS(@year, @month, @day, ...) | EF Core 5.0
EF.Functions.IsDate(expression) | ISDATE(@expression) | EF Core 3.0
EF.Functions.SmallDateTimeFromParts(year, month, day, ...) | SMALLDATETIMEFROMPARTS(@year, @month, @day, ...) | EF Core 5.0
EF.Functions.TimeFromParts(hour, minute, second, ...) | TIMEFROMPARTS(@hour, @minute, @second, ...) | EF Core 5.0
timeSpan.Hours | DATEPART(hour, @timeSpan) | EF Core 5.0
timeSpan.Milliseconds | DATEPART(millisecond, @timeSpan) | EF Core 5.0
timeSpan.Minutes | DATEPART(minute, @timeSpan) | EF Core 5.0
timeSpan.Seconds | DATEPART(second, @timeSpan) | EF Core 5.0

## Numeric functions

.NET | SQL
----------------------- | ---
Math.Abs(value) | ABS(@value)
Math.Acos(d) | ACOS(@d)
Math.Asin(d) | ASIN(@d)
Math.Atan(d) | ATAN(@d)
Math.Atan2(y, x) | ATN2(@y, @x)
Math.Ceiling(d) | CEILING(@d)
Math.Cos(d) | COS(@d)
Math.Exp(d) | EXP(@d)
Math.Floor(d) | FLOOR(@d)
Math.Log(d) | LOG(@d)
Math.Log(a, newBase) | LOG(@a, @newBase)
Math.Log10(d) | LOG10(@d)
Math.Pow(x, y) | POWER(@x, @y)
Math.Round(d) | ROUND(@d, 0)
Math.Round(d, decimals) | ROUND(@d, @decimals)
Math.Sign(value) | SIGN(@value)
Math.Sin(a) | SIN(@a)
Math.Sqrt(d) | SQRT(@d)
Math.Tan(a) | TAN(@a)
Math.Truncate(d) | ROUND(@d, 0, 1)

## String functions

.NET | SQL | Added in
----------------------------------------------------------------------- | ---------------------------------------------------------------------- | --------
EF.Functions.Collate(operand, collation) | @operand COLLATE @collation | EF Core 5.0
EF.Functions.Contains(propertyReference, searchCondition) | CONTAINS(@propertyReference, @searchCondition) | EF Core 2.2
EF.Functions.Contains(propertyReference, searchCondition, languageTerm) | CONTAINS(@propertyReference, @searchCondition, LANGUAGE @languageTerm) | EF Core 2.2
EF.Functions.FreeText(propertyReference, freeText) | FREETEXT(@propertyReference, @freeText)
EF.Functions.FreeText(propertyReference, freeText, languageTerm) | FREETEXT(@propertyReference, @freeText, LANGUAGE @languageTerm)
EF.Functions.Like(matchExpression, pattern) | @matchExpression LIKE @pattern
EF.Functions.Like(matchExpression, pattern, escapeCharacter) | @matchExpression LIKE @pattern ESCAPE @escapeCharacter
string.Compare(strA, strB) | CASE WHEN @strA = @strB THEN 0 ... END
string.Concat(str0, str1) | @str0 + @str1
string.IsNullOrEmpty(value) | @value IS NULL OR @value = N''
string.IsNullOrWhiteSpace(value) | @value IS NULL OR LTRIM(RTRIM(@value)) = N''
stringValue.CompareTo(strB) | CASE WHEN @stringValue = @strB THEN 0 ... END
stringValue.Contains(value) | @stringValue LIKE N'%' + @value + N'%'
stringValue.EndsWith(value) | @stringValue LIKE N'%' + @value
stringValue.FirstOrDefault() | SUBSTRING(@stringValue, 1, 1) | EF Core 5.0
stringValue.IndexOf(value) | CHARINDEX(@value, @stringValue) - 1
stringValue.LastOrDefault() | SUBSTRING(@stringValue, LEN(@stringValue), 1) | EF Core 5.0
stringValue.Length | LEN(@stringValue)
stringValue.Replace(@oldValue, @newValue) | REPLACE(@stringValue, @oldValue, @newValue)
stringValue.StartsWith(value) | @stringValue LIKE @value + N'%'
stringValue.Substring(startIndex, length) | SUBSTRING(@stringValue, @startIndex + 1, @length)
stringValue.ToLower() | LOWER(@stringValue)
stringValue.ToUpper() | UPPER(@stringValue)
stringValue.Trim() | LTRIM(RTRIM(@stringValue))
stringValue.TrimEnd() | RTRIM(@stringValue)
stringValue.TrimStart() | LTRIM(@stringValue)

## Miscellaneous functions

.NET | SQL | Added in
---------------------------------------- | ---------------------------------- | --------
collection.Contains(item) | @item IN @collection | EF Core 3.0
enumValue.HasFlag(flag) | @enumValue & @flag = @flag
Guid.NewGuid() | NEWID()
nullable.GetValueOrDefault() | COALESCE(@nullable, 0)
nullable.GetValueOrDefault(defaultValue) | COALESCE(@nullable, @defaultValue)

> [!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/sql-server/spatial#spatial-function-mappings)
2 changes: 1 addition & 1 deletion entity-framework/core/providers/sql-server/spatial.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ As mentioned in the main [Spatial Data](xref:core/modeling/spatial) documentatio
> [!WARNING]
> CircularString, CompoundCurve, and CurePolygon aren't supported by NTS.
## Translated Operations
## Spatial function mappings

This table shows which NTS members are translated into which SQL functions. Note that the translations vary depending on whether the column is of type geography or geometry.

Expand Down
Loading

0 comments on commit d0af197

Please sign in to comment.