Udostępnij przez


Koncepcyjny model kanoniczny do mapowania funkcji programu SQL Server

W tym temacie opisano, jak funkcje kanoniczne modelu koncepcyjnego są mapowane na odpowiadające im funkcje SQL Server.

Funkcje daty i godziny

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

Funkcje kanoniczne Funkcje programu SQL Server
DodajDni(wyrażenie) DATEADD(day, number, date)
AddHours(expression) DATEADD(hour, number, date)
AddMicroseconds(expression) DATEADD(microsecond, number, date)
AddMilliseconds(expression) DATEADD(millisecond, number, date)
DodajMinuty(wyrażenie) 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) Na serwerze dla SQL Server 2000 i SQL Server 2005 tworzona jest wartość sformatowana datetime. Dla SQL Server 2008 i jego nowszych wersji na serwerze tworzona jest wartość datetime2.
CreateDateTimeOffset(rok, miesiąc, dzień, godzina, minuta, sekunda, tzoffset) Na serwerze tworzona jest wartość sformatowana.

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

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

GetDate() w 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)
Dzień(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)
Godzina(wyrażenie) DatePart(hour, expression)
Milisekund(wyrażenie) DatePart(millisecond, expression)
Minuta(expression) DatePart(minute, expression)
Month(expression) DatePart(month, expression)
Second(expression) DatePart(second, expression)
Skróć(wyrażenie) W przypadku SQL Server 2000 i SQL Server 2005 na serwerze tworzona jest obcięta wartość sformatowana jako datetime. W przypadku programu SQL Server 2008 i nowszych wersji na serwerze tworzona jest obcięta wartość datetime2 lub datetimeoffset.
Year(expression) DatePart(YEAR, expression)

Agregujących

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)
Moc(wartość) POWER(value, exponent)
Round(wartość) ROUND(value, digits, 0)
obcinanie 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, jeśli string 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)
Prawa (ciąg1, długość) RIGHT (string1, length)
Trim(tekst) LTRIM(RTRIM(string))
Zamień (ciąg1, ciąg2, ciąg3) REPLACE(string1, string2, string3)
Odwróć (łańcuch znaków) REVERSE (string)
RTrim(ciąg) RTRIM(string)
StartsWith(tekst, element docelowy) CHARINDEX(target, string)
Podciąg (ciąg, początek, długość) SUBSTRING(string, start, length)
ToLower(ciąg) LOWER(string)
ToUpper(łańcuch) 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) wartość1 | wartość2
BitWiseXor (wartość1, wartość2) wartość1 ^ wartość2