☰ SQL lessons Home › Tutorials › SQL › SQL Server Functions Reference SQL Server Functions Reference 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.
Moving queries from SQL Server to PostgreSQL? Watch the functions whose behavior differs, not just their names: LEN ignores trailing spaces, + joins text but returns NULL if either side is NULL, and DATEDIFF counts boundaries crossed rather than full units.
String Functions SQL Server What it does PostgreSQL ASCII(c)Code of the first character ascii(c)CHAR(n)Character from a code chr(n)CHARINDEX(sub, s)Position of a substring position(sub IN s)CONCAT(a, b)Join strings, NULL as empty concat(a, b)a + bJoin strings; NULL if any is NULL `a CONCAT_WS(sep, a, b)Join with a separator concat_ws(sep, a, b)DATALENGTH(s)Length in bytes octet_length(s)FORMAT(x, fmt)Format a value to_char(x, fmt)LEFT(s, n)First n characters left(s, n)LEN(s)Length, ignoring trailing spaces length(rtrim(s))LOWER / UPPER(s)Change case lower / upperLTRIM / RTRIM / TRIM(s)Remove spaces ltrim / rtrim / trimPATINDEX(pattern, s)Position of a LIKE pattern position or regexp functions REPLACE(s, a, b)Replace text replace(s, a, b)REPLICATE(s, n)Repeat a string repeat(s, n)REVERSE(s)Reverse reverse(s)RIGHT(s, n)Last n characters right(s, n)SPACE(n)n spaces repeat(' ', n)STR(x)Number as text x::textSTRING_AGG(s, sep)Join values from many rows string_agg(s, sep)STUFF(s, pos, len, new)Replace part of a string overlay(s PLACING new FROM pos FOR len)SUBSTRING(s, pos, len)Part of a string substring(s FROM pos FOR len)TRANSLATE(s, from, to)Replace characters translate(s, from, to)
Numeric Functions SQL Server What it does PostgreSQL ABS / CEILING / FLOOR / ROUND(x, d)Absolute value and rounding abs / ceil / floor / roundPOWER(x, y) / SQRT / SQUAREPowers power, sqrt, x * xRAND()Random number random()SIGN(x)Sign sign(x)PI()π pi()LOG / LOG10 / EXPLogarithms and e^x ln, log, expAVG / COUNT / MAX / MIN / SUMAggregates same names a % bRemainder a % b
Date Functions SQL Server What it does PostgreSQL CURRENT_TIMESTAMP / GETDATE() / SYSDATETIME()Current date and time now()GETUTCDATE()Current UTC time now() AT TIME ZONE 'UTC'DATEADD(part, n, d)Add to a date d + n, or d + INTERVAL 'n days'DATEDIFF(part, a, b)Difference in units b - a (dates), EXTRACTDATEFROMPARTS(y, m, d)Build a date make_date(y, m, d)DATENAME(part, d)Name of a part to_char(d, 'FMMonth')DATEPART(part, d)Number of a part EXTRACT(part FROM d)DAY / MONTH / YEAR(d)Parts of a date EXTRACT(DAY ...)EOMONTH(d)Last day of the month (date_trunc('month', d) + INTERVAL '1 month - 1 day')::dateISDATE(s)1 if text is a valid date pg_input_is_valid(s, 'date')SYSDATETIMEOFFSET()Now with time zone now()
Advanced Functions SQL Server What it does PostgreSQL CAST(x AS type)Convert a type CAST(x AS type)CONVERT(type, x, style)Convert with a format style to_char, to_dateTRY_CAST / TRY_CONVERTConvert, or NULL on failure pg_input_is_valid, then CASTCOALESCE(a, b)First non-NULL COALESCE(a, b)ISNULL(a, b)Replace NULL COALESCE(a, b)IIF(cond, a, b)If / else CASE WHEN cond THEN a ELSE b ENDNULLIF(a, b)NULL when equal NULLIF(a, b)ISNUMERIC(s)1 if text looks numeric pg_input_is_valid(s, 'numeric')CURRENT_USER / SESSION_USER / USER_NAME()Current user current_userDB_NAME()Current database current_database()SCOPE_IDENTITY()Last identity value in this scope RETURNING id@@VERSIONServer version version()
For the full syntax of common tasks in SQL Server, see the SQL Server Syntax .
✕ Was this tutorial helpful?
👍 Yes 👎 No