Model koncepcyjny Canonical do mapowania funkcji serwera SQL

W tym temacie opisano sposób mapowania koncepcyjnych funkcji kanonicznych modelu na odpowiednie funkcje programu SQL Server.

Funkcje daty i godziny

W poniższej tabeli opisano mapowanie funkcji daty i godziny:

Funkcje kanoniczne Funkcje programu SQL Server
AddDays(wyrażenie) DATEADD(day, number, date)
AddHours(expression) DATEADD(hour, number, date)
AddMicroseconds(expression) DATEADD(microsecond, number, date)
AddMilliseconds(expression) DATEADD(millisecond, number, date)
AddMinutes(expression) DATEADD(minute, number, date)
AddMonths(expression) DATEADD(month, number, date)
AddNanoseconds(expression) DATEADD(nanosecond, number, date)
AddSeconds(expression) DATEADD(second, number, date)
AddYears(expression) DATEADD(year, number, date)
CreateDateTime(rok, miesiąc, dzień, godzina, minuta, sekunda) W przypadku programów SQL Server 2000 i SQL Server 2005 datetime na serwerze jest tworzona sformatowana wartość. W przypadku programu SQL Server 2008 i nowszych wersji datetime2 na serwerze jest tworzona wartość.
CreateDateTimeOffset(rok, miesiąc, dzień, godzina, minuta, sekunda, tzoffset) Na datetimeoffset serwerze jest tworzona sformatowana wartość.

Nieobsługiwane w programie SQL Server 2000 lub SQL Server 2005.
CreateTime(godzina, minuta, sekunda) Na time serwerze jest tworzona sformatowana wartość.

Nieobsługiwane w programie SQL Server 2000 lub SQL Server 2005.
CurrentDateTime() SysDateTime() w programie SQLServer 2008.

GetDate() w usługach SQLServer 2000 i SQLServer 2005.
CurrentDateTimeOffset() SysDateTimeOffset() w programie SQL Server 2008.

Nieobsługiwane w programie SQL Server 2000 lub SQL Server 2005.
CurrentUtcDateTime() SysUtcDateTime() w programie SQLServer 2008. GetUtcDate() w programach SQL Server 2000 i SQL Server 2005.
DayOfYear(expression) DatePart(dayofyear, expression)
Day(expression) DatePart(day, expression)
DiffDays(startExpression, endExpression) DATEDIFF(day, startdate, enddate)
DiffHours(startExpression, endExpression) DATEDIFF(hour, startdate, enddate)
DiffMicroseconds(startExpression, endExpression) DATEDIFF(microsecond, startdate, enddate)
DiffMilliseconds(startExpression, endExpression) DATEDIFF(millisecond, startdate, enddate)
DiffMinutes(startExpression, endExpression) DATEDIFF(minute, startdate, enddate)
DiffNanoseconds(startExpression, endExpression) DATEDIFF(nanosecond, startdate, enddate)
DiffSeconds(startExpression, endExpression) DATEDIFF(second, startdate, enddate)
DiffYears(startExpression, endExpression) DATEDIFF(year, startdate, enddate)
GetTotalOffsetMinutes(DateTimeOffset) DatePart(tzoffset, expression)
Hour(expression) DatePart(hour, expression)
Milisekund(wyrażenie) DatePart(millisecond, expression)
Minute(expression) DatePart(minute, expression)
Month(expression) DatePart(month, expression)
Second(expression) DatePart(second, expression)
Truncate(wyrażenie) W przypadku programów SQL Server 2000 i SQL Server 2005 na serwerze jest tworzona obcięta sformatowana datetime wartość. W przypadku programu SQL Server 2008 i nowszych wersji na serwerze jest tworzony obcięty datetime2 lub datetimeoffset wartość.
Year(expression) DatePart(YEAR, expression)

Funkcje agregujące

W poniższej tabeli opisano mapowanie funkcji agregujących:

Funkcje kanoniczne Funkcje programu SQL Server
Avg(expression) AVG(expression)
BigCount(wyrażenie) BIGCOUNT(expression)
Count(expression) COUNT(expression)
Min(wyrażenie) MIN(expression)
Max(wyrażenie) MAX(expression)
StDev(wyrażenie) STDEV(expression)
StDevP(wyrażenie) STDEVP(expression)
Sum(expression) SUM(expression)
Var(expression) VAR(expression)
VarP(wyrażenie) VARP(expression)

Funkcje matematyczne

W poniższej tabeli opisano mapowanie funkcji matematycznych:

Funkcje kanoniczne Funkcje programu SQL Server
Abs(wartość) ABS(value)
Sufit (wartość) CEILING(value)
Floor(value) FLOOR(value)
Power(wartość) POWER(value, exponent)
Round(wartość) ROUND(value, digits, 0)
Obciąć ROUND(value , digits, 1)

Funkcje ciągów

W poniższej tabeli opisano mapowanie funkcji ciągów:

Funkcje kanoniczne Funkcje programu SQL Server
Contains(string, target) CHARINDEX(target, string)
Concat(ciąg1, ciąg2) ciąg1 + ciąg2
EndsWith(ciąg, element docelowy) CHARINDEX(REVERSE(target), REVERSE(string)) = 1

Uwaga Funkcja CHARINDEX zwraca false wartość , jeśli string element jest przechowywany w kolumnie ciągu o stałej długości i target jest stałą. W takim przypadku przeszukiwany jest cały ciąg, w tym wszystkie spacje końcowe. Możliwe obejście polega na przycinaniu danych w ciągu o stałej długości przed przekazaniem ciągu do EndsWith funkcji, jak w poniższym przykładzie: EndsWith(TRIM(string), target)
IndexOf(element docelowy, ciąg2) CHARINDEX(target, string2)
Lewa (ciąg1, długość) LEFT(string1, length)
Długość (ciąg) LEN(string)
LTrim(ciąg) LTRIM(string)
Prawo (ciąg1, długość) RIGHT (string1, length)
Trim(ciąg) LTRIM(RTRIM(string))
Zamień (ciąg1, ciąg2, ciąg3) REPLACE(string1, string2, string3)
Odwrotne (ciąg) REVERSE (string)
RTrim(ciąg) RTRIM(string)
StartsWith(ciąg, element docelowy) CHARINDEX(target, string)
Podciąg (ciąg, początek, długość) SUBSTRING(string, start, length)
ToLower(ciąg) LOWER(string)
ToUpper(ciąg) UPPER(string)

Funkcje bitowe

W poniższej tabeli opisano mapowanie funkcji bitowych:

Funkcje kanoniczne Funkcje programu SQL Server
BitWiseAnd (wartość1, wartość2) wartość1 i wartość2
BitWiseNot (wartość) ~Wartość
BitWiseOr (wartość1, wartość2) value1 | value2
BitWiseXor (wartość1, wartość2) value1 ^ value2