Category Archives: SQL Basic

Script to find complete months between two given dates

By | July 10, 2015

Complete months between two given dates Below script will help in finding the complete months within the 2 given dates. In the below script we takes below dates as exampleStartdate =2015-07-05 Enddate =2016-01-06 /**********************Script Start**************************/ IF (object_id(‘tempdb..#month’) is not null) DROP TABLE #month CREATE TABLE #month (id int identity(1,1),MonthStartDate DATETIME,MonthEndDate DATETIME) DECLARE @startdate AS DATETIME… Read More »

Script to find the complete weeks within two given dates

By | July 10, 2015

Complete weeks within two given dates Sometimes we need to find out the complete weeks within two given dates. Below script will help in finding the complete weeks within 2 given dates. In this script, I have used below dates as exampleStartdate=2015-04-12 Enddate=2015-06-10 SET DATEFORMAT YMD if (object_id(‘tempdb..#weekdays’) is not null) drop table #weekdays Declare… Read More »

RIGHT() function in SQL Server

By | January 26, 2015

SQL Server – RIGHT () function Right():- This function returns the specified number of characters from the Right part of the given character string.Syntax:- Select RIGHT ( character_expression , integer_expression ) For example, the below query will return two characters from the last. Select RIGHT(‘Vivek’,2) Result ek

LEFT() function in SQL Server

By | January 26, 2015

LEFT() function in SQL Server Left():- This function returns the specified number of characters from the left part of the given character string. Syntax:-Select LEFT ( character_expression , integer_expression ) For example, the below query will return two characters from the beginning Select LEFT  (‘Vivek’,2) Result Vi

SQL Server – Convert() function

By | January 24, 2015

Convert () function in SQL Server Convert () function is used to convert an expression from one data type to another data typeSyntax for CONVERT function CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) Convert functions is usually used with date/time data type to convert a date in different formats. Below examples will… Read More »

Group by…..Having Clause

By | January 31, 2010

Group By:- Group By clauses is used to groups rows based on the distinct values of the specified columns. The syntax of the Group by clause is:-   Select column1, column2, column3, aggregate_function(expression )            From TableName Where (condition)            Group By Column1, column2, column3 For Example, suppose we have a… Read More »