SQL Server Functions Reference

Updated · 4 min read
mediumPostgreSQL 18sqlsql-serverfunctionsreferenceassessment

This page lists the SQL Server functions you'll meet most, what each does, and how to write the same thing in PostgreSQL, the database this course's examples run on. It covers the functions in the common online SQL Server references. Names follow the SQL Server 2022 and later documentation; the examples on this site don't run SQL Server, so test a function on your own server before relying on it. The lessons String Functions, Numeric Functions, Date Functions and Type Conversion explain the ideas.

String Functions

SQL ServerWhat it doesPostgreSQL
ASCII(c)Code of the first characterascii(c)
CHAR(n)Character from a codechr(n)
CHARINDEX(sub, s)Position of a substringposition(sub IN s)
CONCAT(a, b)Join strings, NULL as emptyconcat(a, b)
a + bJoin strings; NULL if any is NULL`a
CONCAT_WS(sep, a, b)Join with a separatorconcat_ws(sep, a, b)
DATALENGTH(s)Length in bytesoctet_length(s)
FORMAT(x, fmt)Format a valueto_char(x, fmt)
LEFT(s, n)First n charactersleft(s, n)
LEN(s)Length, ignoring trailing spaceslength(rtrim(s))
LOWER / UPPER(s)Change caselower / upper
LTRIM / RTRIM / TRIM(s)Remove spacesltrim / rtrim / trim
PATINDEX(pattern, s)Position of a LIKE patternposition or regexp functions
REPLACE(s, a, b)Replace textreplace(s, a, b)
REPLICATE(s, n)Repeat a stringrepeat(s, n)
REVERSE(s)Reversereverse(s)
RIGHT(s, n)Last n charactersright(s, n)
SPACE(n)n spacesrepeat(' ', n)
STR(x)Number as textx::text
STRING_AGG(s, sep)Join values from many rowsstring_agg(s, sep)
STUFF(s, pos, len, new)Replace part of a stringoverlay(s PLACING new FROM pos FOR len)
SUBSTRING(s, pos, len)Part of a stringsubstring(s FROM pos FOR len)
TRANSLATE(s, from, to)Replace characterstranslate(s, from, to)

Numeric Functions

SQL ServerWhat it doesPostgreSQL
ABS / CEILING / FLOOR / ROUND(x, d)Absolute value and roundingabs / ceil / floor / round
POWER(x, y) / SQRT / SQUAREPowerspower, sqrt, x * x
RAND()Random numberrandom()
SIGN(x)Signsign(x)
PI()πpi()
LOG / LOG10 / EXPLogarithms and e^xln, log, exp
AVG / COUNT / MAX / MIN / SUMAggregatessame names
a % bRemaindera % b

Date Functions

SQL ServerWhat it doesPostgreSQL
CURRENT_TIMESTAMP / GETDATE() / SYSDATETIME()Current date and timenow()
GETUTCDATE()Current UTC timenow() AT TIME ZONE 'UTC'
DATEADD(part, n, d)Add to a dated + n, or d + INTERVAL 'n days'
DATEDIFF(part, a, b)Difference in unitsb - a (dates), EXTRACT
DATEFROMPARTS(y, m, d)Build a datemake_date(y, m, d)
DATENAME(part, d)Name of a partto_char(d, 'FMMonth')
DATEPART(part, d)Number of a partEXTRACT(part FROM d)
DAY / MONTH / YEAR(d)Parts of a dateEXTRACT(DAY ...)
EOMONTH(d)Last day of the month(date_trunc('month', d) + INTERVAL '1 month - 1 day')::date
ISDATE(s)1 if text is a valid datepg_input_is_valid(s, 'date')
SYSDATETIMEOFFSET()Now with time zonenow()

Advanced Functions

SQL ServerWhat it doesPostgreSQL
CAST(x AS type)Convert a typeCAST(x AS type)
CONVERT(type, x, style)Convert with a format styleto_char, to_date
TRY_CAST / TRY_CONVERTConvert, or NULL on failurepg_input_is_valid, then CAST
COALESCE(a, b)First non-NULLCOALESCE(a, b)
ISNULL(a, b)Replace NULLCOALESCE(a, b)
IIF(cond, a, b)If / elseCASE WHEN cond THEN a ELSE b END
NULLIF(a, b)NULL when equalNULLIF(a, b)
ISNUMERIC(s)1 if text looks numericpg_input_is_valid(s, 'numeric')
CURRENT_USER / SESSION_USER / USER_NAME()Current usercurrent_user
DB_NAME()Current databasecurrent_database()
SCOPE_IDENTITY()Last identity value in this scopeRETURNING id
@@VERSIONServer versionversion()

For the full syntax of common tasks in SQL Server, see the SQL Server Syntax.

Was this tutorial helpful?