SQL

SQL

SQL is a standard language for storing, manipulating, and retrieving data in databases.

...see more

TRUNCATE TABLE Explained

The SQL command TRUNCATE TABLE removes all rows from a table at once — quickly and efficiently.
Unlike DELETE, which deletes rows one by one, TRUNCATE empties the entire table without affecting its structure.

That means columns, data types, indexes, and permissions remain intact — only the data is gone.

When to Use TRUNCATE

Use TRUNCATE TABLE when

  • you want to clear all data from a table,
  • you don’t need a WHERE clause, and
  • speed matters.

Example:

TRUNCATE TABLE customers;

This command instantly removes all records from the customers table while keeping the table ready for new data.

Key Differences: TRUNCATE vs. DELETE

Aspect TRUNCATE TABLE DELETE FROM
Supports WHERE clause No Yes
Speed Very fast Slower
Rollback possible Only in a transaction Yes
Triggers fire Usually no Yes
Resets AUTO_INCREMENT Yes (depends on DB) No

Important Notes

  • You cannot truncate a table that is referenced by a foreign key.
  • In most databases, TRUNCATE is treated as a DDL command (Data Definition Language), like CREATE or DROP.
  • Best practice: use TRUNCATE for temporary or helper tables where you’re certain the data can be safely removed.
...see more

When working with date and time data in SQL Server, you might want to find how many entries exist for each day within a specific timeframe. To do this, you need to:

  1. Extract the date part from a datetime column using CAST(date_column AS DATE). This removes the time portion and groups entries by day.
  2. Filter your data using a dynamic date range. For example, to select entries from 3 days ago up to 7 days in the future, use DATEADD with GETDATE().
  3. Use GROUP BY to count entries per day.

Here is an example query:

SELECT 
    CAST(date_column AS DATE) AS entry_date,
    COUNT(*) AS entry_count
FROM your_table
WHERE date_column BETWEEN CAST(DATEADD(DAY, -3, GETDATE()) AS DATE)
                      AND CAST(DATEADD(DAY, 7, GETDATE()) AS DATE)
GROUP BY CAST(date_column AS DATE)
ORDER BY entry_date;

This query lists each day in the timeframe along with the number of entries for that day. It’s useful for reports, activity summaries, or monitoring trends over time.

...see more

The following SQL statement shows how to copy the data from one column to another.

It will replace any existing values in the destination column, so be sure to know exactly what you’re doing and use the WHERE statement to minimize updates (e.g., WHERE destination_column IS NULL ).

UPDATE table 
SET destination_column = source_column
WHERE destination_column IS NULL
...see more

The SQL UPDATE Statement

The UPDATE statement is used to modify the existing records in a table.

UPDATE Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
 

SQL SET Keyword

The SET command is used with UPDATE to specify which columns and values should be updated in a table.

The following SQL updates the first category (CategoryID = 1) with a new Name and a new Type:

Example

UPDATE Category
SET Name = 'Article', Type = 'Premium'
WHERE CategoryID = 1;
 
Add to Set
  • .NET
  • Agile
  • AI
  • ASP.NET Core
  • Azure
  • C#
  • Cloud Computing
  • CSS
  • EF Core
  • HTML
  • JavaScript
  • Microsoft Entra
  • PowerShell
  • Quotes
  • React
  • Security
  • Software Development
  • SQL
  • Technology
  • Testing
  • Visual Studio
  • Windows
Actions
 
Sets