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

DATE_FORMAT

DATE_FORMAT यह चुनने जैसा है कि एक card पर date कैसे लिखनी है, जैसे day पहले या month name पहले, हर piece के लिए special codes इस्तेमाल करके।
Syntax
sql
DATE_FORMAT(date, 'format_string')
-- e.g. '%d-%m-%Y'

DATE_FORMAT का Introduction

DATE_FORMAT आपको exactly control करने देता है कि एक date value कैसे display होती है, MySQL के raw default format पर भरोसा करने के बजाय year, month, और day का order और style चुनने के लिए format specifiers इस्तेमाल करते हुए।

उदाहरण: Introduction to DATE_FORMAT

sql
SELECT DATE_FORMAT('2024-06-15', '%d-%m-%Y') AS formatted_date;

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

Years और Months Extract करना

आप एक date का सिर्फ एक हिस्सा readable text की तरह निकाल सकते हैं, जैसे अकेले month name, जो पूरी date value expose किए बिना charts या reports पर labels generate करने के लिए उपयोगी है।

उदाहरण: Extracting Years and Months

sql
SELECT DATE_FORMAT('2024-06-15', '%M') AS month_name;

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

Time Values Format करना

Time components को dates जैसे ही format किया जा सकता है, आपको यह चुनने देते हुए कि आपकी audience क्या देखने की उम्मीद करती है इसके आधार पर AM/PM वाली एक 12-hour clock या 24-hour format।

उदाहरण: Formatting Time Values

sql
SELECT DATE_FORMAT('2024-06-15 14:30:00', '%h:%i %p') AS twelve_hour;
SELECT DATE_FORMAT('2024-06-15 14:30:00', '%H:%i') AS twenty_four_hour;

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

Custom Spacing और Slashes

Slashes, dashes, या words जैसे literal characters को सीधे format string में mix किया जा सकता है, और MySQL extracted date parts के आसपास exactly written की तरह उन्हें insert कर देगा एक पूरी तरह custom layout बनाने के लिए।

उदाहरण: Custom Spacing and Slashes

sql
SELECT DATE_FORMAT('2024-06-15', '%d/%m/%Y') AS slashed_date;

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

Table Dates Format करना

एक SELECT statement के अंदर DATE_FORMAT apply करना user के लिए display format करता है साथ ही actual stored date value को पूरी तरह untouched छोड़ते हुए, इसलिए sorting और filtering अभी भी real underlying date के against काम करते हैं।

उदाहरण: Formatting Table Dates

sql
CREATE TABLE orders (id INT, order_date DATE);
INSERT INTO orders VALUES (1, '2024-06-15');
SELECT id, DATE_FORMAT(order_date, '%d %M %Y') AS display_date FROM orders ORDER BY order_date;

⚠️ 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. minutes के लिए %m इस्तेमाल करना, जबकि %i minutes है और %m month है।
  2. %Y (four-digit year) और %y (two-digit year) को mix up करना।
  3. एक date को text की तरह format करना और फिर उससे sort करने की कोशिश करना, ताकि order alphabetical हो जाए।
🔒

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.