Tag: SQL Server
How to get the first day of the month in SQL Server ?
If you want to get the first day of the month of a input datetime variable from a SQL Query , here’s how you do it.
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 escape a single quote in SQL Server ?
You can escape a single quote in your T-SQL query in SQL Server by simply doubling them. Here’s a simple example that demonstrates how the single quote ie escaped in SQL Server. How to escape a single quote in SQL Server ?
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…
What is the equivalent of MySQL’s Now() function in SQL Server ?
MySQL has the function Now() which is a Datetime type field which shows the current date as well as the time. What is the equivalent of MySQL’s Now() function in SQL Server ? In SQL Server , you can use the GetDate() function to achieve the same.
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 Check for NULL value in a Query in SQL Server ?
You might want to check if a column contains a NULL value or NOT and filter the rows. You can use the IS NULL and IS NOT NULL keywords in SQL Server to perform the NULL check. How to Check for NULL value in a Query in SQL Server ? The NULL values cannot be easily identified using the = operator because the NULL refers…
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…
SQL Server 2014 Tutorial – Assign TCP/IP Port to Database Engine
Problem Statement You need to change the SQL Server 2014 port number from 1433 to a different port number. Solution By default , SQL Server runs on the port number 1433. You can change it to different port number by following the below steps. 1. Open SQL Server 2014 Configuration Manager from the Windows Start menu. 2. In the left side bar of the Sql…
GOTO Label in SQL Server
There are times when you want to jump to a specific section (Label) in the code when a condition satisfies. You can create a label and then use the GOTO statement to branch directly to the code. To create a label , simply specify the label name followed by the colon. Label1: Here’s a query demonstrating the usage of the Label named “RecordExistsLabel” and GOTO…
How to Update From Select Statement in SQL Server ?
Below is a sample query that demonstrates how to update a table by getting the data from the select query in SQL Server. Assume that there are 2 tables AbundantCodeEmployee and AbundantCodeDepartment which contains the id . The id(in department) in this example refers to the department head . The columns “DepartmentName” is updated in the query based on the value retreived . How to Update From…
How to remove a column from an existing table in SQL Server?
Use the DDL (Data definition language) query to remove a column from an existing table in SQL Server. How to remove a column from an existing table in SQL Server? ALTER command will allow the user to remove a column from the table. Syntax to remove the column from the table. ALTER TABLE <tablename> DROP COLUMN <columnname>; For example , assume that you have a…
How to get specific columns from a Table in SQL Query ?
If you want to get the data from the specific columns from a table in your database , you can specify them in the query. For example, This would return the data of all the columns available in the Department table. If you want to get the data of the specific columns from the Department table , you can specify the column names in the…
NULLIF function in SQL Server
The NULLIF function in SQL Server returns a NULL value if the two input expressions return the same value else the first value (expression) is returned. For example , assume that you want to compare two columns (Address1 and Address2) in the Address table and want to return NULL if both are same to indicate there is NO change and if the addresses are different…
How to get the table structure in SQL Server ?
To get the details of the table structure along with the indexes and the constraints , you can use the sp_help command as demonstrated in this blog post. How to get the table structure in SQL Server ?
UPDATE from SELECT query in SQL Server
Assume a scenario where you have a temporary table contains the data and would like to update a table using the values from the temporary table. Here’s an query demonstrating how to do it in SQL Server.
How to display Row Numbers in the results in SQL Query ?
If you want to return the row number as a part of your results in the SQL Query , you can use the ROW_NUMBER function and specify the ORDER BY clause to it as shown in this blog post. How to display Row Numbers in the results in SQL Query ?
How to Add a Column with Default Value to Existing Table in SQL Server ?
Looking to add a column with default value to an existing table in SQL Server ? Below is an example and sample Data Definition Language Query for achieving the same. How to Add a Column with Default Value to Existing Table in SQL Server ? Assume that the table name is “ACEmployee” which already exists and you want to add a new column called “Status”…
SQL Server 2014 Tutorial – Open SQL Server Instance Port in Windows Firewall
Problem Statement You need open the SQL Server Instance port number in Windows Firewall so that you can connect to SQL Server from another machine. Solution When you try to connect to the SQL Server instance from another machine , you might receive the connection timeout error . To allow the users of other machine to connect to your SQL Server instance , you need…