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 Puzzle: Advance SQL Query – Convert single cell of table into multiple Rows

SQL Puzzle: Advance SQL Query – Convert single cell of table into multiple Rows

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

Check the below input and require output, and convert single cell of a table into multiple Rows.

Input:

1
2
3
string
----------
aBCDe14885

Output:

1
2
3
4
5
6
7
8
9
10
11
12
string
------
a
B
C
D
e
1
4
8
8
5

Solution 1:

1
2
3
4
5
6
7
8
9
10
11
DECLARE @Var VARCHAR(10) = 'aBCDe14885'
GO
;with cte as
(
select 1 as rnk, substring(@Var,1,1) as string
union all
select rnk +1 as rnk, substring(@Var,rnk+1,1) as string
from cte where rnk < datalength(@Var)
)
select string from cte
GO

Solution 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
DECLARE @Var VARCHAR(10) = 'aBCDe14885'
GO
;with cte as
(
select
substring(@Var, 1, 1) as string
,substring(@Var, 2, len(@Var)) as data
,1 as rnk
union all
select
substring(data, 1, 1) as string
,substring(data, 2, len(data)) as data
,rnk + 1 as rnk
from cte
where len(data) > 0
)
select string from cte
GO

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

Jun 14, 2017Anvesh Patel
PostgreSQL: Find a list of active Temp tables with Size and User informationGreenplum: Script to find Role information with associated Resource Queue
Comments: 2
  1. Dinesh
    June 15, 2017 at 9:38 am

    Hi Sir

    I tried this below methods

    –Method-1
    DECLARE @Var VARCHAR(10) = ‘aBCDe14885′
    Declare @TableResult Table(Value VarChar(50));
    Declare @Len Int,@Cnt As Int
    Set @Cnt=0
    While Len(@Var)>0
    Begin

    Insert Into @TableResult
    Values(SubString(@Var,1,1))

    Set @Var=Right(@Var,Len(@Var)-1)

    End
    Select * From @TableResult

    GO
    –Method-2
    Declare @Var VarChar(10)=’aBCDe14885’
    Declare @TableResult Table(Value VarChar(50));
    Declare @Len Int,@Cnt Int
    Set @Cnt=1
    Set @Len=Len(@Var)
    While (1=1)
    Begin

    If @Cnt>@Len
    Begin
    Break;
    End
    Else
    Begin
    Insert Into @TableResult
    Values(SubString(@Var,@Cnt,1))
    Set @Cnt=@Cnt+1
    End
    End
    Select * From @TableResult
    GO

    Regards
    Dinesh.

    ReplyCancel
  2. Andrius
    October 23, 2019 at 3:44 am

    Postgres:
    SELECT regexp_split_to_table(‘aBCDe14885’, ”);

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

June 14, 2017 2 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Puzzle, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, 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....