Tags
Advanced SQL Interview Questions and Answers, Advanced SQL tutorial pdf, any recommended websites for sql puzzles, Best SQL Puzzles, Buy SQL Server Interview Questions Book Online at Low Price, COmples tSQL puzzles, Complex SQL Challenges, complex sql statement(puzzle), Complex tsql, Complex TSQL Challenge, convert string to proper case in sql server, Divide rows into two columns, Download SQL Questions, Download SQL Server Interview Question in PDF, Download SQL SERVER Interview questions, Download SQL Server Interview questions and answers, download sql server interview questions and answers pdf, download sql server interview questions by Pawan Khowal, download sql server interview questions by Pawan Kumar, download sql server interview questions by Pawan Kumar Khowal, Download T-SQL Interview Questions, Find if a string contains number sequences, Free Download SQL SERVER Interview questions, Get Next Value Puzzle, Huge blank areas in database field, I'm trying to come up with a query that can determine if there is a number sequence of 3 or more numbers within a string (varchar). This is for SQL Server, Improve SQL Skills, Interview Puzzles in SQL Server, Interview Qs.SQL SERVER Questions, Interview questions and Answers for MS SQL Server designing, Interview Questions and Answers For SQL, Interview questions on Joins, Interview Questions on SQL, Interview SQL Puzzles, InterviewQuestions, InterviewQuestions for SQL, Learn complex SQL, Learn SQL, Learn T-SQL, Microsoft SQL Server interview questions for DBA, MS SQL Server interview questions, MSBI Interview Questions, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, Puzzle SQL, puzzle sql developer, Puzzle SQl Server, Puzzles, PUzzles in SQL SERVER, Queries for SQL Interview, Remove Huge Multiple Spaces from a DB Fields, Remove Multiple Spaces from a DB Fields, Separate Int and char from a string column, Single Quote update SQL, SQL, SQL - The Pattern Puzzles, SQL 2012, SQL 2014, SQL 2014 Interview Questions, SQL Challenge, SQL Challenges, SQL Extreme Difficult Puzzle, SQL FAQ, SQL FAQs, sql group by only rows which are in sequence, SQL Interview Q & A, SQL Interview Questions, SQL Interview Questions - Part 2, SQL Interview Questions for SQL Professionals, SQL Joins, SQL Pattern Puzzles, SQL pl/sql puzzles, SQL prime number puzzle, SQL puzzle, SQL Puzzles, sql puzzles & answers, sql puzzles and answers free download, sql puzzles and answers pdf, sql puzzles for interview, sql puzzles oracle, SQL Queries, SQL Queries asked in interviews, SQL QUERY PUZZLES, SQL Query to Find Nth Highest Salary of Employee, SQL Questions, SQL Quiz, SQL Replace Puzzle, SQL Server - Common Interview Questions and Answers, SQL SERVER - Find Nth Highest Salary of Employee, SQL Server - General Interview Questions and Answers, sql server 2008 interview questions, SQL Server 2008 Interview Questions and Answers, SQL Server Database, SQL Server database developer interview questions and answers, sql server dba interview questions, SQL Server developer Interview questions and answers, SQL Server developer Interview questions with answers, SQL Server Developer T-SQL Interview Questions, SQL server filter index with LIKE and RIGHT function?, SQL Server Interview Puzzle, SQL SERVER Interview questions, SQL SERVER Interview questions & Answers, SQL Server Interview Questions - Part 1, SQL Server Interview questions and answers, SQL Server Interview Questions and Answers - Free PDF, SQL SERVER Interview questions and answers for experienced, sql server interview questions and answers for net developers, SQL Server Interview Questions And Answers.pdf, sql server interview questions by Pawan Khowal sql interview questions, SQL SERVER Interview questions pdf, SQL Server Interview Questions | MSBISKILLS.Com, SQL Server Interview Questions | MSBISKILLS.com Top 50 SQL Server Questions & Answers, SQL Server Interview Questions/Answers Part-1, SQL Server Puzzle, SQL SERVER Puzzles, SQL server Questions and Answers, SQL SERVER Tips, SQL Skills, SQL Sudoku, SQL Tips & Tricks, SQL Tips and Tricks, SQL Top clause, SQL Tough Puzzle, SQL Tricks, SQL Trikcy question, sql/database interview for puzzle sql developer, SQLBI Interview Questions, SQLSERVER, SUM of grouped COUNT in SQL Query, T SQL Puzzles, T-SQL Challenge, T-SQL Interview Questions | SQL Server Interviews and Jobs, T-SQL Puzzle, T-SQL Server Interview Questions, T-SQL Tricky Puzzles, The Status Puzzle, Top 10 Frequently Asked SQL Query Interview Questions, TOP 100 SQL SERVER INTERVIEW QUESTIONS QUERIES, Top 50 SQL Server Interview Question for Testers, Tough SQL Challenges, Tough SQL Puzzles, Tricky Questions, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Puzzle, TSQL Puzzles, TSQL Queries
SQL Puzzle | Strings containing 3 consecutive numbers in increasing order Puzzle | SQL Interview Question
In this puzzle you have to find strings which contains number sequences. E.g. If you have a string like ‘1231786’ so in this string you have 123 in sequence so we need this row in the output otherwise we will exclude that row from the output. Also note that we will only have string of numbers only. Alphanumeric characters will not be there.
Please check the sample input and the expected output.
Sample Input
Vals |
---|
1234 |
1112 |
1 |
1034 |
NULL |
Expected Output
VALS |
---|
1234 |
Use below script to create table and insert sample data into it.
-- CREATE TABLE CONSECUTIVE ( Vals VARCHAR(100) ) GO INSERT INTO CONSECUTIVE VALUES (1234), (1112), (1), (1034), (NULL) GO SELECT * FROM CONSECUTIVE GO -- |
Rules/Restrictions
The solution should be should use “SELECT” statement or “CTE”.
Add your solution(s) in the comments section or send you solution(s) to pawankkmr@gmail.com
1 SOLUTION
SOLUTION – 1
-- /* In this solution we have created a new table called [pos] where we are storing all the combinations of 3 digits in consecutive numbers in increasing order*/ CREATE TABLE [dbo].[pos] ( [opt] [int] NULL ) GO INSERT [dbo].[pos] ([opt]) VALUES (123) GO INSERT [dbo].[pos] ([opt]) VALUES (234) GO INSERT [dbo].[pos] ([opt]) VALUES (345) GO INSERT [dbo].[pos] ([opt]) VALUES (456) GO INSERT [dbo].[pos] ([opt]) VALUES (567) GO INSERT [dbo].[pos] ([opt]) VALUES (678) GO INSERT [dbo].[pos] ([opt]) VALUES (789) GO /*After that we just have to JOIN this table with the input table to match the things*/ SELECT VALS from consecutive c CROSS JOIN pos WHERE CHARINDEX(CAST(opt AS VARCHAR(50)),vals) > 0 GROUP BY VALS -- |
Output-1
-- VALS ---------------- 1234 (1 row affected) -- |
SOLUTION – 2 | Using Recursion – Solution BY Ashu S
-- ;With cte as ( select vals as a, vals as b from consecutive union all select a, SUBSTRING(b,2,len(b)-1) from cte where len(b)>3 ) select distinct a from cte where cast(cast(b as int)- cast (replicate(substring(b,1,1),len(b)) as int) as varchar(max)) like '12%' -- |
Output-2
-- a ------------------ 1234 (1 row affected) -- |
Add a comment if you have any other or better solution in mind. I would love to learn it. We all need to learn.
Pawan Khowal
Pawan is a SQL Server Developer. If you need any help in writing code/puzzle or training please email at – pawankkmr”AT”gmail.com. Meanwhile please go throgh the top pages from his blog.
Page Detail | URL |
---|---|
☛ SQL Advance Puzzles | https://msbiskills.com/tsql-puzzles-asked-in-interview-over-the-years/ |
☛ SQL Tricky Queries | https://msbiskills.com/sql-puzzles-finding-outputs/ |
☛ SQL Server Perfomance tuning Articles | https://msbiskills.com/sql-performance-tuning/ |
☛ SQL Server Articles | https://msbiskills.com/t-sql/ |
☛ SQL Interview Questions & Answers | https://msbiskills.com/sql-server-interview-questions/ |
My SQL Groups on Facebook:
1. If you like this post, you may want to join my SQL SERVER Interview Puzzles/Interview Questions on Facebook: https://www.facebook.com/groups/1430882120556342/
2. If you like this post, you may want to join my SQL Server Puzzles on Facebook:
https://www.facebook.com/groups/206594023090781/
My SQL Page on Facebook:
2. For all the updates you may follow my page -> https://www.facebook.com/MSBISkillscom-1602779883299222/
Enjoy !!! Keep Learning
Http://MsbiSkills.com
Nice Puzzle. I have a more generic solution where it picks up any consecutive sequence more than 3 in a Number
DECLARE @Nums TABLE(num BIGINT)
INSERT INTO @Nums VALUES
(3561234),(346789),(1356789),(13579),(7945678), (54961),
(1234),(1112),(1),(1034),(NULL),(56123784),(123456789)
;WITH T AS
(
SELECT i=1,st=Cast(NUM as varchar(10)), j=substring(Cast(NUM as varchar(10)),1,1),Num FROM @Nums
UNION ALL
SELECT i=i+1, st, j=substring(st,i+1,1),Num FROM T
WHERE i1 AND col = (SELECT Max(col) FROM
T2 c WHERE c.num =b.num and c.num =a.num) FOR XML path(”)) NumSubset
FROM @Nums a
)
SELECT NUM Vals,NumSubset Consecutive_Sub_Val FROM T3 WHERE CHARINDEX(NumSubset,NUM)>0 and len(NumSubset)>2
LikeLike
it gives the following results
Vals Consecutive_Sub_Val
——————– ———————-
3561234 1234
346789 6789
1356789 56789
7945678 45678
1234 1234
56123784 123
123456789 123456789
LikeLike
Vals Consecutive_Sub_Val
——————– ———————-
3561234 1234
346789 6789
1356789 56789
7945678 45678
1234 1234
56123784 123
123456789 123456789
LikeLike
sorry this TextArea Control is messing up the text format when I was trying to post the results
LikeLike