Referência de Funções SQL

Referência pesquisável de funções SQL para PostgreSQL, MySQL, SQLite e SQL Server. Filtre por categoria e dialeto.

LENGTH
PGMySQLSQLiteSQL

Returns the number of characters in a string.

Sintaxe

LENGTH(string)

Retorna

INTEGER

Exemplo

SELECT LENGTH('hello') -- 5

MSSQLLEN — Does not count trailing spaces

UPPER
PGMySQLSQLiteMSSQLSQL

Converts all characters in a string to uppercase.

Sintaxe

UPPER(string)

Retorna

TEXT

Exemplo

SELECT UPPER('hello') -- 'HELLO'
LOWER
PGMySQLSQLiteMSSQLSQL

Converts all characters in a string to lowercase.

Sintaxe

LOWER(string)

Retorna

TEXT

Exemplo

SELECT LOWER('HELLO') -- 'hello'
SUBSTRING
PGMySQLMSSQLSQL

Extracts a substring starting at position start (1-indexed). Optional length limits characters returned.

Sintaxe

SUBSTRING(string FROM start [FOR length])

Retorna

TEXT

Exemplo

SELECT SUBSTRING('hello world' FROM 7 FOR 5) -- 'world'

SQLiteSUBSTR

MySQLMID — Alias for SUBSTRING

CONCAT
PGMySQLMSSQLSQL

Concatenates two or more strings together.

Sintaxe

CONCAT(string1, string2, ...)

Retorna

TEXT

Exemplo

SELECT CONCAT('hello', ' ', 'world') -- 'hello world'

SQLite|| — Use || operator

CONCAT_WS
PGMySQLMSSQL

Concatenates strings with a separator, skipping NULL values.

Sintaxe

CONCAT_WS(separator, string1, string2, ...)

Retorna

TEXT

Exemplo

SELECT CONCAT_WS(', ', 'Alice', NULL, 'Bob') -- 'Alice, Bob'
REPLACE
PGMySQLSQLiteMSSQLSQL

Replaces all occurrences of a substring with another string.

Sintaxe

REPLACE(string, from, to)

Retorna

TEXT

Exemplo

SELECT REPLACE('hello world', 'world', 'SQL') -- 'hello SQL'
TRIM
PGMySQLSQLiteMSSQLSQL

Removes leading and/or trailing characters (default: spaces) from a string.

Sintaxe

TRIM([LEADING | TRAILING | BOTH] [chars] FROM string)

Retorna

TEXT

Exemplo

SELECT TRIM('  hello  ') -- 'hello'

SQLLTRIM — Leading spaces only

SQLRTRIM — Trailing spaces only

INSTR
MySQLSQLite

Returns the position of the first occurrence of a substring (1-indexed, 0 if not found).

Sintaxe

INSTR(string, substring)

Retorna

INTEGER

Exemplo

SELECT INSTR('hello world', 'world') -- 7

PGPOSITION

MSSQLCHARINDEX

LEFT
PGMySQLMSSQL

Returns the leftmost n characters of a string.

Sintaxe

LEFT(string, n)

Retorna

TEXT

Exemplo

SELECT LEFT('hello world', 5) -- 'hello'

SQLiteSUBSTR

RIGHT
PGMySQLMSSQL

Returns the rightmost n characters of a string.

Sintaxe

RIGHT(string, n)

Retorna

TEXT

Exemplo

SELECT RIGHT('hello world', 5) -- 'world'
REPEAT
PGMySQL

Returns a string repeated count times.

Sintaxe

REPEAT(string, count)

Retorna

TEXT

Exemplo

SELECT REPEAT('ab', 3) -- 'ababab'

MSSQLREPLICATE

REVERSE
PGMySQLMSSQL

Returns the string with characters in reverse order.

Sintaxe

REVERSE(string)

Retorna

TEXT

Exemplo

SELECT REVERSE('hello') -- 'olleh'
LPAD
PGMySQL

Pads a string on the left to a total length using the fill character.

Sintaxe

LPAD(string, length, fill)

Retorna

TEXT

Exemplo

SELECT LPAD('42', 5, '0') -- '00042'

MSSQLRIGHT + REPLICATE

RPAD
PGMySQL

Pads a string on the right to a total length using the fill character.

Sintaxe

RPAD(string, length, fill)

Retorna

TEXT

Exemplo

SELECT RPAD('hi', 5, '.') -- 'hi...'
SPLIT_PART
PG

Splits a string by delimiter and returns the nth field (1-indexed).

Sintaxe

SPLIT_PART(string, delimiter, n)

Retorna

TEXT

Exemplo

SELECT SPLIT_PART('a,b,c', ',', 2) -- 'b'

MySQLSUBSTRING_INDEX

REGEXP_REPLACE
PGMySQL

Replaces substrings matching a regular expression with a replacement string.

Sintaxe

REGEXP_REPLACE(string, pattern, replacement)

Retorna

TEXT

Exemplo

SELECT REGEXP_REPLACE('abc123', '[0-9]+', 'N') -- 'abcN'

MSSQLNot built-in — Requires CLR or workaround

NOW
PGMySQLSQLite

Returns the current date and time (including timezone in PostgreSQL).

Sintaxe

NOW()

Retorna

TIMESTAMP

Exemplo

SELECT NOW() -- 2024-06-15 14:30:00

MSSQLGETDATE — No timezone

CURRENT_DATE
PGMySQLSQLiteMSSQLSQL

Returns the current date without a time component.

Sintaxe

CURRENT_DATE

Retorna

DATE

Exemplo

SELECT CURRENT_DATE -- 2024-06-15

MySQLCURDATE

DATE_ADD
MySQL

Adds a time interval to a date or timestamp.

Sintaxe

DATE_ADD(date, INTERVAL n unit)

Retorna

DATE / TIMESTAMP

Exemplo

SELECT DATE_ADD('2024-01-01', INTERVAL 7 DAY) -- '2024-01-08'

PG+

MSSQLDATEADD

SQLiteDATE

DATEDIFF
MySQL

Returns the number of days between two dates.

Sintaxe

DATEDIFF(date1, date2)

Retorna

INTEGER

Exemplo

SELECT DATEDIFF('2024-01-31', '2024-01-01') -- 30

PGSubtraction — Returns INTERVAL or days as INTEGER

MSSQLDATEDIFF

SQLiteJULIANDAY

EXTRACT
PGMySQLSQLiteSQL

Extracts a specific date/time part (YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, WEEK, DOW).

Sintaxe

EXTRACT(part FROM date)

Retorna

NUMERIC

Exemplo

SELECT EXTRACT(YEAR FROM NOW()) -- 2024

MSSQLDATEPART

DATE_TRUNC
PG

Truncates a timestamp to the specified precision (year, month, day, hour, minute, etc.).

Sintaxe

DATE_TRUNC('unit', timestamp)

Retorna

TIMESTAMP

Exemplo

SELECT DATE_TRUNC('month', NOW()) -- 2024-06-01 00:00:00

MySQLDATE_FORMAT — Workaround for month truncation

MSSQLDATETRUNC — SQL Server 2022+

TO_CHAR
PG

Formats a date, timestamp, or number as a string using a format pattern.

Sintaxe

TO_CHAR(value, 'format')

Retorna

TEXT

Exemplo

SELECT TO_CHAR(NOW(), 'YYYY-MM-DD') -- '2024-06-15'

MySQLDATE_FORMAT

MSSQLFORMAT

SQLiteSTRFTIME

AGE
PG

Returns the interval between two timestamps expressed in years, months, and days.

Sintaxe

AGE(timestamp1, timestamp2)

Retorna

INTERVAL

Exemplo

SELECT AGE('2024-06-15', '2000-03-01') -- '24 years 3 months 14 days'
ABS
PGMySQLSQLiteMSSQLSQL

Returns the absolute (non-negative) value of a number.

Sintaxe

ABS(number)

Retorna

same as input

Exemplo

SELECT ABS(-42) -- 42
ROUND
PGMySQLSQLiteMSSQLSQL

Rounds a number to the specified number of decimal places (default 0).

Sintaxe

ROUND(number [, decimals])

Retorna

NUMERIC

Exemplo

SELECT ROUND(3.14159, 2) -- 3.14
CEIL
PGMySQLSQLiteSQL

Rounds a number up to the nearest integer.

Sintaxe

CEIL(number)

Retorna

INTEGER

Exemplo

SELECT CEIL(4.2) -- 5

MSSQLCEILING

FLOOR
PGMySQLSQLiteMSSQLSQL

Rounds a number down to the nearest integer.

Sintaxe

FLOOR(number)

Retorna

INTEGER

Exemplo

SELECT FLOOR(4.9) -- 4
MOD
PGMySQLSQL

Returns the remainder after division.

Sintaxe

MOD(dividend, divisor)

Retorna

same as input

Exemplo

SELECT MOD(10, 3) -- 1

SQLite%

MSSQL%

POWER
PGMySQLMSSQLSQL

Returns base raised to the power of exponent.

Sintaxe

POWER(base, exponent)

Retorna

NUMERIC

Exemplo

SELECT POWER(2, 10) -- 1024

SQLiteNot built-in — Use ROUND(EXP(exponent*LN(base)), 10) workaround

SQRT
PGMySQLMSSQLSQL

Returns the square root of a non-negative number.

Sintaxe

SQRT(number)

Retorna

NUMERIC

Exemplo

SELECT SQRT(16) -- 4
GREATEST
PGMySQLSQLite

Returns the largest value from a list of expressions.

Sintaxe

GREATEST(value1, value2, ...)

Retorna

same as inputs

Exemplo

SELECT GREATEST(3, 1, 4, 1, 5) -- 5

MSSQLNot built-in — Use CASE WHEN or VALUES workaround

LEAST
PGMySQLSQLite

Returns the smallest value from a list of expressions.

Sintaxe

LEAST(value1, value2, ...)

Retorna

same as inputs

Exemplo

SELECT LEAST(3, 1, 4, 1, 5) -- 1
RANDOM
PGSQLite

Returns a random floating-point value between 0.0 and 1.0.

Sintaxe

RANDOM()

Retorna

FLOAT

Exemplo

SELECT RANDOM() -- 0.7381924...

MySQLRAND

MSSQLRAND

SIGN
PGMySQLMSSQLSQL

Returns -1 for negative numbers, 0 for zero, and 1 for positive numbers.

Sintaxe

SIGN(number)

Retorna

INTEGER

Exemplo

SELECT SIGN(-42) -- -1
COUNT
PGMySQLSQLiteMSSQLSQL

Counts rows. COUNT(*) counts all rows; COUNT(col) skips NULLs; DISTINCT counts unique non-null values.

Sintaxe

COUNT(*) | COUNT(column) | COUNT(DISTINCT column)

Retorna

BIGINT

Exemplo

SELECT COUNT(*) FROM users -- 150
SUM
PGMySQLSQLiteMSSQLSQL

Returns the sum of non-null numeric values.

Sintaxe

SUM(expression)

Retorna

NUMERIC

Exemplo

SELECT SUM(amount) FROM orders -- 42000.00
AVG
PGMySQLSQLiteMSSQLSQL

Returns the arithmetic mean of non-null values.

Sintaxe

AVG(expression)

Retorna

NUMERIC

Exemplo

SELECT AVG(score) FROM results -- 87.5
MIN
PGMySQLSQLiteMSSQLSQL

Returns the minimum non-null value in the group.

Sintaxe

MIN(expression)

Retorna

same as input

Exemplo

SELECT MIN(created_at) FROM orders -- 2023-01-01
MAX
PGMySQLSQLiteMSSQLSQL

Returns the maximum non-null value in the group.

Sintaxe

MAX(expression)

Retorna

same as input

Exemplo

SELECT MAX(score) FROM results -- 100
STRING_AGG
PGMSSQL

Concatenates non-null string values from a group with a delimiter.

Sintaxe

STRING_AGG(expression, delimiter)

Retorna

TEXT

Exemplo

SELECT STRING_AGG(name, ', ') FROM users -- 'Alice, Bob, Eve'

MySQLGROUP_CONCAT

ARRAY_AGG
PG

Collects values from a group into an array (PostgreSQL) or JSON array.

Sintaxe

ARRAY_AGG(expression [ORDER BY ...])

Retorna

ARRAY

Exemplo

SELECT ARRAY_AGG(id ORDER BY id) FROM users -- {1,2,3}

MySQLJSON_ARRAYAGG

BOOL_AND
PG

Returns TRUE if all non-null input values are true.

Sintaxe

BOOL_AND(expression)

Retorna

BOOLEAN

Exemplo

SELECT BOOL_AND(active) FROM users -- false

MySQLBIT_AND workaround — Use MIN(active = 1) as workaround

ROW_NUMBER
PGMySQLSQLiteMSSQLSQL

Assigns a unique sequential integer to each row within a partition, starting at 1.

Sintaxe

ROW_NUMBER() OVER ([PARTITION BY ...] ORDER BY ...)

Retorna

BIGINT

Exemplo

SELECT ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) AS rn FROM employees
RANK
PGMySQLSQLiteMSSQLSQL

Assigns a rank to each row; ties share the same rank and leave gaps in sequence.

Sintaxe

RANK() OVER ([PARTITION BY ...] ORDER BY ...)

Retorna

BIGINT

Exemplo

SELECT RANK() OVER (ORDER BY score DESC) AS rnk FROM results
DENSE_RANK
PGMySQLSQLiteMSSQLSQL

Like RANK but without gaps: tied rows share the same rank and the next rank is consecutive.

Sintaxe

DENSE_RANK() OVER ([PARTITION BY ...] ORDER BY ...)

Retorna

BIGINT

Exemplo

SELECT DENSE_RANK() OVER (ORDER BY score DESC) FROM results
LAG
PGMySQLSQLiteMSSQLSQL

Returns the value from a row that is offset rows before the current row.

Sintaxe

LAG(expression [, offset [, default]]) OVER ([PARTITION BY ...] ORDER BY ...)

Retorna

same as expression

Exemplo

SELECT LAG(revenue) OVER (ORDER BY month) AS prev_revenue FROM sales
LEAD
PGMySQLSQLiteMSSQLSQL

Returns the value from a row that is offset rows after the current row.

Sintaxe

LEAD(expression [, offset [, default]]) OVER ([PARTITION BY ...] ORDER BY ...)

Retorna

same as expression

Exemplo

SELECT LEAD(revenue) OVER (ORDER BY month) AS next_revenue FROM sales
NTILE
PGMySQLSQLiteMSSQLSQL

Divides rows into n roughly equal groups (tiles) and returns the tile number for each row.

Sintaxe

NTILE(n) OVER ([PARTITION BY ...] ORDER BY ...)

Retorna

INTEGER

Exemplo

SELECT NTILE(4) OVER (ORDER BY score) AS quartile FROM results
FIRST_VALUE
PGMySQLSQLiteMSSQLSQL

Returns the value of expression from the first row of the window frame.

Sintaxe

FIRST_VALUE(expression) OVER ([PARTITION BY ...] ORDER BY ...)

Retorna

same as expression

Exemplo

SELECT FIRST_VALUE(salary) OVER (PARTITION BY dept ORDER BY salary DESC) FROM employees
LAST_VALUE
PGMySQLMSSQLSQL

Returns the value from the last row of the window frame. Requires explicit ROWS BETWEEN clause for meaningful results.

Sintaxe

LAST_VALUE(expression) OVER ([PARTITION BY ...] ORDER BY ... ROWS BETWEEN ... AND CURRENT ROW)

Retorna

same as expression

Exemplo

SELECT LAST_VALUE(salary) OVER (PARTITION BY dept ORDER BY salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM employees
PERCENT_RANK
PGMySQLSQLiteMSSQLSQL

Returns the relative rank of a row as a fraction between 0 and 1.

Sintaxe

PERCENT_RANK() OVER ([PARTITION BY ...] ORDER BY ...)

Retorna

FLOAT

Exemplo

SELECT PERCENT_RANK() OVER (ORDER BY score) FROM results
COALESCE
PGMySQLSQLiteMSSQLSQL

Returns the first non-null value from the list. Short-circuits once a non-null is found.

Sintaxe

COALESCE(value1, value2, ...)

Retorna

same as inputs

Exemplo

SELECT COALESCE(NULL, NULL, 'fallback') -- 'fallback'
NULLIF
PGMySQLSQLiteMSSQLSQL

Returns NULL if value1 equals value2; otherwise returns value1. Useful to avoid division-by-zero.

Sintaxe

NULLIF(value1, value2)

Retorna

same as value1

Exemplo

SELECT total / NULLIF(divisor, 0) FROM sales
CASE
PGMySQLSQLiteMSSQLSQL

Evaluates conditions in order and returns the result for the first true condition. Equivalent to if-else.

Sintaxe

CASE WHEN condition THEN result [...] [ELSE default] END

Retorna

same as result expressions

Exemplo

SELECT CASE WHEN score >= 90 THEN 'A' WHEN score >= 80 THEN 'B' ELSE 'C' END FROM results
IIF
MSSQL

Short form of CASE WHEN: returns true_value if condition is true, otherwise false_value.

Sintaxe

IIF(condition, true_value, false_value)

Retorna

same as true/false expressions

Exemplo

SELECT IIF(active = 1, 'Active', 'Inactive') FROM users

MySQLIF

CAST
PGMySQLSQLiteMSSQLSQL

Converts a value to the specified data type. Standard SQL syntax supported by all major databases.

Sintaxe

CAST(expression AS type)

Retorna

specified type

Exemplo

SELECT CAST('42' AS INTEGER) -- 42

PG:: — PostgreSQL shorthand

CONVERT
MySQLMSSQL

Converts a value to the specified type. Dialect varies: MySQL/SQL Server have CONVERT; PostgreSQL prefers CAST.

Sintaxe

CONVERT(type, expression)

Retorna

specified type

Exemplo

SELECT CONVERT(INT, '42') -- 42
TO_DATE
PG

Parses a string into a DATE using the specified format pattern.

Sintaxe

TO_DATE(string, 'format')

Retorna

DATE

Exemplo

SELECT TO_DATE('15-Jun-2024', 'DD-Mon-YYYY') -- 2024-06-15

MySQLSTR_TO_DATE

MSSQLPARSE

TO_NUMBER
PG

Converts a formatted string to a numeric value.

Sintaxe

TO_NUMBER(string, 'format')

Retorna

NUMERIC

Exemplo

SELECT TO_NUMBER('1,234.56', '9,999.99') -- 1234.56

MySQLCAST + REPLACE — CAST(REPLACE('1,234.56', ',', '') AS DECIMAL)

MSSQLPARSE

JSON_EXTRACT
MySQLSQLite

Extracts a value from a JSON document at the specified path.

Sintaxe

JSON_EXTRACT(json, path)

Retorna

JSON / scalar

Exemplo

SELECT JSON_EXTRACT('{"name":"Alice"}', '$.name') -- "Alice"

PG-> — Use ->> for text

MSSQLJSON_VALUE — Returns scalar only

->
PG

PostgreSQL operator: extracts a JSON sub-object or array element. Use ->> to get a text value.

Sintaxe

json_col -> 'key'

Retorna

JSON

Exemplo

SELECT data -> 'address' -> 'city' FROM users

PG->> — Returns text, not JSON

JSON_OBJECT
MySQLSQLiteMSSQL

Creates a JSON object from key-value pairs.

Sintaxe

JSON_OBJECT(key, value [, key, value ...])

Retorna

JSON

Exemplo

SELECT JSON_OBJECT('name', 'Alice', 'age', 30) -- {"name":"Alice","age":30}

PGjsonb_build_object

JSON_ARRAY
MySQLSQLiteMSSQL

Creates a JSON array from the provided values.

Sintaxe

JSON_ARRAY(value1, value2, ...)

Retorna

JSON

Exemplo

SELECT JSON_ARRAY(1, 'two', true) -- [1,"two",true]

PGjson_build_array

JSON_AGG
PG

Aggregates values as a JSON array. Useful with GROUP BY to collect related rows.

Sintaxe

JSON_AGG(expression)

Retorna

JSON

Exemplo

SELECT dept, JSON_AGG(name) FROM employees GROUP BY dept

MySQLJSON_ARRAYAGG

JSON_EACH
PGSQLite

Expands a JSON object into a set of (key, value) rows. Used in FROM clause.

Sintaxe

JSON_EACH(json)

Retorna

TABLE(key TEXT, value JSON)

Exemplo

SELECT key, value FROM JSON_EACH('{"a":1,"b":2}')

PGjson_each

MSSQLOPENJSON

JSONB_BUILD_OBJECT
PG

PostgreSQL: creates a JSONB object from key-value pairs. Prefer over json_build_object when working with JSONB columns.

Sintaxe

JSONB_BUILD_OBJECT(key, value [, ...])

Retorna

JSONB

Exemplo

SELECT JSONB_BUILD_OBJECT('id', id, 'name', name) FROM users

68 functions