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 October SQL Puzzle: SQL Advance Query – Split the String and generate count of Each part

SQL Puzzle: SQL Advance Query – Split the String and generate count of Each part

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

Check the below input data and expected output to split the string and generate a count of each string. Split the string by semi colon.

Input Data:

1
2
3
4
5
6
str1
--------------------
anvesh;dbrnd;blog
dbrnd;blog
dbrnd
blog;anvesh

Expected Output:

1
2
3
4
5
StringName StringCounts
-------------------- ------------
anvesh 2
blog 3
dbrnd 3

Create a table with data:

1
2
3
4
5
6
7
8
9
CREATE TABLE tbl_SplitData
(
str1 VARCHAR(20)
)
GO
INSERT INTO tbl_SplitData (str1)
VALUES ('anvesh;dbrnd;blog'),('dbrnd;blog'),('dbrnd'),('blog;anvesh')
GO

Solution 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
SELECT
StringName
,COUNT(*) StringCounts
FROM
(
SELECT CAST(CONCAT('' , REPLACE(str1, ';' , '' ) , '' ) AS XML) Xmlcol FROM tbl_SplitData
) s
CROSS apply
(
SELECT ProjectData.D.value('.', 'VARCHAR(20)') as StringName
FROM s.xmlcol.nodes('t') as ProjectData(D)
)t
GROUP BY t.StringName

Please try the different solution for this puzzle and share it via comment...

Oct 21, 2017Anvesh Patel
SQL Puzzle: SQL Advance Query - Use STUFF() and form the aggregate of columnsSQL Puzzle: SQL Advance Query - Check number is an Integer or not an Integer
Comments: 5
  1. Dinesh.IS
    October 24, 2017 at 4:55 am

    Declare @SplitDetails Table(StringValue VarChar(5000));
    Declare @StringValue VarChar(5000),@Len As Int
    Declare Cur Cursor For Select Str1 From TBL_SplitData
    Open Cur
    Fetch Next From Cur Into @StringValue
    While @@Fetch_Status=0
    Begin

    While Len(@StringValue)>0
    Begin

    Set @Len=(Case CharIndex(‘;’,@StringValue) When 0 Then Len(@StringValue) Else CharIndex(‘;’,@StringValue)-1 End)

    Insert Into @SplitDetails
    (StringValue)
    Values(SubString(@StringValue,1,@Len))

    Set @StringValue=(Case Len(@StringValue)-@Len When 0 Then Null Else Right(@StringValue,Len(@StringValue)-@Len-1) End)
    End
    Fetch Next From Cur Into @StringValue
    End
    CLose Cur
    Deallocate Cur

    Select StringValue,Count(*) As StringCounts From @SplitDetails
    Group By StringValue

    ReplyCancel
  2. Dinesh.IS
    October 24, 2017 at 4:55 am

    Declare @SplitDetails Table(StringValue VarChar(5000));
    Declare @StringValue VarChar(5000)=”,@Len As Int

    Select @StringValue+=’;’+Str1 From (Select Str1 From TBL_SplitData ) A
    Set @StringValue=STUFF(@StringValue,1,1,”)

    While Len(@StringValue)>0
    Begin

    Set @Len=(Case CharIndex(‘;’,@StringValue) When 0 Then Len(@StringValue) Else CharIndex(‘;’,@StringValue)-1 End)

    Insert Into @SplitDetails
    (StringValue)
    Values(SubString(@StringValue,1,@Len))

    Set @StringValue=(Case Len(@StringValue)-@Len When 0 Then Null Else Right(@StringValue,Len(@StringValue)-@Len-1) End)

    End

    Select StringValue,Count(*) As StringCounts From @SplitDetails
    Group By StringValue

    ReplyCancel
    • ankush g
      September 12, 2018 at 10:27 am

      Select NAME, count(NAME) from (
      Select DISTINCT
      trim(regexp_substr(str1, ‘[^;]+’, 1, level)) Name, level
      FROM tbl_SplitData
      connect by regexp_substr(name, ‘[^;]+’, 1, level) is not null)foo
      GROUP BY NAME

      ReplyCancel
  3. Ankush
    September 12, 2018 at 10:25 am

    What do u say about this?

    Select NAME, count(NAME) from (
    Select DISTINCT
    trim(regexp_substr(str1, ‘[^;]+’, 1, level)) Name, level
    FROM tbl_SplitData
    connect by regexp_substr(name, ‘[^;]+’, 1, level) is not null)foo
    GROUP BY NAME

    ReplyCancel
  4. Ankush
    September 12, 2018 at 10:26 am

    Select NAME, count(NAME) from (
    Select DISTINCT
    trim(regexp_substr(str1, ‘[^;]+’, 1, level)) Name, level
    FROM tbl_SplitData
    connect by regexp_substr(name, ‘[^;]+’, 1, level) is not null)foo
    GROUP BY NAME

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

October 21, 2017 5 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks, string
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....