Tag: T-SQL

  • Get Current Date & Time in SQL Server

    Getting the current date and time in SQL Server is straightforward. Choosing the correct function for your workload is what matters. Whether you’re stamping audit rows, logging ETL runs, or investigating production issues, SQL Server exposes several built-in functions with different precision and return types. All of these functions use the Windows OS clock of…

  • How to Kill a SPID in SQL Server

    In SQL Server, every connection to the database engine is assigned a Session Process ID, commonly known as a SPID. There are situations where you may need to kill a SPID in SQL Server. This typically occurs when a session is blocking other queries, running indefinitely, holding locks during maintenance, or preventing a database from…

  • How to Check Blocking SPIDs in SQL Server

    Blocking is one of the most common causes of performance issues in SQL Server. When one session holds a lock on a resource and another session needs that same resource, the second session waits. If that wait persists, users experience slowness. Understanding how to quickly identify blocking SPIDs is a core DBA skill. This guide…

  • Deleting Rows in Batches in SQL Server

    Deleting large volumes of data from a SQL Server table looks simple, but it can cause real problems if done carelessly. A single large DELETE can: When you’re cleaning up historical data or running routine maintenance, deleting rows in batches is usually the safest and most predictable approach. This post shows a simple, production-safe pattern…

  • RAND() vs NEWID() in SQL Server

    Most people searching for “random numbers in SQL Server” want one of two things: SQL Server gives you two common tools for this: RAND() and NEWID(). They solve different problems, and confusion usually starts when they are treated as interchangeable. This post shows how to use both, starting with simple examples, and explains why they…

  • Applying Data Retention Safely in SQL Server

    Data retention in SQL Server usually means deleting old data in a controlled and repeatable way. That might be driven by compliance requirements, table growth that’s starting to hurt performance, SQL Server Express size limits, or simply keeping log and audit tables under control. Whether this is a one-off cleanup or a permanent maintenance task,…

  • Checking Table Sizes in SQL Server

    Checking table sizes in SQL Server is a routine DBA task that supports capacity planning, performance troubleshooting, and data retention work. There are several ways to get this information. Some methods are better suited to automation and reporting, while others are quicker for ad hoc checks in SQL Server Management Studio. This post walks through…

  • Understanding WHERE 1=1 and WHERE 1=2 in SQL Queries

    In SQL, you will often see queries written using WHERE 1=1 or WHERE 1=2. At first glance, both look pointless. One is always true, the other always false. In practice, both are deliberate patterns used for query construction and control, not filtering logic. They exist to make SQL safer, easier to manipulate, and more predictable…

  • What the USE Command Does in SQL Server

    The USE command changes the database context for the current session. Every statement in SQL Server runs in the context of a database. When you run USE, you are telling SQL Server which database subsequent statements should execute against until the context changes again or the session ends. That single behaviour explains a lot of…