SQL – Use HAVING to filter GROUP BY

WHERE filters individual rows. HAVING filters aggregated rows. When you use GROUP BY, you are grouping rows together into aggregated rows. How do you filter the aggregated rows? You use the HAVING clause. Example of using HAVING Let’s say we want to see all NFL divisions where all teams have played in the Super Bowl … Read more SQL – Use HAVING to filter GROUP BY

C# – How to use IN with Dapper

Let’s say you have a SQL Query that uses IN and you want to execute it using Dapper. Your query looks something like this: Here’s how you’d execute that with Dapper: Then you’d call it like this: There are two key things to notice about this: You have to exclude the parentheses. I specified one … Read more C# – How to use IN with Dapper

SSMS – Change how many rows are returned by SELECT Top 1000 Rows

When you right-click a table in SSMS, you can “Select Top 1000 Rows” and “Edit Top 200 Rows.” You can change how many rows are returned by changing the defaults. Tools SQL Server Object Explorer Change the values under Table and View Options Change these values to whatever makes sense in your situation. Note: if … Read more SSMS – Change how many rows are returned by SELECT Top 1000 Rows

SSMS – How to turn off “Prevent saving changes that require table to be re-created”

Problem You’re trying to save a table change in SSMS and you get the error message: Saving changes is not permitted. The changes that you have made require the following tables to be dropped and re-created. You have either made changes to a table that can’t be re-created or enabled the option Prevent saving changes … Read more SSMS – How to turn off “Prevent saving changes that require table to be re-created”

SqlTypeException: SqlDateTime overflow

Problem I am executing a SQL query and trying to set a datetime column to DateTime.MinValue. I’m getting the following exception: System.Data.SqlTypes.SqlTypeException: ‘SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.’ The problem is that DateTime.MinValue is 1/1/0001 12:00:00 AM and the SQL Server DateTime minimum value is 1/1/1753 12:00 AM. Solution … Read more SqlTypeException: SqlDateTime overflow

Using ORDER BY with CASE – Conversion failed when converting date and/or time from character string

Problem When I execute a SQL query I’m getting the following error: Conversion failed when converting date and/or time from character string. The query is using ORDER BY with CASE with a passed in parameter called @OrderBy. Solution Use a separate CASE statement for each possible value (instead of one CASE with multiple WHENs).

‘CREATE/ALTER PROCEDURE’ must be the first statement in a query batch

Problem You have a SQL query like this: While executing this you get the following error: ‘CREATE/ALTER PROCEDURE’ must be the first statement in a query batch Solution The error message says it all: the CREATE PROCEDURE statement cannot be executed after other queries in the same batch of queries. The solution is to execute … Read more ‘CREATE/ALTER PROCEDURE’ must be the first statement in a query batch

Event-driven .NET: How to use query notifications in SQL Server to monitor database changes

How do you check for new records in a database and react to the new data? You really only have two options: Poll for changes every X seconds Use query notifications to be notified when new data is inserted In this article I’ll show you how to configure query notifications so that your application receives … Read more Event-driven .NET: How to use query notifications in SQL Server to monitor database changes

Find the distance between two coordinates using SQL and C#

Imagine you’re in the middle of Millennium Park in Chicago. You want some coffee, but want a good deal on it. You open an app and see the nearest coffee shops offering deals to anyone with this app. How did this app know the distance from you to the coffee shops? This article will show … Read more Find the distance between two coordinates using SQL and C#

The specified sa password does not meet strong password requirements

Problem When entering the sa password while installing SQL Server you get the following error: “The specified sa password does not meet strong password requirements.“ Solution If the computer is on a domain Go to Administrative Tools / Domain Security Policy :: Security Settings | Account Policies | Password Policy Set “Minimum password length” = Not … Read more The specified sa password does not meet strong password requirements