Type की Casting
Casting एक value को CAST या :: shortcut से एक type से दूसरे में convert करता है।
In this page:
Syntax
SELECT CAST(expression AS type);
SELECT expression::type;
Type Casting
CAST(expr AS type) standard SQL है और हर database में काम करता है, जबकि expr::type PostgreSQL का shorter form है। Text को number में cast करना fail होता है अगर text एक valid number न हो।
Integer division truncate करता है, इसलिए जब आपको fractional result चाहिए एक side को decimal type में cast करें। Example CAST इस्तेमाल करता है ताकि यह SQLite पर भी चले।
Note:
PostgreSQL में, 42::integer और CAST(42 AS integer) equivalent हैं।
उदाहरण: Type casting
SELECT CAST('42' AS INTEGER) + 8 AS sum_int;
SELECT 7 / 2 AS integer_division, CAST(7 AS REAL) / 2 AS decimal_division;
SELECT CAST(3.99 AS INTEGER) AS truncated;
SELECT CAST(12345 AS TEXT) || ' items' AS label;
-- Output:
-- sum_int
-- 50
-- integer_division | decimal_division
-- 3 | 3.5
-- truncated
-- 3
-- label
-- 12345 items
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- integers divide करना और decimals की उम्मीद करना
- invalid text को numbers में cast करना
- implicit conversions पर depend करना
चैप्टर सारांश
- CAST(expr AS type) standard है
- :: PostgreSQL shortcut है
- Invalid text cast होने में fail होता है
- decimals के लिए divide करने से पहले cast करें
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: