← Back to MySQL Course | Chapter 13: Date & Numeric Functions | Lesson 2 of 6

ABS और MOD

ABS यह ignore करने जैसा है कि आप आगे चले या पीछे और सिर्फ distance count करना। MOD divide करने के बाद जो बचता है वह देता है।
Syntax
sql
ABS(number)
MOD(dividend, divisor)

ABS से Absolute Value

ABS किसी भी negative number को उसके positive equivalent में convert करता है साथ ही positive numbers को unchanged छोड़ते हुए, जो दो values के बीच के अंतर का size measure करने के लिए उपयोगी है बिना यह परवाह किए कि कौन बड़ा है।

उदाहरण: The Absolute Value with ABS

sql
SELECT ABS(-15) AS positive_value;

MOD से Remainder

MOD एक number को दूसरे से divide करने के बाद बचा remainder return करता है, ज़्यादातर programming languages में % operator जैसा ही operation — rows को एक fixed संख्या के groups में evenly distribute करने जैसे tasks के लिए उपयोगी।

उदाहरण: The Remainder with MOD

sql
SELECT MOD(17, 5) AS remainder;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Even और Odd Numbers ढूँढना

यह check करना कि एक number even है या odd MOD का एक classic इस्तेमाल है: 2 से divide करना और यह test करना कि remainder 0 है या नहीं आपको तुरंत evenness बताता है, बिना एक ज़्यादा complex conditional लिखे।

उदाहरण: Finding Even and Odd Numbers

sql
CREATE TABLE numbers (n INT);
INSERT INTO numbers VALUES (4), (7), (10);
SELECT n, CASE WHEN MOD(n, 2) = 0 THEN 'even' ELSE 'odd' END AS parity FROM numbers;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Decimal Values के साथ MOD इस्तेमाल करना

MOD सिर्फ whole numbers तक limited नहीं है — यह decimal values पर भी काम करता है, पहले number को दूसरे से divide करते हुए और एक error के बजाय बचा हुआ floating-point remainder return करते हुए।

उदाहरण: Using MOD with Decimal Values

sql
SELECT MOD(10.5, 3) AS decimal_remainder;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

ABS और MOD Combine करना

ABS और MOD को साथ combine करना negative inputs के साथ काम करते समय भी एक positive remainder guarantee करता है, क्योंकि अकेला MOD अगर dividend negative हो तो एक negative result return कर सकता है।

उदाहरण: Combining ABS and MOD

sql
SELECT MOD(-7, 3) AS raw_mod, ABS(MOD(-7, 3)) AS positive_mod;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

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. #}
आम गलतियां
  1. किसी negative number के MOD को positive होने की उम्मीद करना, जबकि MOD(-7, 3) -1 है।
  2. एक NULL पर ABS इस्तेमाल करना, जो NULL return करता है।
  3. MOD(x, 0) से zero से divide करना, जो NULL return करता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.