Author : Gopal Krishna Ranjan


Gopal Krishna Ranjan

About Gopal Krishna Ranjan

I am Gopal Krishna Ranjan, having 6+ years of industry experience in Software development using Microsoft technologies. I have a head down experience in Database development, performance tuning in SQL Server, T-SQL optimization, BI (Business Intelligence) project implementation, reporting in SSRS, using SSIS for ETL, implementing multi dimensional and tabular data-warehouse in SSAS, querying cubes using MDX and DAX, Windows and Web Applications development with C#.


Different data types in case expression 5

In this blog post titled as “Different data types in case expression”, we will explore the behavior of CASE expression when dealing with different data types in THEN part (true) and ELSE part (false) of CASE expression. We will limit the scope of discussion of this post to data conversion issue only. If you are interested to explore more about the CASE expression, visit this MSDN link.

What is the Return type of CASE expression

As per MSDN, CASE expression returns the highest precedence type from the given set of types in THEN and ELSE part (true and false part). To explore data type precedence, you can visit Data Type Precedence link here.

Lets play with a simple demo to see this return type in action. This demo is for test and may look a bit silly. In below SQL query, we have two variables, @RetValThen as DATETIME … More


Unique Key is Clustered or Nonclustered 2

An easy question for you “Unique Key is Clustered or Nonclustered”? Lets explain in more detail, When I create a Unique key on a table without explicitly defining the type of index(clustered or nonclustered), which type of index will be used to accommodate the key?

I have already written a blog on behavior of primary key for clustered and nonclustered index type selection in such kind of scenarios and here is the link. In this post titled as “Unique Key is Clustered or Nonclustered”, we will explore the behavior of Unique Key in regards of Clustered and Nonclustered index type selection. Before going forward, I would like to focus on the differences between Primary Key and Uniquer Key below.

Difference between Primary Key and Unique Key

 Primary key

  1. It is a column or set of columns, used to uniquely identify each row of the given table.
  2. NULL values
More

Rename Table and Change Schema with SP_RENAME

In this post “SP_RENAME table with schema change” I will share a trick to rename a table using sp_rename and then transfer it schema. As we know that we can use SP_RENAME system stored procedure to rename user created objects like tables, procedures, functions, views, indexes, columns, user defined types, CLR user defined types etc in current database.

For example, if you have a table with name “TestTableA” in schema “dbo” and want to change its name to “TestTableB”, you can use below command;

SP_RENAME 'dbo.TestTableA', 'TestTableB'

But in case you want to change the table name with schema change using procedure “sp_rename”, putting “Schemaname.Tablename” as new name in second parameter, will not work. What will happen if we put “Schemaname.Tablename” as new name and why it will happen, I have explained it in next section of this blog post.

Schema.TableName as New Name in sp_rename will not work

In … More


NOLOCK table hint 9

A lot of people use NOLOCK table hint in their SELECT queries, some of them are expert and well aware of its pros and cons, but some are not. So, In this post we are going to focus on this table hint and its impact.

What is NOLOCK

NOLOCK and READUNCOMMITTED hints used with SELECT queries to get the data without issuing shared lock (which prevents modification of data by other transactions during read operation) on the data, and also allows to read the data acquired with exclusive lock by other transactions. NOLOCK hint is equivalent to READUNCOMMITTED isolation level which is least restrictive in SQL Server. It does not restrict the dirty reads and increases the concurrency by reducing blocking.

It holds a Sch-S (Schema stability) lock during its execution scope which blocks the transactions requesting for Sch-M (Schema modification) lock, required by DDL operations. Reverse is also true … More


Divide rows of a column in two columns 5

In this post “Divide rows in two columns”, we are going to learn a trick to divide a column’s rows in two columns. We have find the total number of rows and then distribute it in two columns, For example, a table with a column containing 6 rows, will split in two columns, each of 3 rows. In case of odd numbers of rows in the column, in second column, a blank string should appear for last entry. Lets have a look on the below table and required output to understand this;

SELECT * FROM dbo.DemoTable

Divide rows in two columns

Required output

Divide rows in two columns

You can see in above figures, we have 7 rows in our demo table and these rows have been distributed in two columns. Hence, we have a odd count for total rows, last row in column2 has a blank value. Below is the demo table with sample data.

Create a demo table

More

Text wrapping in SSRS Reports in ReportViewer Control 2

In this blog post “Report Viewer wrap text”, we are going to learn an important trick of text wrapping in SSRS reports for long texts (without space and any separator) exceeding the width of a report column’s defined width and being rendered in a Report Viewer control of .Net  Application. Below is the problem explanation in more detail;

I have a report which is rendering through a ReportViewer control in a .Net Web Application. I have defined the width of each and every column in my report. As there are multiple columns in my report and some of which contains long texts without any space or separator. What i want, if the text is too long to fit as per the defined width, the text should be wrapped in next row. I don’t want to expand the column width to accommodate this text in one row. In other words, I

More

Count of total spaces in a string in SQL Server – Various methods 6

In this post we are going to find the total number of spaces in a given string. I have a string which contains spaces at multiple places and i just want to count them all. We can achieve this in various ways. Here, i am sharing some of these methods. Why i am doing this in various ways and sharing with you is just to show you the performance of different approaches of problem solving and also to share some interesting tricks to solve this problem in different manners. I am not going to cover all the possible ways to solve this problem and in fact you can achieve this in some other ways too. In case you want to share your own method, I welcome it you all to share this in comment section of this post.

Create a demo table as below and then put some demo data … More


Convert rows into comma separated values in a column 9

In this post, we are going to learn a technique to combine all the values of a row (based on a particular condition) with a separator, in a column along with other columns. What i meant by this let me explain it in more detail. Have a look on below question as raised by a colleague;

I have a Students table and a Subjects table. One student has been assigned to multiple Subjects in a transaction table. I want to extract the name of all the Subjects for which the student is assigned in a comma separated way along with other details of the student. If student is not assigned yet, i want to show a “Not Assigned Yet” value for those records.

To achieve this task, i have created a demo script and sharing the same with all the details. Below you can find the table structures and demo … More


Count of total not null values from all columns of a table 1

In this post, we will count not null values from all the columns of a given table. One of my friend asked me to get the count of all not null values from all the columns of a given table. I used a CASE statement in my query and achieved this task but again i thought can i do this with the help of COUNT function too? And then i found an another way to achieve this task too. Here i am sharing both the ways. I am also sharing a trick to find only NULL values too.
To demonstrate this first create a table and put some demo data as below;

Create a Table:

CREATE TABLE dbo.TEST
(
    ColA VARCHAR(10),
    ColB VARCHAR(10),
    ColC VARCHAR(10),
    ColD VARCHAR(10),
    ColE VARCHAR(10),
    ColF VARCHAR(10)
)
GO

Add rows:

INSERT INTO dbo.TEST(ColA, ColB, ColC, ColD, ColE, ColF)
VALUES('Mango', 'Apple', 'Banana', NULL, NULL, NULL),
('Mango', 
More

sp_executesql vs execute in SQL Server 4

In this post “SP_EXECUTESQL vs Execute”, we are going to compare sp_executesql and execute in SQL Server. Apart from differences, we will also discuss the similarities between sp_executesql and execute commands. Lets start with few questions like, What is the difference between Execute and sp_ExecuteSQL? How can we execute a T-SQL String? Similarities between Execute and sp_ExecuteSQL. Execute and sp_ExecuteSQL common features. Benefits of sp_ExecuteSQL. Which is better to use sp_ExecuteSQL or EXEC? Execute vs sp_ExecuteSQL.

Let’s discuss these points one by one.

Difference between sp_ExecuteSQL vs Execute

sp_ExecuteSQL

sp_ExecuteSQL is also used to execute a T-SQL string in SQL Server and points are below:

  • It allows parametrization and hence more secure than EXEC command to execute a SQL string dynamically. It’s tough to inject.
  • We don’t need to cast the parameter values as like we need in EXEC command. We simply put the parameter name in T-SQL string as
More