इसके लिए CASE स्टेटमेंट का उपयोग करें।
उदाहरण
टेबल बनाएं:
mysql> create table DemoTable (
-> Title text
-> );
Query OK, 0 rows affected (0.61 sec)
रिकॉर्ड डालें:
mysql> insert into DemoTable values('My name is John and this is my first tutorial on MySQL');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('This is Carol and I work on MongoDB');
Query OK, 1 row affected (0.19 sec)
सभी रिकॉर्ड देखें:
mysql> select * from DemoTable;
आउटपुट
+--------------------------------------------------------+ | Title | +--------------------------------------------------------+ | My name is John and this is my first tutorial on MySQL | | This is Carol and I work on MongoDB | +--------------------------------------------------------+ 2 rows in set (0.00 sec)
10 से अधिक शब्द होने पर डॉट्स जोड़ने की क्वेरी:
mysql> select (case when substring_index(tbl.Title, ' ', 10) = tbl.Title
-> then tbl.Title
-> else concat(substring_index(tbl.Title, ' ', 10), '....................')
-> end) AS AddDots
-> from DemoTable tbl;
आउटपुट
+-------------------------------------------------------------------+ | AddDots | +-------------------------------------------------------------------+ | My name is John and this is my first tutorial.................... | | This is Carol and I work on MongoDB | +-------------------------------------------------------------------+ 2 rows in set (0.00 sec)
व्याख्या: SUBSTRING_INDEX(स्ट्रिंग, डिलीमीटर, काउंट) - काउंट डिलीमीटर तक का भाग लौटाता है। यदि 10वें स्पेस तक का भाग पूरा स्ट्रिंग के बराबर है, तो पूरा स्ट्रिंग लौटाएं, अन्यथा डॉट्स जोड़ें।