SQL CONV function
CONV('ff', 16, 10) is 255. It parses the input in the from-base and renders it in the to-base, all as strings, both directions.
- Returns
string- In this category
- 6 of 39
語法
CONV(number_or_string, from_base, to_base)
引擎支援
| 引擎 | Support | 備註 |
|---|---|---|
| SQL Server | Not built in | No built-in equivalent. |
| PostgreSQL | Not built in | No built-in equivalent. |
| MySQL | 是 | Supported under this name. |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Not built in | No built-in equivalent. |
範例
MySQL
SELECT CONV('ff', 16, 10) AS dec_; -- 255
MySQL
SELECT CONV(255, 10, 2) AS bin_; -- 11111111
小知識
- Both the input and the output are strings: CONV('ff', 16, 10) returns the string '255'. Cast if a numeric type is required downstream.
- Bases run from 2 to 36, using 0-9 and A-Z. Beyond hex, letters map upward: CONV('z', 36, 10) is 35.
- Parsing stops at the first digit that is invalid for the from-base: CONV('g', 16, 10) is '0' and CONV('1g', 16, 10) is '1'. No error, no NULL, just a wrong number.
- PostgreSQL reaches for to_hex and casts; SQL Server uses CONVERT with binary/hex styles; CONV's general base math is MySQL-only.
Questions about CONV
Does CONV return a number or a string?
A string, which keeps the to-base output representable for bases above 10. Cast the result when arithmetic needs a numeric type.
What bases does CONV support?
2 through 36. Digits above 9 are letters A-Z, case-insensitive on input.
How do I convert between bases in PostgreSQL or SQL Server?
Piecewise: PostgreSQL has to_hex() and int casts for binary via ::bit; SQL Server has CONVERT styles for hex. There is no general from-base parser outside MySQL.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: MySQL.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.