Azure-Sql-Db

SDU Tools: LeftPad and RightPad in T-SQL (Right-align, Left-align)

SDU Tools: LeftPad and RightPad in T-SQL (Right-align, Left-align)

Over the years, I’ve had a surprising number of questions on how to right-align a set of numbers using T-SQL.

The first concept that people seem to miss is that numbers aren’t strings. So there’s no concept of actually aligning a number, it’s only a string representation of a number that can be right-aligned or left-aligned. Or how it’s displayed in a client application.

But if you really want to create a string that has right-aligned numbers, then left padding of the number is what you want.

2017-12-22

SDU Tools: ListNonIndexedForeignKeys to Avoid a Code Smell

SDU Tools: ListNonIndexedForeignKeys to Avoid a Code Smell

I’ve written before about how important it is to find code smells.

One of these relates to foreign keys. I’ve argued in previous posts about the importance of foreign keys but if you do have them in place, you need to index them.

When SQL Server creates a primary key, it creates an index under the covers to support the primary key. The index has the same name as the key. (And is one of the reasons why you should name your primary keys and not let the system do it for you). A primary key needs to be both unique and NOT NULL. So SQL Server creates an index so it can quickly check if a value already exists.

2017-12-20

SDU Tools: Converting T-SQL Strings to PascalCase or camelCase

SDU Tools: Converting T-SQL Strings to PascalCase or camelCase

I’ve often had requests for code to convert strings to ProperCase and I described that in a recent blog post, but another common request is for either PascalCase or camelCase.

In PascalCase, all spaces between words are removed, the first letter of each word is capitalized, and the other letters in each word are lower case.

You can see it in this example:

camelCase is similar except that the first letter of the first word is not capitalized. The first letter of subsequent words are capitalized:

2017-12-15

SDU Tools: Find String Within a SQL Server Database (Did you mask your database properly?)

SDU Tools: Find String Within a SQL Server Database (Did you mask your database properly?)

I’ve mentioned that I work on a lot of financial systems. Masking and trimming the production databases so they can be used in development is important, yet I see many issues with masking.

A few months back, I was working on a SQL Server database from a large financial client, and the database was supposed to have been masked. However, what they missed was that the XML metadata associated with images stored in the database still had the actual client’s details. Any developer in the organization could have retrieved private details of all the members of the fund.

2017-12-13

SDU Tools: Split a Delimited String (like a CSV) Into Columns Using T-SQL

SDU Tools: Split a Delimited String (like a CSV) Into Columns Using T-SQL

I recently wrote about the tool we provide for splitting T-SQL delimited strings (like CSVs or comma-delimited strings). It outputs a row for every value.

Sometimes, however, I’m asked how I can get each value out into a separate column. Now that’s a little trickier without dynamic code because SQL Server wants to know the output metadata of the statement. SQL Server wants to know in advance what the output columns are called and what their data types are. This is especially important if you want to consume the output of the function in tools like SQL Server Integration Services (SSIS) or Biztalk.

2017-12-08

SDU Tools: New T-SQL Statement: STRING_SPLIT plus Split Delimited String Tool

SDU Tools: New T-SQL Statement: STRING_SPLIT plus Split Delimited String Tool

I’ve mentioned that I love it when SQL Server gets new T-SQL functionality.

A useful function that was added in SQL Server 2016 was STRING_SPLIT. You can see it in action in the main image for this post.

It’s another function that is great as far as it goes, but when I was a kid, this would be described as “nice try but no cigar”.

It works, and it’s fast, so what’s missing. I think three things:

2017-12-06

SDU Tools: Proper Case and Title Case (ie Don't Shout at Me)

SDU Tools: Proper Case and Title Case (ie Don't Shout at Me)

When writing emails or online text, it’s become common to see UPPER CASE TEXT AS A FORM OF SHOUTING.

Yet I see IT systems every day that still have text that humans are meant to read in all upper-case or all lower-case. Please don’t do that to your users.

But how do you fix text like that? We have a tool for that.

The SDU Tools collection has a ProperCase function and a TitleCase function that you can use in T-SQL and stop your SQL Server based applications from shouting at your users.

2017-12-01

SDU Tools: List Mismatched Data Types

SDU Tools: List Mismatched Data Types

I might be somewhat anal when it comes to database design. (Probably almost any type of coding). Perhaps it’s a mild form of  OCPD-behavior , but inconsistency frustrates me. (I’m told that OCPD is the correct term, and that people often apply OCD to that behavior incorrectly).

Worse, inconsistency leads to nasty, hard-to-find errors and your applications become more and more fragile.

If I’m holding an email address and I define it as 70 characters in one place, 100 in another, and 160 in yet another, I have the chance of random failures when I’m moving data around or assigning values from place to place. (For email addresses, I recently discussed why they should be 255 characters anyway).

2017-11-29

SDU Tools: List Subset Indexes

SDU Tools: List Subset Indexes

Everyone working with databases knows that having too many indexes can be a problem. Indexes need to be modified whenever the data in the associated tables need to be modified, so yes it can slow down data inserts, updates, and deletes.

How big a problem is too many indexes?

Overall, I’m not as concerned about this as many other people seem to be. I always want to focus on what the system is spending its time doing, and on most systems that I work on, that’s reading, not writing.

2017-11-24

SDU Tools: AsciiOnly and DigitsOnly

SDU Tools: AsciiOnly and DigitsOnly

This post covers another pair of useful functions that are part of the SDU Tools collection.

The first tool AsciiOnly deals with situations where you need to convert Unicode data (multi-byte data) to ASCII data (single byte). It processes each character and if it’s not in the ASCII range, it replaces the character with the value that you supply.  If your second parameter is an empty string, you’ll effectively strip out the non-ASCII characters, but your replacement can also be one or more characters.

2017-11-22