Tag: database

CASE expression in a Query in SQL Server

You can use the CASE expression in a T-SQL query in SQL Server to get the same behavior of the switch statement in the programming languages. Below is a sample Query demonstrating the implementation of the CASE expression in a query in SQL Server. CASE expression in a Query in SQL Server The query associates a Department group name to a country USA and UK…

Using FETCH and OFFSET to get N records in SQL Server

In SQL Server , you can use the OFFSET and FETCH and apply paging and retreive N records at a time. OFFSET AND FETCH clause are part of the ORDER BY clause and hence you must include the ORDER BY clause. For example , you might want to skip the first 4 records and retrieve 5 records from the query , you could use the…

How to get the Size of all tables in a SQL Server ?

In SQL Server 2014 Management Studio , you can get the size of all tables in a SQL Server from the Object explorer details window. How to get the Size of all tables in a SQL Server ? 1. In SQL Management Studio 2014 , navigate to the Object explorer details screen from View -> Object explorer details menu or by simply pressing the F7…

How to disable Foreign Key constraints using T-SQL in SQL Server ?

You can enable or disable the foreign key constraints for a table in a SQL Server using the ALTER statement. How to disable Foreign Key constraints using T-SQL in SQL Server ? Here’s the T-SQL query to disable all the constraints of the table “Employee”. If you want to enable all the constraints of the table , here’s how you do it.

How to drop a table if it exists in SQL Server 2014 ?

Assume that you want to write a SQL Query in SQL Server which checks if the table exists in database and want to drop it , you can use the OBJECT_ID function to determine the table existence by passing the table name and the ‘U’ as parameters. How to drop a table if it exists in SQL Server 2014 ?

How to get the first character of a string in SQL Server ?

Assume a scenario where you have a column in your table in SQL Server database where you want to retreive the first character of the column value. How to get the first character of a string in SQL Server ? You can use the LEFT function and specify the column name as well as the number of characters from left that you want to retreive.

How to get the date part from the date time in SQL Server ?

If you are on SQL Server 2008 and higher version and want to get the date part of the datetime data in SQL Server , you can use the CONVERT method as shown in this post. How to get the date part from the date time in SQL Server ? If you want to get the current date leaving out the time from the datetime…

Exit the Current Scope with a return value in SQL Server

You can use the RETURN statement to discontinue the execution of a T-SQL batch statement or a stored procedure and provide a status code or value on return. For example , you want to display the Employees whose MaritalStatus is Divorced. You want to return a value -1 to indicate that no records exist and also You do not want the SQL Statements following it…

How to change the ‘Edit Top 200 Rows’ in SQL Management Studio ?

When working with the SQL Server Management Studio and trying to edit the table records , you would have noticed the context menu option “Edit Top 200 Rows”. How do you change this to display all the records for editing instead of “Edit top 200 rows” in the context menu ? There are couple of ways in which you can do it. 1. Right click…

How to rename a column in SQL Server ?

One of the options for the user to rename a column in SQL Server is using the so_rename stored procedure and specifying the parameters. Here’s the syntax of the sp_rename. For example , if you want to rename the column description in the temp_abundantcode table to comment , this is how it looks.

How to get all the column names of a table in SQL Server ?

If you have a scenario where you need to get all the column names of a table in SQL Server , you can query the database’s INFORMATION_SCHEMA.Columns table by specifying the table name. How to get all the column names of a table in SQL Server ? Here’s a query to demonstrate how to achieve this. This would display all the columns of the table…

How to find the duplicate records on certain fields in SQL Server ?

You can use the group by and having clause in your T-SQL query to find out the duplicate records from the table. How to find the duplicate records on certain fields in SQL Server ? Lets have a look at the query that uses the group by and having clause on the Person table in the AdventureWorks database. The firstname column is used as the…