Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Category: SQL

SQL

SQL Tip: “NOCOUNT” should be activated on “PROCEDURE” and “TRIGGER” definitions

- 03.12.22 - ErcanOPAK comment on SQL Tip: “NOCOUNT” should be activated on “PROCEDURE” and “TRIGGER” definitions

NOCOUNT is by default deactivated (OFF) at the server level. It means by default, the server will send to the client the number of rows affected by the SQL query executed which is, in most cases, useless because no one will read this information. Deactivating this feature will save some network traffic and improve the […]

Read More
SQL

SQL Tip: “NULL” should not be compared directly

- 03.12.22 - ErcanOPAK comment on SQL Tip: “NULL” should not be compared directly

“NULL” is never equal to anything, even itself. Therefore comparisons using equality operators will always return False, even when the value actually IS NULL. For that reason, comparison operators should never be used to make comparisons with NULL; IS NULL and IS NOT NULL should be used instead. Bad example: UPDATE books SET title = ‘unknown’ WHERE title = NULL — Noncompliant […]

Read More
SQL

How to get ‘n’th row in Sql Query with OFFSET FETCH NEXT and ROW_NUMBER()

- 20.07.22 | 21.07.22 - ErcanOPAK comment on How to get ‘n’th row in Sql Query with OFFSET FETCH NEXT and ROW_NUMBER()

In SQL Server 2012+, you can use OFFSET…FETCH. The below query will help you to get 2nd row. But with little changes, you can get the ‘n’th row as well. SELECT <column(s)> FROM <table(s)> ORDER BY <sort column(s)> OFFSET 1 ROWS — Skip this number of rows FETCH NEXT 1 ROWS ONLY; — Return this […]

Read More
SQL

How to get stored procedure parameters details in SQL

- 23.06.22 - ErcanOPAK comment on How to get stored procedure parameters details in SQL

select ‘Parameter_name’ = name, ‘Type’ = type_name(user_type_id), ‘Length’ = max_length, ‘Prec’ = case when type_name(system_type_id) = ‘uniqueidentifier’ then precision else OdbcPrec(system_type_id, max_length, precision) end, ‘Scale’ = OdbcScale(system_type_id, scale), ‘Param_order’ = parameter_id, ‘Collation’ = convert(sysname, case when system_type_id in (35, 99, 167, 175, 231, 239) then ServerProperty(‘collation’) end) from sys.parameters where object_id = object_id(‘MySchema.MyProcedureName’)  

Read More
ASP.Net MVC / ASP.Net WebForms / C# / SQL

How to add default value for Entity Framework migrations for DateTime and Bool

- 19.06.22 | 16.01.26 - ErcanOPAK comment on How to add default value for Entity Framework migrations for DateTime and Bool

When working with Code First Migrations, developers often struggle with setting default values at the database level. While you can set a default value in your C# class, doing it directly in the SQL schema is much more robust. Here is how you can achieve this using defaultValue and defaultValueSql. 1. Using Static Default Values […]

Read More
SQL

How to get triggers create and update date in SQL

- 07.06.22 - ErcanOPAK comment on How to get triggers create and update date in SQL

SELECT o.name as [Trigger Name], CASE WHEN o.type = ‘TR’ THEN ‘SQL DML Trigger’ WHEN o.type = ‘TA’ THEN ‘DML Assembly Trigger’ END AS [Trigger Type], sc.name AS [Schema_Name], OBJECT_NAME(parent_object_id) as [Table Name], o.create_date [Trigger Create Date], o.modify_date [Trigger Modified Date] FROM sys.objects o INNER JOIN sys.schemas sc ON o.schema_id = sc.schema_id WHERE (type = […]

Read More
SQL

How to enable, disable and check if Service Broker is enabled on a database in SQL Server

- 14.01.22 | 24.03.26 - ErcanOPAK comment on How to enable, disable and check if Service Broker is enabled on a database in SQL Server

The Context: SQL Server Service Broker is a powerful framework for building highly scalable, asynchronous database applications. Whether you are using Query Notifications, External Activations, or Distributed Messaging, knowing how to properly manage the Broker state is essential for any DBA or Backend Developer. The Challenge: Simply running an ALTER DATABASE command often hangs indefinitely […]

Read More
SQL

How to check if recursive triggers are enabled in SQL

- 25.12.21 - ErcanOPAK comment on How to check if recursive triggers are enabled in SQL

Recursive triggers are set at the database level. It’s part of the database metadata information and is available through “sys.databases” view. You can use the below query to check whether the recursive triggers are enabled or not on your database: SELECT name AS ‘Database’, is_recursive_triggers_on AS ‘Recursive Trigger Enabled’ FROM sys.databases Here is the output:

Read More
SQL

How to hide letters with asterisks except first letter in SQL

- 08.11.21 - ErcanOPAK comment on How to hide letters with asterisks except first letter in SQL

The easiest way to do that is using a function CREATE FUNCTION [dbo].[HideNameWithAsterisks](@Name varchar(max)) RETURNS varchar(MAX) AS BEGIN DECLARE @loop int = LEN(@Name) WHILE @loop > 1 SELECT @Name = STUFF(@Name, @loop, 1, CASE WHEN SUBSTRING(@Name, @loop – 1, 2) like ‘% ‘ THEN ‘ ‘ WHEN SUBSTRING(@Name, @loop – 1, 2) like ‘ %’ […]

Read More
C# / SQL

How to use SQL RAISEERROR() messages in C#

- 21.10.21 - ErcanOPAK comment on How to use SQL RAISEERROR() messages in C#

DECLARE @MyValue int SELECT @MyValue = Column1 WHERE Column2 > 50 IF @MyValue IS NULL BEGIN RAISEERROR(‘This is my custom message from SQL.’, 16, 1) END ELSE SELECT MyValue END Use an error code within 11-16, or just use 16 for a “general” case. RAISERROR(‘This is my custom message from SQL.’,16,1) Why? Here’s summary of […]

Read More
SQL

How to check column data types of a table in SQL

- 12.10.21 | 15.02.26 - ErcanOPAK comment on How to check column data types of a table in SQL

The Scenario: You are handed a legacy database with hundreds of tables, or perhaps you’re debugging a “Type Mismatch” error in your backend. You need to know exactly what a table is made of—data types, nullability, and precision—without clicking through a clunky UI. The Solution: While every database engine has its own way, the INFORMATION_SCHEMA […]

Read More
SQL

How to use Nested Cursors in SQL

- 17.08.21 - ErcanOPAK comment on How to use Nested Cursors in SQL

Declare @Parameter1 int; Declare @Parameter2 int; DECLARE Cur1 CURSOR FOR SELECT Column1 From Table1; OPEN Cur1 FETCH NEXT FROM Cur1 INTO @Parameter1; WHILE @@FETCH_STATUS = 0 BEGIN PRINT ‘Processing Column1: ‘ + Cast(@Parameter1 as Varchar); DECLARE Cur2 CURSOR FOR SELECT Column2 FROM Table2 Where Column2 = @Parameter1; OPEN Cur2; FETCH NEXT FROM Cur2 INTO @Parameter2; […]

Read More
SQL

How To Get All Row Count For All Tables In SQL Server Database

- 04.08.21 - ErcanOPAK comment on How To Get All Row Count For All Tables In SQL Server Database

SELECT (SCHEMA_NAME(A.schema_id) + ‘.’ + A.Name) AS TableName , SUM(B.rows) AS RecordCount FROM sys.objects A INNER JOIN sys.partitions B ON A.object_id = B.object_id WHERE A.type = ‘U’ GROUP BY A.schema_id, A.Name ORDER BY RecordCount DESC  

Read More
SQL

How to check for ‘IS NOT NULL’ And ‘IS NOT EMPTY’ string in SQL

- 09.04.21 - ErcanOPAK comment on How to check for ‘IS NOT NULL’ And ‘IS NOT EMPTY’ string in SQL

If you only want to match N” as an empty string SELECT COLUMN FROM TABLE WHERE DATALENGTH(COLUMN) > 0 If you want to count any string consisting entirely of spaces as empty SELECT COLUMN FROM TABLE WHERE COLUMN <> ” If you want to use DATALENGTH for any string consisting entirely of spaces then you […]

Read More
SQL

Filter Rows By Max Date in SQL

- 13.01.21 - ErcanOPAK comment on Filter Rows By Max Date in SQL

SELECT m1.Column1, m1.Column2, m1.Column3, r.MaxTime FROM ( SELECT Column1, MAX(TimeColumn) as MaxTime FROM MyTable GROUP BY Column1 ) r INNER JOIN MyTable m1 ON m1.Column1= r.Column1AND m1.TimeColumn = r.MaxTime

Read More
SQL

SQL Server CONCAT_WS Function (Concat with Separator)

- 13.08.20 - ErcanOPAK comment on SQL Server CONCAT_WS Function (Concat with Separator)

The SQL Server CONCAT_WS() function concatenates two or more strings into one string with a separator. CONCAT_WS() means concatenate with separator.

Read More
SQL

Search text in all tables in SQL Server

- 27.05.20 | 27.05.20 - ErcanOPAK comment on Search text in all tables in SQL Server

Please note that with a little bit changing you can also use this query as a stored procedure.

Read More
SQL

How to hide message window in MS SQL Server

- 16.04.20 | 15.02.26 - ErcanOPAK comment on How to hide message window in MS SQL Server

The Struggle: You are writing a complex stored procedure on your laptop screen. Every time you execute a query, the bottom half of your screen gets hijacked by the “Messages” or “Results” window, leaving you with a tiny viewport for your actual code. The Fix: Stop reaching for the mouse to minimize that window. Master […]

Read More
SQL

How to find the first missing value in a series in MS SQL

- 14.04.20 - ErcanOPAK comment on How to find the first missing value in a series in MS SQL

Let’s say we have this values in the database: num — 1 2 4 5 6 8 9 11 To Find the first missing value, we can use this: ;WITH CteRN AS( SELECT *, RN = num – ROW_NUMBER() OVER(ORDER BY num) FROM tbl ) SELECT TOP 1 num – RN FROM CteRN WHERE RN […]

Read More
SQL

How to solve ‘incorrect syntax near the keyword WITH’ problem?

- 14.04.20 | 14.04.20 - ErcanOPAK comment on How to solve ‘incorrect syntax near the keyword WITH’ problem?

Always use with statement like ;WITH then you’ll never get this error. The WITH command required a ; between it and any previous command, by always using ;WITH you’ll never have to remember to do this.

Read More
C# / SQL

How to solve ‘Size property has an invalid size of 0’ in C#

- 14.04.20 | 14.04.20 - ErcanOPAK comment on How to solve ‘Size property has an invalid size of 0’ in C#

You need to define a length when specifying the varchar parameter: SqlParameter TheFirstJob = cmd.Parameters.Add(“@spParam”, SqlDbType.VarChar, 50); Note: You should use the same length as defined in your SQL Server stored procedure.

Read More
SQL

How to clean Query and Execution Plan Cache for Efficient Testing in MS SQL

- 11.04.20 - ErcanOPAK comment on How to clean Query and Execution Plan Cache for Efficient Testing in MS SQL

CHECKPOINT GO –Cleaen Query Cache DBCC DROPCLEANBUFFERS; GO –Clean Execution Plan Cache DBCC FREEPROCCACHE; GO

Read More
SQL

How to create Local and Global Temp Tables in MS SQL

- 11.04.20 - ErcanOPAK comment on How to create Local and Global Temp Tables in MS SQL

Creating Local Temp Table [with #]: SELECT TOP(10) ID INTO #TempTable FROM MyTable Craeting Global Temp Table [with ##]: SELECT TOP(10) ID INTO ##TempTable FROM MyTable Reaching Local Temp Table with Query: SELECT name FROM tempdb..sysobjects WHERE name LIKE ‘#TempTable%’  

Read More
SQL

Set Identity Insert On/Off in MSSQL

- 08.04.20 | 11.04.20 - ErcanOPAK comment on Set Identity Insert On/Off in MSSQL

SET IDENTITY_INSERT MyTable ON INSERT INTO MyTable (IdentityColumn, col2, col3, …) VALUES (IdentityValue, col2value, col3value, …) SET IDENTITY_INSERT MyTable OFF Note that you can not insert explicit value for identity column in table ‘MyTable’ when IDENTITY_INSERT is set to OFF.

Read More
SQL

Reset the Identity on table in MSSQL

- 08.04.20 - ErcanOPAK comment on Reset the Identity on table in MSSQL

DBCC CHECKIDENT(‘TableName’, RESEED, 0) Note that after running DBCC CHECKIDENT(‘TableName’, RESEED, 0): – Newly created tables will start with identity 0 – Existing tables will continue with identity 1

Read More
SQL

Unique Constraints in MS SQL Server to prevent duplications

- 31.03.20 | 31.03.20 - ErcanOPAK comment on Unique Constraints in MS SQL Server to prevent duplications

The UNIQUE constraint ensures that all values in a column are different. Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint. However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint […]

Read More
SQL

View the Definition of a Stored Procedure in SQL

- 31.12.19 - ErcanOPAK comment on View the Definition of a Stored Procedure in SQL

– In Object Explorer, connect to an instance of the Database Engine. – On the toolbar, click New Query. – In the query window, enter the following statement that uses the sp_helptext system stored procedure. Change the database name and stored procedure name to reference the database and stored procedure that you want. EXEC sp_helptext […]

Read More
SQL

SQL ‘string_split()’ function

- 07.12.19 | 07.12.19 - ErcanOPAK comment on SQL ‘string_split()’ function

string_split() is a table-valued function that splits a string into rows of substrings, based on a specified separator character. SELECT * FROM string_split(’21;32;34;47;53;61;’, ‘;’) OUTPUT ====== 21 32 34 47 53 61

Read More
SQL

How to use Replicate function for masking in SQL?

- 06.12.19 | 07.12.19 - ErcanOPAK comment on How to use Replicate function for masking in SQL?

You can use SQL “Replicate” command to mask the data. Below code shows how to make it:

Read More
SQL

Rename column SQL Server

- 28.11.19 - ErcanOPAK comment on Rename column SQL Server

EXEC sp_RENAME ‘TableName.OldColumnName’ , ‘NewColumnName’, ‘COLUMN’

Read More
Page 7 of 9
« Previous 1 2 3 4 5 6 7 8 9 Next »

Posts navigation

Older posts
Newer posts
April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (858)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (753)
  • Add Constraint to SQL Table to ensure email contains @ (578)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (564)
  • Average of all values in a column that are not zero in SQL (531)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (489)
  • Find numbers with more than two decimal places in SQL (447)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (858)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (753)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com