DATETIME : SQL SERVER QUERIES INTERVIEW FOR EXPERIENCED 2-3 YEARS WITH EXAMPLE : SET-3
SQL DATETIME QUERIES INTERVIEW QUESTIONS WITH ANSWERS
TOP 10 DATETIME RELATED INTERVIEW QUERIES
If you think this set contain easy question then you can start with Set 9 (tricky quries)and be continue.
This is the 3rd set(21-30) of SQL INTERVIEW QUERIES SET, This set is for experienced developers, it's containing datetime related interview question with answers, So test your sql skill by answers following queries by yourself first.
So lets take it as a SQL Queries Quiz
21). Get only Year part of "JoiningDate".So lets take it as a SQL Queries Quiz
Ans: SELECT DATEPART(YEAR, JoiningDate) FROM [EmployeeDetail]
22). Get only Month part of "JoiningDate".
Ans: SELECT DATEPART(MONTH,JoiningDate) FROM [EmployeeDetail]
23). Get system date.
Ans: SELECT GETDATE()
24). Get UTC date.
Ans: SELECT GETUTCDATE()
25). Get the first name, current date, joiningdate and diff between current date and joining date in months.
Ans: SELECT FirstName, GETDATE() [Current Date], JoiningDate,
DATEDIFF(MM,JoiningDate,GETDATE()) AS [Total Months] FROM [EmployeeDetail]
26). Get the first name, current date, joiningdate and diff between current date and joining date in days.
Ans: SELECT FirstName, GETDATE() [Current Date], JoiningDate,
DATEDIFF(DD,JoiningDate,GETDATE()) AS [Total Months] FROM [EmployeeDetail]
27). Get all employee details from EmployeeDetail table whose joining year is 2013.
Ans: SELECT * FROM [EmployeeDetail] WHERE DATEPART(YYYY,JoiningDate) = '2013'
28). Get all employee details from EmployeeDetail table whose joining month is Jan(1).
Ans: SELECT * FROM [EmployeeDetail] WHERE DATEPART(MM,JoiningDate) = '1'
29). Get all employee details from EmployeeDetail table whose joining date between "2013-01-01" and "2013-12-01".
Ans: SELECT * FROM [EmployeeDetail] WHERE JoiningDate BETWEEN '2013-01-01' AND '2013-12-01'
30). Get how many employee exist in "EmployeeDetail" table.
Ans: SELECT COUNT(*) FROM [EmployeeDetail]
Click on the following link for NEXT SET OF QUESTIONS:
CLICK HERE FOR NEST SET (MORE THAN 600 QUERIES)
question no. 21 and 22 is not showing the output. It is showing out of range value.
ReplyDeleteQuestion no.21 Try this: SELECT DATEPART(YYYY,JoiningDate) FROM [EmployeeDetail]
DeleteQuestion no.22 Try this: SELECT DATEPART(MM,JoiningDate) FROM [EmployeeDetail]
Can u share screen shot of ur result at ahlawat.vikas24@gmail
ReplyDeleteNice work. Appreciate
ReplyDeletenice
ReplyDeletevery nice...useful for beginners
ReplyDelete#26: Column Name in results should be "Total Days" (not Months)
ReplyDeletehow to get three months before joining employee record
ReplyDeletein employeedetail table how to insert joining date format...can any one answer..
ReplyDeleteselect datediff(mm,joining_date,getdate()) from table_name where datediff(mm,joining_date,getdate()) < 3
ReplyDeleteIn question 21 and 22, datepart function used as Datepart(YEAR, Joiningdate),Datepart(Month, Joiningdate). But month and Year can be used with DateName like Datename(Month, Joiningdate) and Datename(Year, Joiningdate). Datename returns string and datepart returns int.
ReplyDeleteUseful content for beginners
ReplyDelete