Tags
Complex SQL Challenges, Complex TSQL Challenge, Interview Qs.SQL SERVER Questions, Interview Questions on SQL, InterviewQuestions, InterviewQuestions for SQL, Learn complex SQL, Learn SQL, Learn T-SQL, PL/SQL Challenges, Puzzles, Queries for SQL Interview, SQL, SQL 2012, SQL Challenge, SQL Challenges, SQL pl/sql puzzles, SQL Puzzles, SQL SERVER Interview questions, SQL SERVER2005/2008, SQL Sudoku, SQLSERVER, T SQL Puzzles, T-SQL Challenge, Tough SQL Challenges, Tough SQL Puzzles, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Queries
T-SQL Query | [The Birth Day Puzzle] – Write a query which will find the Date of Birth of employees whose birthdays lies between May 7 and May 15
Sample Input
EmpId | EmpName | BirthDate |
1 | Pawan | 04-12-1983 |
2 | Zuzu | 28-11-1986 |
3 | Parveen | 07-05-1977 |
4 | Mahesh | 13-01-1983 |
5 | Ramesh | 09-05-1983 |
Expected Output
EmpName | BirthDate |
Parveen | 07-05-1977 |
Ramesh | 09-05-1983 |
Rules/Restrictions
- The solution should be should use “SELECT” statement or “CTE”.
- Send your solution to pawankkmr@gmail.com
- Do not post you solution in comments section
Script Use the below script to generate the source table and fill them up with the sample data.
--Create table CREATE TABLEEmpBirth ( EmpId INT IDENTITY(1,1) ,EmpName VARCHAR(50) ,BirthDate DATETIME ) --Insert Data INSERT INTO EmpBirth(EmpName,BirthDate) SELECT 'Pawan' , '12/04/1983' UNION ALL SELECT 'Zuzu' , '11/28/1986' UNION ALL SELECT 'Parveen', '05/07/1977' UNION ALL SELECT 'Mahesh', '01/13/1983' UNION ALL SELECT'Ramesh', '05/09/1983' --Verify data SELECT EmpId,EmpName,BirthDate FROM EmpBirth |
Update May 14 | Solution
-- /************ SOLUTION 1 | Pawan Kumar Khowal ****************/ ;WITH CTE AS ( SELECT EmpId , EmpName,BirthDate , MONTH(BirthDate) AS Mont, DAY(BirthDate) AS days FROM EmpBirth ) SELECT EmpId , EmpName,BirthDate FROM CTE WHERE Mont = 5 AND days >= 7 AND days <= 15 /************ SOLUTION 2 | Pawan Kumar Khowal ****************/ ;WITH CTE AS ( SELECT EmpId , EmpName,BirthDate, CASE WHEN MONTH(BirthDate) < 10 THEN '0' + CAST(MONTH(BirthDate) AS VARCHAR(1)) ELSE CAST(MONTH(BirthDate) AS VARCHAR(2)) END + CASE WHEN DAY(BirthDate) < 10 THEN '0' + CAST(DAY(BirthDate) AS VARCHAR(1)) ELSE CAST(DAY(BirthDate) AS VARCHAR(2)) END FormattedDOB FROM EmpBirth ) SELECT EmpId , EmpName,BirthDate FROM CTE WHERE FormattedDOB BETWEEN '0507' AND '0515' -- |
Add a comment if you have any other solution in mind. We all need to learn.
Keep Learning
SELECT EmpId,EmpName,BirthDate FROM EmpBirth
WHERE SUBSTRING(CONVERT(VARCHAR(8),BirthDate,112),5,2) = ’05’ AND SUBSTRING(CONVERT(VARCHAR(8),BirthDate,112),7,2) BETWEEN ’07’ AND ’15’
LikeLike
select Empname,Birthdate, DATEPART(mm,BirthDate ) ‘month’,DATEPART(dd,BirthDate ) ‘date’ from EmpBirth
where DATEPART(mm,BirthDate ) = 5 and DATEPART(dd,BirthDate ) > 0 and DATEPART(dd,BirthDate ) <=31
LikeLiked by 1 person
Select EmpName, BirthDate
From EmpBirth
Where Convert(datetime, convert(varchar, month(birthdate)) + ‘/’ + Convert(varchar, day(birthdate)) + ‘/’ + ‘2017’)
Between ’05/07/2017′ and ’05/21/2017′
LikeLike
SELECT EMPName,CAST(BirthDate AS DATE) FROM EmpBirth
WHERE MONTH(BirthDate)= 05 AND DAY(BirthDate)>=7 AND DAY(BirthDate)<=15
LikeLike
SELECT EmpName
FROM EmpBirth
WHERE SUBSTRING(convert(NVARCHAR(8), BirthDate, 112), 5, 8) >= ‘0507’
AND SUBSTRING(convert(NVARCHAR(8), BirthDate, 112), 5, 8)
LikeLike
SELECT EmpName,BirthDate
FROM [StackOverflow].[dbo].[EmpBirth]
WHERE MONTH(BirthDate) = 5
LikeLike
Select Birthdate, EMPNAME from ST_EmpBirth
WHERE DATEADD( YY, datediff(Year, BIRTHDATE, getdate() ) , BIRTHDATE ) BETWEEN ‘2017-05-07’ and ‘2017-05-15’
LikeLike
SELECT EmpName, BirthDate from EmpBirth WHERE MONTH( BirthDate)=’5′ AND DAY(BirthDate) BETWEEN ‘7’ AND ’15’
LikeLike
select empname,Birthdate from empBirth where datename(m,birthdate)+’-‘+convert(nvarchar,DATEPART(d,birthdate)) >= ‘May-7′ and datename(m,birthdate)+’-‘+convert(nvarchar,DATEPART(d,birthdate))
LikeLike
; With EmpBirth_CTE as
(
select EmpId, EmpName, BirthDate
from EmpBirth
Where MONTH(birthdate) = ’05’
)
select *
from EmpBirth_CTE
Where DATEPART(dd,birthDate) between 7 and 9
LikeLiked by 1 person
select * from empbirth
where month(birthdate) = 5 and day(birthdate) between 7 and 15
LikeLike
convert(varchar, BirthDate,112)%10000 between 507 and 715
LikeLike