Reading Time: < 1 minute“If I have a query that I run frequently, why should I save it as a View in the database instead of just simply saving it as a .SQL script file and running the query when I need it?” There are two main benefits to saving your query as a Continue Reading
SQL Questions
SQL Server DELETE vs. TRUNCATE: What’s the difference?
Reading Time: 2 minutesThe DELETE and TRUNCATE statements are both meant to delete content from a table. Since that’s true, it begs the question: What’s the difference? In this very brief tutorial, we’ll walk through the main differences between DELETE and TRUNCATE and explain when you should choose to use one over the Continue Reading
SQL Server COALESCE System Function: How It Works
Reading Time: 2 minutesThe COALESCE system function offers a great way to check expressions for NULL. The syntax for COALESCE is very simple. It looks like this: COALESCE(<expr1>,<expr2>,<expr3>,<expr4>,….<exprN>) As you can see, all we do is outline a comma-separated list of expressions to the function. SQL Server will evaluate each expression, from left Continue Reading
How to calculate age in SQL: Use THIS simple script!
Reading Time: 7 minutesOne interesting task you may be given as a data professional is to calculate a person’s age based on their date of birth. In this brief tutorial, we’ll discuss a simple script you can use to derive the number of years, months, and days that a person has aged since Continue Reading
How to make a column NOT NULL in SQL Server: Explained with Examples
Reading Time: 3 minutesYou may find yourself needing the change the definition of a table or column from time-to-time while working as a database professional. One change you may need to make is changing a column from nullable to non nullable. It may have been decided that we actually don’t want NULL to appear Continue Reading
Difference between unique constraints and primary key constraints: Answered with examples
Reading Time: 4 minutesA primary key constraint and a unique constraint are both meant to enforce uniqueness in column(s). Since that’s true, it begs the question: What’s the difference? In this very brief tutorial, we’ll discuss the two main differences between primary key constraints and unique constraints. Difference # 1: Primary key constraints Continue Reading
SQL Server Week Number: Use this ONE Simple Function!
Reading Time: 2 minutesSQL Server makes it very easy to gather the week number of a specific date. Maybe you need to know what week of the year a certain date falls within. The DATEPART system function is what we can use to gather that information. Using the DATEPART system function The date Continue Reading
SQL Server QUALIFY: Doesn’t Exist, Do THIS Instead
Reading Time: 3 minutesSQL Server has many useful window functions available to us that can make the task of gathering aggregate/ranking/offset data very easy. One tool that you might have heard of is the QUALIFY clause. This tool can be used to filter the result of a window function. The problem is that Continue Reading
DELETE Statement with a JOIN: How it’s done
Reading Time: 4 minutesIt is very easy to write a DELETE statement with a JOIN. In this very brief tutorial, we’ll walk through how it’s done. We’ll go over just these two topics: Write it as a SELECT statement first Convert your query to a DELETE statement Everything in this tutorial can also Continue Reading
How to remove trailing/leading whitespace in SQL
Reading Time: 2 minutesHow do we remove whitespace from a character string in SQL Server? It’s easy, just use the LTRIM/RTRIM string manipulation function! Take the following silly example: We have a large amount of leading whitespace in our string. To remove that whitespace, we can put this string in the LTRIM system Continue Reading