Sql-Server

Data Modeling: Don't use money for storing money in SQL Server

I know that SQL Server has a number of data types that look like they could be used for storing amounts of money. Developers go looking for data types that have a decimal point in them, and there are a few:

Float - this one seems to get picked by people from a Java background. And it’s pretty much never the right choice. It doesn’t store values exactly because it’s an approximation. Just like we have numbers in decimal that we can’t write exactly (like 1/3 as a decimal is 0.33333 and so on), there are numbers in binary that we can’t store exactly. And that includes numbers like 0.1 (yes, ten cents). So using this data type for money is usually a newbie mistake, and the same applies to the potentially even more approximate real.

2019-06-18

T-SQL 101: 22 Using the LIKE operator in T-SQL

I’ve discussed standard operators in previous posts but an interesting additional one is the LIKE operator. This allows for basic pattern matching in T-SQL predicates.

Let’s look at a simple example:

WHERE ProductName LIKE ‘Grant%’

The % symbol is a wild-card that matches any set of characters, including no characters at all. So this expression would match the following and more:

Grant Grants Cola Grants Lemonade

If we’re looking for a word that’s contained inside a string, we can use it like this:

2019-06-17

SQL: Using CROSS APPLY to replace multi-column predicate look-ups in T-SQL

A few months ago, I was asked a question by a friend of mine who is also a Data Platform MVP. He was trying to express a query like this:


SELECT si.StockItemID, si.StockItemName, si.Size, si.LeadTimeDays FROM Warehouse.StockItems AS si WHERE (si.Size, si.LeadTimeDays) IN (('M', 7), ('XXL', 7), ('XXL', 12));

He was trying to select stock items where the combinations of Size and LeadTimeDays are in the list. Unfortunately, in SQL Server and T-SQL, an IN can only be used with a single column. We can’t use a row constructor like (si.Size, si.LeadTimeDays) as a lookup into a list of constructed rows.

2019-06-13

SDU Tools: Is Week Day or Is Weekend in SQL Server T-SQL

I often need to know if a date that I’m working with is a week day or a weekend. While T-SQL has some functions that help with that, you need to use a combination of them to get a reliable outcome that’s independent of the current session settings. (Based on Saturday / Sunday being weekends but easy for you to change if you have different weekends). To make it easy, we added two functions IsWeekday and IsWeekend to our free SDU Tools for developers and DBAs.

2019-06-12

T-SQL 101: 21 Comparison operators in T-SQL

We use comparison operators in T-SQL expressions. And the expressions are often used in WHERE clauses. For example:

WHERE CreditLimit = 10000

The = (equals) sign shown is a simple one but T-SQL has a number of operators.

Most people writing T-SQL code are familiar with these:

= Equals <> Does not equal < Less than > Greater than <= Less than or equals >= Greater than or equals

However, less people seem to be aware of the other operators added a while back:

2019-06-10

SQL: Penetration Tests, SQL CLR, and Poor Advice

I was working in a bank a few weeks back. They were planning an upgrade from SQL Server 2008 R2 to SQL Server 2017. One of the things that I raised with them is why there were extended stored procedures that had been added to the master database. And they told me they were the result of a penetration test.

OK, so this had me intrigued.

The logic that was being applied was that the penetration testers required the bank to have SQL CLR disabled. No-one in the bank could tell me why; just that it was required to be off.

2019-06-06

SDU Tools: Script SQL Server Table as T-SQL Unpivot

Many of my friends have told me that they like how concise the PIVOT and UNPIVOT operators are, but they find the syntax confusing. I must admit that I wasn’t a big fan of these when they were added in SQL Server 2008 as they didn’t really add anything to the language that I couldn’t have done with CASE statements, etc. and there were so many other parts of the language that needed work.

2019-06-05

T-SQL 101: 20 Filtering rows to return with WHERE clause predicates

I showed earlier how we can retrieve data from a table using a SELECT clause. It can also be used to determine which columns are returned, and which table the data is being retrieved from. But we don’t always want all the rows to be returned. The WHERE clause fixes that.

It’s important to understand that the WHERE clause limits the rows returned to ones where the expression in the WHERE clause evaluates to a logical TRUE value.

2019-06-03

Fixing Locking and Blocking Issues in SQL Server - Part 7 - Handling Deadlocks in T-SQL

This is part 7 in a series of posts:

  • Part 1 covered being sure there is a locking and blocking issue
  • Part 2 covered the impact of RCSI
  • Part 3 looked at the impacts of indexing on locking and blocking
  • Part 4 looked at what deadlocks really are and how SQL Server handles them
  • Part 5 looked at how applications should handle deadlocks
  • Part 6 looked at how to avoid deadlocks in the first place

Today, though, I want to look at how to handle deadlocks if you must do that in T-SQL.

2019-05-30

SDU Tools: List user access to Reporting Services content

I often need to document the access that various users have to content stored in SQL Server Reporting Services (SSRS). Surprisingly, I didn’t find any built-in way to do that.

We had started to add some Reporting Services related options to our free SDU Tools for developers and DBAs, and, just for this, we added a new procedure RSListUserAccessToContent.

It takes two parameters:

@IsOrderedByUserName bit -> Is the output ordered by user (default yes else by item) @RSDatabaseName sysname -> Reporting Services DB name (default is ReportServer)

2019-05-29