In this article, we will briefly explain the SUBSTRING function and then focus on performance tips about it.
SQL Server offers various built-in functions and these functions make complicated calculations easier for us. When we use these functions in the SELECT statement, performance impacts are mostly acceptable. However, scalar functions can affect query performances negatively when it uses after the WHERE clauses. The following rule is generally accepted as a performance practice to improve query performances.
The main idea behind this principle is, SQL Server can not know the result of the function without executing the scalar function. Therefore, SQL Server must perform the function individually for each row to find qualified data on the execution time. So, the data engine will read the entire index pages or the entire table rows so it causes more I/O activity.
SUBSTRING function is one of the built-in function and that helps to obtain a particular character data of the text data in the queries. This function is widely used by database developers in the queries, for this reason, we will focus on performance details of this function.
This is the syntax of the Substring() function – SUBSTRING(string, start, length)
Now we will make a very straightforward example :
SELECT SUBSTRING('SAVE THE GREEN',6,3)
The following illustration represents how this function works for the above example.
This function also can be implemented with the SELECT statement that retrieves data from the tables. The following query returns a certain part of the PurchaseOrderNumber column values. In this usage of the SUBSTRING function, it starts to extract from the second character of the column values and continues until the seventh character. So that it extracts five characters as we specified in the length parameter of the function.
SELECT PurchaseOrderNumber, SUBSTRING(PurchaseOrderNumber, 2, 5) AS NewPOrderNumber FROM Sales.SalesOrderHeader WHERE SalesOrderID BETWEEN 43682 AND 43694
#development #execution plans #sql commands #t-sql #sql
SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.
Models for SQL exist. In any case, the SQL that can be utilized on every last one of the major RDBMS today is in various flavors. This is because of two reasons:
1. The SQL order standard is genuinely intricate, and it isn’t handy to actualize the whole standard.
2. Every database seller needs an approach to separate its item from others.
Right now, contrasts are noted where fitting.
#programming books #beginning sql pdf #commands sql #download free sql full book pdf #introduction to sql pdf #introduction to sql ppt #introduction to sql #practical sql pdf #sql commands pdf with examples free download #sql commands #sql free bool download #sql guide #sql language #sql pdf #sql ppt #sql programming language #sql tutorial for beginners #sql tutorial pdf #sql #structured query language pdf #structured query language ppt #structured query language
When working in the SQL Server, we may have to check some other databases other than the current one which we are working. In that scenario we may not be sure that does we have access to those Databases?. In this article we discuss the list of databases that are available for the current logged user in SQL Server
#sql server #available databases for current user #check database has access #list of available database #sql #sql query #sql server database #sql tips #sql tips and tricks #tips
This article will introduce the concept of SQL recursive. Recursive CTE is a really cool. We will see that it can often simplify our code, and avoid a cascade of SQL queries!
The recursive queries are used to query hierarchical data. It avoids a cascade of SQL queries, you can only do one query to retrieve the hierarchical data.
First, what is a CTE? A CTE (Common Table Expression) is a temporary named result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. For example, you can use CTE when, in a query, you will use the same subquery more than once.
A recursive CTE is one having a subquery that refers to its own name!
Recursive CTE is defined in the SQL standard.
A recursive CTE has this structure:
In this example, we use hierarchical data. Each row can have zero or one parent. And it parent can also have a parent etc.
Create table test (id integer, parent_id integer); insert into test (id, parent_id) values (1, null); insert into test (id, parent_id) values (11, 1); insert into test (id, parent_id) values (111, 11); insert into test (id, parent_id) values (112, 11); insert into test (id, parent_id) values (12, 1); insert into test (id, parent_id) values (121, 12);
For example, the row with id 111 has as ancestors: 11 and 1.
Before knowing the recursive CTE, I was doing several queries to get all the ancestors of a row.
For example, to retrieve all the ancestors of the row with id 111.
While (has parent) Select id, parent_id from test where id = X
With recursive CTE, we can retrieve all ancestors of a row with only one SQL query :)
WITH RECURSIVE cte_test AS ( SELECT id, parent_id FROM test WHERE id = 111 UNION SELECT test.id, test.parent_id FROM test JOIN cte_test ON cte_test.id = test.parent_id
) SELECT * FROM cte_test
It indicates we will make recursive
It is the initial query.
It is the recursive expression! We make a jointure with the current CTE!
Replay this example here
#sql #database #sql-server #sql-injection #writing-sql-queries #sql-beginner-tips #better-sql-querying-tips #sql-top-story
There is no doubt that writing code is more art than science and every coder cannot write beautiful code which is both readable and maintainable, even with the experience. Yes, it’s blunt and hard but it’s mostly true.
In general, coding improves with experience but only when you learn the art of coding like favoring composition over inheritance or coding for interface than implementation, but, unfortunately only a few developers able to master these techniques.
The same applies to SQL queries. The way you structure your query, the way you write it goes a long way to communicate your intent to the fellow developer, DBA, and even yourself after a few months.
Whenever I see SQL queries on emails from different developers, I can see the stark difference in their writing style. Some developers and DBAs write it so neatly and indent their query such that you can easily spot key details like which columns you are extracting, and from which table, and what are joining or filtering conditions.
Since in real-life projects, SQL queries are hardly one-liner, learning the right way to write complex SQL queries makes a lot of difference; especially when you share that query to someone for review or execution. It also helps when you read it yourself later as I said, after a few months.
The problem is there are many books and courses to teach you SQL like what is a table, different SQL commands but there are very few (like The Complete SQL Bootcamp by Josh Portilla) which focus on writing proper SQL queries.
In this article, I am going to show you a couple of styles which I have tried in the past, their pros and cons and what I think is the best way to write SQL queries.
Unless you have a good reason not to use my style e.g. you have a better style or you want to stick with the style used in your project (consistency overrules everything) there is no reason not to use it.
By the way, I expect that you are familiar with SQL and definitely know how to write queries. I expect that you have used different SQL clauses like SELECT, INSERT, UPDATE, DELETE and understand their meaning in a SQL query. If you are not, it’s better you gain some experience with SQL by joining some of my recommended courses like:
Learn SQL by CodeCademy
Introduction to SQL by Jon Flanders
The Complete SQL Bootcamp by Josh Portilla, a Data Scientist, on Udemy or
SQL for Newbs: Data Analysis for Beginners by David Kim and Peter Sefton’s course on Udemy.
They all are great courses and teach you SQL basics, but, if you need some free alternatives you can also check out this list of free SQL courses for programmers and developers.
#database #sql #mysql #oracle #tips #sql server #sql (structured query language) #intro to sql #sql bootcamp
In this article, We will discuss how to backup our database in MS-SQL Server using T-SQL Statements.
We need to use **_BACKUP DATABASE _**statement to create full database backup, along with the name of Database and device to store the backup file.
#sql server #backup database #backup database using t-sql #sql query #sql tips #t-sql #tips