Tier 1 · Foundations

Literal Values, Data Types, and Type Casting in SQL

By Owen Middleton · Updated September 2026 · Examples run on PostgreSQL 17

What is Type Casting in SQL?

Every value in SQL has a type, and that type determines what SQL can do with it.

When you write 48000 * 0.075, SQL already knows what it's working with: both values are numeric, so the multiplication works. But sometimes a value arrives as the wrong type. A date stored as text. A price stored as a string. SQL won't treat those as numbers or dates on its own. You have to tell it what type a value really is. The :: operator handles this — it's called a cast.

How do you cast a value to another type in SQL?

Think of :: as saying "treat this as." You put it after the value, then write the target type. A text string like '2025-06-30' is just characters until you add ::date:

SELECT '2025-06-30'::date AS contract_end_date

Now SQL treats it as a real date. The ::date cast is what makes that switch.

You can cast between many types. Text to number: '199.99'::numeric. Number to text: 42::text. These come up regularly when data is imported from spreadsheets or external systems where everything lands as a string.

What is the difference between :: and CAST() in SQL?

SQL also has a second casting syntax that does the same thing:

SELECT CAST('2024-09-01' AS date) AS launch_date

:: is shorter and more common in PostgreSQL. CAST() is standard SQL and appears in other databases. Both produce the same result.

What is a typed literal like INTERVAL '30 days'?

SQL also has type-prefixed literals for some types. An interval — a duration of time — is written as INTERVAL '30 days'. The type name comes before the quoted value instead of after it.

SELECT '10000'::integer / 4 AS truncated_result,
       '10000'::numeric / 4 AS decimal_result

Why does integer division drop the decimal in SQL?

The one thing that trips people up: integer division drops the decimal.

Divide an integer by an integer and SQL returns an integer, cutting off anything after the decimal point. '10000'::integer / 4 returns 2500, not 2500.0. Cast to ::numeric first and you get the full decimal result. The same fix works whenever you need the fractional part of a division: make at least one side a decimal type.

Practice Type Casting in SQL

Practice · easy

Brightlane's legal team uses a contract management system that stores expiry dates as plain text strings rather than calendar dates. The string '2025-06-30' needs to be returned as a proper date value so the system can perform date comparisons.

Write a query to convert that text string into a date.

Output:

  • A single row with one column, contract_end_date, typed as a date.

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution

The full breakdown walks through the shape, each clause, why this approach beats the alternatives, and the trap to avoid.

See the full worked solution

9 Type Casting practice problems

Start learning to practice all 9 Type Casting problems, with instant grading and mastery tracking.

Deeper guides on Type Casting

  • casting a string to a date

    ::date for ISO strings, to_date() for everything else, and the day-first date Postgres reads as a month.

Common questions about Type Casting

What is the difference between the double colon and CAST?

Only the spelling. Both produce the same value from the same input, so you can use either wherever a cast is allowed. The double colon is shorter and the form you will meet most often in PostgreSQL code.

What happens when a cast fails?

The query stops with an error naming the type and the value it could not read, such as invalid input syntax for type integer. A cast is not a best-effort conversion, so PostgreSQL will not quietly hand back a zero or a null for text that is not a number.

Does casting a column change the data in the table?

No. A cast produces a new value in the result of that one query and leaves the stored column exactly as it was. Casting a numeric price to text gives you text in the output while the column itself is still numeric.

How do you stop integer division from dropping the decimals?

Cast one side to numeric before dividing. Dividing an integer by an integer produces an integer, and the fractional part is discarded rather than rounded, so nine divided by ten comes out as zero. Make either side numeric and the full result survives.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.