sqlcmd.net validated sql reference
beginner conversion MySQL MariaDB SQL Server PostgreSQL SQLite

Convert Values With CAST

Change a value from one SQL type to another using an explicit cast.

Docker-validated Not currently validation-green

Cast text digits to an integer

Casting avoids leaving the value as plain text when you need numeric behavior later in the query.

MySQL MariaDB
Engine-specific syntax
Setup
CREATE TABLE raw_scores (score_text VARCHAR(10));

INSERT INTO
  raw_scores (score_text)
VALUES
  ('42');
SQL
SELECT
  CAST(score_text AS SIGNED) AS score_number
FROM
  raw_scores;
score_number
42
SQL Server PostgreSQL
Engine-specific syntax
Setup
CREATE TABLE raw_scores (score_text VARCHAR(10));

INSERT INTO
  raw_scores (score_text)
VALUES
  ('42');
SQL
SELECT
  CAST(score_text AS INT) AS score_number
FROM
  raw_scores;
score_number
42
SQLite
Engine-specific syntax
Setup
CREATE TABLE raw_scores (score_text VARCHAR(10));

INSERT INTO
  raw_scores (score_text)
VALUES
  ('42');
SQL
SELECT
  CAST(score_text AS INTEGER) AS score_number
FROM
  raw_scores;
score_number
42

The cast target syntax differs slightly, but the converted numeric result is the same.

Where this command helps.

  • turning text input into a numeric type before calculation
  • making the output type explicit in reports or API-facing queries

What the command is doing.

CAST(expression AS type) converts a value into a target SQL type so it can be compared, formatted, or returned in the shape you need. Explicit casts are especially useful when imported data arrives as text but must behave like numbers, dates, or booleans.