SQL Server Date and Time related Interview Questions

SQL Server Date and Time related Interview Questions

SQL Server Date and Time related Interview Questions

This post can help you in answering SQL Server Date and Time related Interview Questions.  Below is the list of queries which are recently asked in SQL Server Developer interviews. These are not just for interview preparation also helps you as a quick reference while working on date & time related queries.

Along with these it would be good if you can refer the most commonly used DATETIME data types (DATE, TIME, SMALLDATETIME, DATETIME, DATETIME2, and DATETIMEOFFSET) and functions (GETDATE, SYSDATETIME, CONVERT, DATENAME, DATEPART, DATEDIFF, DATEADD, EOMONTH etc.)

 

Logic Explained:

When you are asked to write a query in an interview, you need not give the exact output rather you are expected to give the logic. The person who understands the base logic can easily implement a solution for any given requirement. Now let’s try to understand the logic.

Here ‘0’ indicates the starting date in the data time range. i.e : 1900-01-01 00:00:00.000

SELECT DATEADD (DD, 0, 0);

Now we’ll see how to get the first day of current week:

SELECT DATEADD (wk, DATEDIFF (wk, 0, GETDATE ()),0) AS ‘Current_Week_First_Day’

DATEDIFF (wk, 0, GETDATE()): First we are capturing the total number of weeks between the start date in date range (1900-01-01) and the current date (Based on the GTEDATE). For example today’s date is 24-Jan-17 then the difference will be (SELECT DATEDIFF (wk, 0, ‘2017-01-24’) ; 6108.

DATEADD (wk, DATEDIFF (wk, 0, GETDATE ()), 0): We have calculated the inner value as 6108. Now we simply add the number of weeks to the start date then it fetch the 6108 week starting date. i.e.: SELECT DATEADD (wk, 6108, 0); returns the week start date as “2017-01-23 00:00:00.000”

SQL Server Date and Time related Interview Questions  Download the T-SQL script file.

Posted in Interview Q&A, SQL Development, SQL Scripts | Tagged , , , , | 2 Comments

SQL Server Transactions Per Day

SQL Server Transactions Per Day

SQL Server Transactions Per Day

This post can help you in capturing SQL Server Transactions per day. Sometimes we need to capture the average number of transactions per day / hour / minute. Below is the T-SQL script that can help you to capture the SQL Server Transactions per day. Below script returns two result sets:

  1. Retrieves the total transactions occurred in SQL Server Instance since last restart
  2. Database Wise Average Transactions since last restart

T-SQL to capture SQL Server Transactions per day:

Here is the script file: SQL_Server_Transactions_Per_Day

These are the average values. To get the exact transaction details we can automate the capture process:

  • Create a table to capture the transaction counts since last restart
  • Create a stored procedure to capture the transactions since the last restart
  • Schedule a job to execute the procedure on hourly basis
  • Write a query to capture the hourly differences

 

Posted in SQL Development, SQL Scripts, SQL Server DBA | Tagged , , , , , , , , , , | 4 Comments

TSQL script to get the index size and information in SQL Server

TSQL script to get the index size and information in SQL Server

TSQL script to get the index size and information in SQL Server

TSQL script to get the index size and information in SQL Server post can help you in getting the index information from a database in SQL Server. There are two scripts provided:

Script 1: It captures all possible information of all indexes in the given database

Script 2: Captures the table wise number of indexes and total index size on a table

Script 3: Captures the index fragmentation details in the given database

TSQL script to get the index information in SQL Server:

 

TSQL script to get the index size and count in SQL Server:

 

TSQL script to get the index fragmentation details in SQL Server:

Here is the script file: TSQL script to get the index size and information in SQL Server

Posted in Performance Tuning, SQL Development | Tagged , , , , , , , , , , , | Leave a comment