Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 June SQL Server Interview: Advance SQL Query – Find a count of repeated character in a String

SQL Server Interview: Advance SQL Query – Find a count of repeated character in a String

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing one complex SQL Query interview question and solution for T-SQL Developers.

Last week, I asked this question to one of the T-SQL Developer.
If you are going for database developer interview, you must find and practice complex or advance SQL Queries.

Question: Find a count of given repeated character from a string

Create a table with sample data:

1
2
3
4
5
6
7
8
9
10
CREATE TABLE tbl_Strings (name VARCHAR(30))
GO
 
INSERT INTO tbl_Strings
VALUES
('India'),('Southern Europe')
,('Japan'),('Indonesia')
,('Swaziland'),('South Africa')
,('United Arab Emirates'),('United States')
GO

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
;WITH CTE AS
(
SELECT name, CONVERT(VARCHAR(1),LEFT(name,1)) AS Letter, RIGHT(name, LEN(name)-1) AS Remain
FROM tbl_Strings
WHERE LEN(name)>1
UNION ALL
SELECT name, CONVERT(VARCHAR(1),LEFT(Remain,1)) AS Letter,
RIGHT(Remain, LEN(Remain)-1) AS Remain
FROM CTE
WHERE LEN(Remain)>0
)
SELECT
name
,Letter
,ASCII(Letter) AS CharAsciiCode
,COUNT(Letter) AS CountOfLetter
FROM CTE
WHERE Letter IN('a','A')
GROUP BY name, Letter, ASCII(Letter)

The Result:

1
2
3
4
5
6
7
8
9
10
11
name Letter CharAsciiCode CountOfLetter
------------------------------ ------ ------------- -------------
South Africa A 65 1
United Arab Emirates A 65 1
India a 97 1
Indonesia a 97 1
Japan a 97 2
South Africa a 97 1
Swaziland a 97 2
United Arab Emirates a 97 2
United States a 97 1

Jun 1, 2017Anvesh Patel
SQL Server Interview: Advance SQL Query - Find String values which are adjacent to each otherSQL Server Interview: Advance SQL Query - Find Permutations and Combinations of a String Column
Comments: 4
  1. Hemanth Sharma
    November 25, 2017 at 11:58 am

    In the above query why we need to use “ASCII(Letter) AS CharAsciiCode” in the select statement?
    I tried without using that and got the output with no capital letters

    For Eg: India
    & output is like —– n 1 , d 1 , i 1 , a 1

    ReplyCancel
    • Anvesh Patel
      Anvesh Patel
      November 26, 2017 at 6:03 pm

      Sure, you can also share your solution here

      ReplyCancel
  2. Sai Reddy
    July 3, 2018 at 8:27 am

    select name,len(name)-len(replace(name,’a’,”)) from tbl_Strings

    ReplyCancel
  3. Ankush
    October 1, 2018 at 9:18 am

    select coalesce(length(‘INDIA’) – length(replace(‘INDIA’,’A’,null)), length(‘INDIA’), 0)
    from dual;

    ReplyCancel

Leave a Reply to Hemanth Sharma Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

June 1, 2017 4 Comments SQL PuzzleAnvesh Patel, Complex SQL Query, database, database research and development, dbrnd, Repeated String, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Interview, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, string count, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....