Tags
A puzzle, A quick query puzzle, A SQL Puzzle, A SQL Server Puzzle, Complex SQL Challenges, complex sql statement(puzzle), Complex TSQL Challenge, Interesting Interview Questions, Interview Qs.SQL SERVER Questions, Interview questions on Joins, Interview Questions on SQL, InterviewQuestions, InterviewQuestions for SQL, Joins, Joins Interview questions, Joins Puzzle, Khowal, Learn complex SQL, Learn SQL, Learn T-SQL, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, puzzle sql developer, Puzzles, Puzzles in SQL, PUzzles in SQL SERVER, Queries for SQL Interview, SELECT, SELECT Puzzle, Some useful SQL puzzles to teach SQL, SQL, SQL 2012, SQL 2014, SQL 2014 Interview Questions, SQL Challenge, SQL Challenges, SQL Interview Questions, SQL Joins, SQL pl/sql puzzles, SQL puzzle, SQL Puzzles, SQL Queries, SQL QUERY PUZZLES, SQL Quiz, SQL Server Database, SQL SERVER Interview questions, SQL Server Puzzle, SQL SERVER Puzzles, SQL Skills, SQL Sudoku, SQL Top clause, SQL Tricky Puzzles, SQL Trikcy question, sql/database interview for puzzle sql developer, SQLSERVER, T SQL Puzzles, T-SQL Challenge, The Biggest Gap Puzzle, The Gap Puzzle, The GroupBy Puzzle, Tough SQL Challenges, Tough SQL Puzzles, Tricky Puzzles in SQL, tricky puzzles in SQL Server, Tricky Questions, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Puzzles, TSQL Queries, TSQLPuzzles, Week puzzle
T-SQL Output Query | [ The String & Null Puzzle ]
Please go through the sample queries involved substring and reasons behind the outputs and errors..
-- DECLARE @str AS VARCHAR(50) SET @str = 'Pawan Kumar - A Developer' SELECT SUBSTRING(@str, NULL, 5 ) --O/P - NULL --If you provide null in place of starting location , it will return NULL. SELECT SUBSTRING(@str, -1, 5 ) --O/P - Paw --Here we are starting from -1, So it will skip -1,0 location and Start from first location till 3rd position since we don't have characters at those locations SELECT SUBSTRING(@str, 6, 5 ) --O/P - Kuma --Here we are starting from -1, So it will Start from 6th location and will pick 5 characters SELECT SUBSTRING(@str, NULL, NULL ) --O/P - NULL --If everything is NULL, then it will return NULL 🙂 SELECT SUBSTRING(@str, 0, 5 ) --O/P - Pawa --Here we are starting from 0, So will Start from first location till 5th position will pick 4 characters only. SELECT SUBSTRING(@str, NULL, -1 ) --O/P Msg 536, Level 16, State 1, Line 11 Invalid length parameter passed to the substring function. --
Cheers, Keep Learning !!
Pawan Kumar Khowal
MSBISKills.com