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

sp_help for SQL Server Management Studio

- 21.11.19 | 27.11.19 - ErcanOPAK comment on sp_help for SQL Server Management Studio

If you have SQL Server Management Studio, open it up and execute sp_help ‘dbo.TableName‘. See which column that FK is on, and which column of which table it references. And also that code will let you see many details about Target Table. You can use the same with shourtcut ALT + F1 as well.

Read More
SQL

List all Foreign Keys referencing a given table in SQL Server

- 20.11.19 | 27.11.19 - ErcanOPAK comment on List all Foreign Keys referencing a given table in SQL Server

EXEC sp_fkeys ‘TableName’ You can also specify the schema: EXEC sp_fkeys @pktable_name = ‘TableName’, @pktable_owner = ‘dbo’

Read More
SQL

How to find repetitive records and keep only 1 record for uniqueness in SQL

- 08.11.19 | 22.11.19 - ErcanOPAK comment on How to find repetitive records and keep only 1 record for uniqueness in SQL

–STEP 1: Find Repetitive Records (Only the Active Ones) SELECT INDEX1, INDEX2 INTO #TEMP_TABLE FROM MY_TABLE WHERE ACTIVE = 1 GROUP BY INDEX1, INDEX2 HAVING COUNT(1) > 1 ORDER BY COUNT(1) DESC –STEP 2: Create the Cursor DECLARE @INDEX1 INT DECLARE @INDEX2 INT DECLARE @ID INT DECLARE MY_CURSOR CURSOR FOR SELECT INDEX1, INDEX2 FROM #TEMP_TABLE […]

Read More
SQL

Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one sql statement

- 27.10.19 | 22.11.19 - ErcanOPAK comment on Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one sql statement

These 2 scripts clean all views, SPS, functions PKs, FKs and tables. 1. The First script by Adam Anderson, updated to support objects in other schemas than dbo: — check constraints select @stmt = isnull( @stmt + @n, ” ) + ‘alter table [‘ + schema_name(schema_id) + ‘].[‘ + object_name( parent_object_id ) + ‘] drop […]

Read More
SQL

How to Alter a Computed Column in SQL?

- 25.09.19 | 22.11.19 - ErcanOPAK comment on How to Alter a Computed Column in SQL?

There is NO way to alter computed column. You have to drop and recreate it. Let’s say you have a table with 3 column. And the 3rd one is the sum of the other two: CREATE TABLE MyTable (Column1 int, Column2 int, Column3 AS Column1 + Column2); If you want to change that computed column as multiply of the other […]

Read More
SQL

How to solve “A cursor with the name already exists” problem?

- 25.09.19 | 27.11.19 - ErcanOPAK comment on How to solve “A cursor with the name already exists” problem?

If you get this error then it means you are using global cursor that will be defined each time you are calling this procedure and give you the same error. Define a local cursor. Just put the keyword LOCAL after CURSOR: DECLARE MyCursor CURSOR LOCAL FOR … Reference

Read More
SQL

Create, Alter, Drop and Execute SQL Server Stored Procedures

- 23.09.19 | 22.11.19 - ErcanOPAK comment on Create, Alter, Drop and Execute SQL Server Stored Procedures

A stored procedure is a saved block of T-SQL code, such as a query to list the rows in a table.  A block of T-SQL code can be saved in a T-SQL script file.  You can also store the code from a script file in a stored procedure. There are several benefits that result from […]

Read More
SQL

SQL Query to Find and Replace text in a stored procedures

- 20.09.19 | 22.11.19 - ErcanOPAK comment on SQL Query to Find and Replace text in a stored procedures

Declare @spnames CURSOR Declare @spname nvarchar(max) Declare @moddef nvarchar(max) Set @spnames = CURSOR FOR select distinct object_name(c.id) from syscomments c, sysobjects o where c.text like ‘%findtext%’ and c.id = o.id and o.type = ‘P’ OPEN @spnames FETCH NEXT FROM @spnames into @spname WHILE @@FETCH_STATUS = 0 BEGIN Set @moddef = (SELECT Replace ((REPLACE(definition,’findtext’,’replacetext’)),’ALTER’,’create’) FROM sys.sql_modules […]

Read More
SQL

Return number of rows affected by UPDATE statements in SQL

- 18.09.19 | 22.11.19 - ErcanOPAK comment on Return number of rows affected by UPDATE statements in SQL

DECLARE @Teams TABLE( Name varchar(100) ) INSERT INTO @Teams VALUES (N’GALATASARAY’) INSERT INTO @Teams VALUES (N’LIVERPOOL’) INSERT INTO @Teams VALUES (N’BARCELONA’) INSERT INTO @Teams VALUES (N’PARIS SAINT GERMAIN’) INSERT INTO @Teams VALUES (N’JUVENTUS’) UPDATE @Teams SET Name = N’PORTO’ WHERE Name = ‘PARIS SAINT GERMAIN’ SELECT @@ROWCOUNT –gives 1

Read More
SQL

How to return only the Date part from SQL Server DateTime datatype

- 03.09.19 | 22.11.19 - ErcanOPAK comment on How to return only the Date part from SQL Server DateTime datatype

SELECT CONVERT(DATE, GETDATE())

Read More
SQL

“ORDER BY” with CASE and UNION in SQL Server

- 01.09.19 | 22.11.19 - ErcanOPAK comment on “ORDER BY” with CASE and UNION in SQL Server

If you want to use the combination of ORDER BY, CASE and UNION then you should use inner query for a solution without any headache: SELECT * FROM ( SELECT Col_a, Col_b, 0 AS Col_c FROM table1 WHERE conditions UNION SELECT Col_a, NULL AS Col_b, Col_c FROM table2 WHERE conditions ) x ORDER BY CASE […]

Read More
SQL

Update Column with row_number() in SQL

- 01.08.19 | 22.11.19 - ErcanOPAK comment on Update Column with row_number() in SQL

UPDATE x SET x.CODE_DEST = x.New_CODE_DEST FROM ( SELECT CODE_DEST, ROW_NUMBER() OVER (ORDER BY [RS_NOM]) AS New_CODE_DEST FROM DESTINATAIRE_TEMP ) x

Read More
SQL

Average of all values in a column that are not zero in SQL

- 11.06.19 | 24.03.26 - ErcanOPAK comment on Average of all values in a column that are not zero in SQL

The Challenge: You need to calculate the average price, rating, or score in a column. However, the column contains 0 (zero) values that represent “missing data” or “not yet rated.” If you use a standard AVG(), these zeros will pull your average down significantly, giving you an inaccurate result. The Secret: In SQL, the AVG() […]

Read More
SQL

Date and Time Conversions in SQL Server

- 26.05.19 | 22.11.19 - ErcanOPAK comment on Date and Time Conversions in SQL Server

SQL Server provides a number of options you can use to format a date/time string. One of the first considerations is the actual date/time needed. The most common is the current date/time using getdate(). This provides the current date and time according to the server providing the date and time. If a universal date/time is needed, […]

Read More
SQL

How to concatenate text from multiple rows into a single text string in SQL server?

- 19.04.19 | 22.11.19 - ErcanOPAK comment on How to concatenate text from multiple rows into a single text string in SQL server?

STUFF((SELECT ‘, ‘+ Column1 FROM Table1 FOR XML PATH(”)),1,1,”) AS ConcatenatedColumn StudentName ————- Mary John Sam Alaina Edward Result: Mary, John, Sam, Alaina, Edward

Read More
SQL

Drag and Drop objects from Object Explorer to the Query Window in SQL Server

- 22.01.19 | 22.11.19 - ErcanOPAK comment on Drag and Drop objects from Object Explorer to the Query Window in SQL Server

Are you writing a query that selects data from a table with lots of columns?  Does the select list we need to have all or almost all of these columns? Remember, that it is possible to drag and drop objects from Object Explorer to the Query Window. By dragging and dropping the “Columns” node under […]

Read More
SQL

Change Default Value for “Select Top n” and “Edit Top n” Rows in SQL Server Studio

- 20.01.19 | 22.11.19 - ErcanOPAK comment on Change Default Value for “Select Top n” and “Edit Top n” Rows in SQL Server Studio

In SQL Server Management Studio, by default you can only Select Top 1000 Rows or Edit Top 200 Rows for a table as shown in the below snippet.

Read More
SQL

DATEADD Function in SQL

- 14.06.18 | 22.11.19 - ErcanOPAK comment on DATEADD Function in SQL

DATEADD(interval, number, date) Parameter Values (interval, number, date) Parameter Description interval Required. The time/date part to return. Can be one of the following values: year, yyyy, yy = Year quarter, qq, q = Quarter month, mm, m = month dayofyear = Day of the year day, dy, y = Day week, ww, wk = Week […]

Read More
SQL

Get the count of Duplicate Records in SQL

- 11.06.18 | 14.04.20 - ErcanOPAK comment on Get the count of Duplicate Records in SQL

First, Let’s have the table and its records:

Read More
SQL

Replace duplicate spaces with a single space in SQL

- 11.06.18 | 14.04.20 - ErcanOPAK comment on Replace duplicate spaces with a single space in SQL

SELECT REPLACE(REPLACE(REPLACE(‘ With this query you select single spaces’,’ ‘,'<>’),’><‘,”),'<>’,’ ‘) OUTPUT: With this query you select single spaces

Read More
SQL

Convert a Comma-Delimited List to a Table in SQL

- 11.06.18 | 14.04.20 - ErcanOPAK comment on Convert a Comma-Delimited List to a Table in SQL

You can check the example code:

Read More
SQL

Get the User Name and Domain Name from an Email Address in SQL

- 11.06.18 | 24.03.26 - ErcanOPAK comment on Get the User Name and Domain Name from an Email Address in SQL

The Challenge: You have a Users table with thousands of email addresses, and you need to generate a report that separates the Username from the Domain Name. Doing this manually is impossible, and using complex RegEx in SQL can be overkill. The Solution: By combining three core SQL functions—SUBSTRING, CHARINDEX, and LEN—you can perform surgical […]

Read More
SQL

Get the First and Last Word from a String or Sentence in SQL

- 11.06.18 | 24.03.26 - ErcanOPAK 2 Comments on Get the First and Last Word from a String or Sentence in SQL

The Challenge: You have a column of full names, product titles, or long descriptions, and you need to grab just the first and last words. Since SQL doesn’t have a native Split() function that returns an array easily, we have to get creative with string positioning. The Solution: We use a combination of CHARINDEX, SUBSTRING, […]

Read More
SQL

Add Constraint to SQL Table to ensure email contains @

- 11.06.18 | 24.03.26 - ErcanOPAK comment on Add Constraint to SQL Table to ensure email contains @

The Strategy: Never trust your data source blindly. While frontend validation is great for UX, Database Constraints are your last line of defense. By adding a CHECK CONSTRAINT, you ensure that no invalid email address ever touches your storage, whether it comes from an API, a bulk import, or a manual script. The Goal: To […]

Read More
SQL

Capitalize words in SQL (Display data first letter uppercase rest lowercase)

- 08.06.18 | 22.11.19 - ErcanOPAK comment on Capitalize words in SQL (Display data first letter uppercase rest lowercase)

SELECT UPPER(LEFT(colname,1)) + LOWER(RIGHT(colname, LEN(colname) – 1)) FROM tablename

Read More
ASP.Net WebForms / SQL

How to solve “TableAdapter can’t see stored procedure returned fields when using temp table” problem

- 06.06.18 | 22.11.19 - ErcanOPAK comment on How to solve “TableAdapter can’t see stored procedure returned fields when using temp table” problem

Add these lines to the beginning of your stored procedure. The statement is never executed, but for some reason it gets the job done. I really don’t know how and why 🙂 IF 1=0 BEGIN SET FMTONLY OFF END

Read More
SQL

Get the row number “x” from SQL

- 04.06.18 | 22.11.19 - ErcanOPAK comment on Get the row number “x” from SQL

WITH Records AS (SELECT ROW_NUMBER() OVER(ORDER BY ID_of_your_SQLTable) AS ‘ROW’, * FROM yourSQLTable) SELECT * FROM Records WHERE ROW = X –You can change X with the number you wanna get (For Example ROW = 5 will bring the 5th record)

Read More
SQL

Add Default Value in SQL Server

- 03.06.18 | 03.06.18 - ErcanOPAK comment on Add Default Value in SQL Server

If there is no default value for the column then you can use this query: ALTER TABLE MyTable ADD CONSTRAINT df_MyTable_MyColumn DEFAULT 0 FOR MyColumn

Read More
SQL

Search text in all stored procedures in SQL Server

- 03.06.18 | 22.11.19 - ErcanOPAK comment on Search text in all stored procedures in SQL Server

SELECT name FROM sys.procedures WHERE Object_definition(object_id) LIKE ‘%search_parameter%’

Read More
SQL

Find numbers with more than two decimal places in SQL

- 02.06.18 | 24.03.26 - ErcanOPAK comment on Find numbers with more than two decimal places in SQL

The Problem: In financial databases, you often expect values to have exactly two decimal places (e.g., $10.50). However, due to bulk imports or rounding errors, “hidden” decimals like 10.50001 can creep into your tables. These tiny fractions can cause massive discrepancies in your total sums and reports. The Smart Solution: Instead of slow string parsing, […]

Read More
Page 8 of 9
« Previous 1 … 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