T-SQL

Script to create a view in SQL Server

To add a new view in a database, use this script written in T-SQL to create a view in SQL Server. A SQL view allows you to display specific columns of an existing table. Think of a view as a virtual table defined by a SQL query selection. It is a set of columns and rows in a table. SQL Server developers create and

T-SQL

List indexes in SQL Server database with a query

How to list indexes in SQL Server database with one SQL query ? It can be very useful to display all database indexes. Indeed, to have an idea immediately at how this SQL Server database is optimized with indexes, whether an index is A clustered or a non-clustered one. T-SQL query to list all indexes in a SQL Server database vThis query offers you

T-SQL

Disable an index in SQL Server

How to disable an index in SQL Server? To disable an index for performance reasons, use this code and adjust it to your index and table names. For instance, let’s consider a large sales table, with million lines. Let’s consider that the table has a non-unique and non-clustered index on the month. Query to disable an index on a SQL Server table First, why

T-SQL

Create a SQL Server index

Create an index in SQL Server to improve the performance of queries on tables and in clustered views. Two types of indexes exists, the clustered and the nonclustered one. To create a non-clustered index, use this example and adapt it to the project requirements. Indexes are the first recommended step to performance optimization in SQL Server. Create a SQL Server non clustered index on

T-SQL

Clustered index in SQL Server

What is a SQL Server clustered index ? The definition of a SQL Server index is a structure, i.e. a file, stored physically in a disk. It’s like the master data file and the log data file that composes a database. Basically, to find the associated lines or rows faster, the index stores one or more key columns. So what’s the definition of a

T-SQL

Check if table exists in SQL Server

How to Check if table exists in SQL Server before to delete it ? How to drop it only if the existence check shows it already exists in the database to avoid errors, using the DROP TABLE keyword. Indeed, when running long SQL scripts or running scripts twice, it is much better to test the table existence. Indeed, we highly recommend testing the table

T-SQL

Update the same column of another line with SQL Server

How to update the same column in the same table of another line with SQL Server? Example of code to update the same SQL Server column on another line, on the same table. This row must be identified by another key, a simple UPDATE query of this type is not enough. Indeed, this query updates all rows with this key. A simple SQL Server

T-SQL

Split string with delimiter into columns with SQL Server

How to split a string with delimiter with XML or STRING_SPLIT functions with SQL Server ? Split a string with delimiter, like comma or semicolon, into columns with SQL Server. With a SQL Server query, it is possible to cut a string compounded of text, delimited by commas, semicolons, tabulations, dash, underscore, or even dots. It can basically be any special character that defines

T-SQL

Insert or remove SQL Server line breaks from text columns

How to insert or remove line breaks from SQL Server text columns and variables? Tutorial to insert or remove line breaks from SQL Server strings stored in NVARCHAR columns or variables. For example, if you export the string to excel, your line will be cut. Indeed, with SSMS, if you select data containing carriage return or new lines, then the screen displays the full

T-SQL

Split a string after a character with SQL Server

How to split a string after a particular character with the SQL substring function? For example, use the substring function to split a string like file.txt to separate the file name from its extension after the dot character. Indeed, it is obvious that splitting a string just before the point character must be done dynamically, i.e., whatever the position of the character is. How