'SQL query to find first six characters of the first name if the name is too short append $
Attached two images the first image is the question second image is the code that which I tried. I am stuck with append $ . I have even added my code
T-sql:
CREATE TABLE Tab1 (
tname VARCHAR (50)
);
INSERT INTO Tab1(tname)
VALUES ('Ravi Ashwin'),
('Mahendra Singh Dhoni'),
('Shikhar Dhawan');
Select a tname, substring (tname, 1,6) AS First_Sic_character, LEN (tname) AS Character_count,
CASE WHEN LEN(tname) BETWEEN 0 AND 12 THEN 'SHORT & SWEET'
WHEN LEN(tname) BETWEEN 13 AND 15 THEN 'MEDIUM & PREMIUM'
WHEN LEN (tname) > 15 THEN ' LENGHTY$LOVELY'
END AS Category
From Tab1
Solution 1:[1]
use this
CONVERT(VARCHAR(6),tname+ SUBSTRING('$$$$$$',LEN(tname),6))
Solution 2:[2]
with cte(name) as(VALUES ('Ravi Ashwin'),
('Mahendra Singh Dhoni'),
('Shikhar Dhawan'))
select name,substring(name,1,6)
,(case when length(substring(name,1,6))<=6 then concat(name,'$') end )::varchar
,(case when length(name) between 0 and 12 then 'SHORT & SWEET'
when length(name) between 13 and 15 then 'MEDIUM & PREMIUM'
when length(name)>15 then 'LENGHTY$LOVELY' end)Category
from cte
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Piyush Kachhadiya |
| Solution 2 | Nandish |

