The Bit Bucket

SDU Tools: Date Dimension Period Columns in SQL Server T-SQL

SDU Tools: Date Dimension Period Columns in SQL Server T-SQL

The DateDimensionColumns function in our free SDU Tools for developers and DBAs, has been really popular. It provides the standard columns that are part of a typical date dimension in a data warehouse. But we’re especially proud of a new function that can be used to expand a date dimension with really useful additional information. We call it DateDimensionPeriodColumns.

The DateDimensionColumns function had the usual columns:

Date, Day Number, Day Name, Short Day Name, Month Name, Short Month Name, Month Number, Month Label, Year, Year Label, Day of Year, Fiscal Month Number, Fiscal Month Label, Fiscal Year, Fiscal Year Label, Day of Fiscal Year, ISO Week Number

2020-05-13

Opinion: Does a human respond to your website contact requests?

Opinion: Does a human respond to your website contact requests?

Most websites that I visit have a link at the bottom of the page that suggests that you can use it to contact either the website team or the company that owns the site. (Might not be the same people) Based on years of trying, my expectation of ever getting a response from using one of these links is close to zero.

If you have a website that has a contact link, does it lead anywhere sensible?

2020-05-12

T-SQL 101: 69 Extracting date components in SQL Server T-SQL with DAY, MONTH, and YEAR

T-SQL 101: 69 Extracting date components in SQL Server T-SQL with DAY, MONTH, and YEAR

When you’re writing T-SQL code, it’s really common to need to extract components of a date, and the most common need is for year, month, and day. So T-SQL has separate functions just for that.

You can see the output from this query here:

The YEAR(), MONTH(), and DAY() functions take a date (or datetime, datetime2, smalldatetime) as input and return an integer for the year, month, or day.

2020-05-11

SQL: How do I stop my database files going into the Users folder with localdb?

SQL: How do I stop my database files going into the Users folder with localdb?

I’ve seen questions on the forums asking about where localdb stores its database files. Often, the question is about how to stop them going into the C:\Users folder. Clearly, that doesn’t seem to be a good spot for them.

localdb stores database files in the C:\Users area by default as it’s trying to find a location that the current user will have permission to write to. While I understand the rationale, that’s not where most users will want those files.

2020-05-08

SQL: Why didn't my SQL Server transaction roll back on a primary key violation?

SQL: Why didn't my SQL Server transaction roll back on a primary key violation?

There’s a question that I hear all the time in SQL Server forums:

Why didn’t the transaction roll back when a primary key violation occurred?

Take a look at the code in the main image above. Would you expect it to execute the second and third INSERTs if the first INSERT failed with a primary key violation? So many people would. If you’re one of them, read on.

By default, that’s not how SQL Server works.

2020-05-07

SDU Tools: Extracting initials from a name in SQL Server T-SQL

SDU Tools: Extracting initials from a name in SQL Server T-SQL

I recently came across a requirement to extract someone’s initials from within their name. That was a new one for me, so we added a new function into our free SDU Tools for developers and DBAs. It’s called InitialsFromName.

It’s a straightforward scalar function that takes two parameters:

@Name nvarchar(max) is the name to extract the initials from

@Separator nvarchar(max) is a separator placed between the returned initials.  (Make it an empty string for none)

2020-05-06

Opinion: On forums, don't do DBTs (drive-by trashings)

Opinion: On forums, don't do DBTs (drive-by trashings)

I hear about frequent drive-by shootings in some countries. Fortunately that doesn’t happen where I live. But what I come across all the time on Q&A forums, is what I’d like to call DBTs (Drive by trashings).

It usually starts when someone makes a genuine effort to try to help answer a question. The DBT (drive-by trasher) pops in and leaves a nasty unhelpful message. It could be “That’s misleading” or “That’s wrong” or “You don’t understand how it works”.

2020-05-05

T-SQL 101: 68 Current date and time in SQL Server T-SQL with SYSDATETIME and SYSDATETIMEOFFSET

T-SQL 101: 68 Current date and time in SQL Server T-SQL with SYSDATETIME and SYSDATETIMEOFFSET

It’s really common to need to know the current date and time in T-SQL code. Ever since the beginning of the SQL Server product, we’ve had a function called GETDATE(). It returns the current date and time of the server as a datetime value.

In SQL Server 2008, we got new higher precision data types, and so new current date and time functions were added as well.

SYSDATETIME() is a function that returns the current date and time as a datetime2 value. And SYSDATETIMEOFFSET() returns the timezone offset for the server as well.

2020-05-04

Opinion: Don't add pages to your website if you're not going to update them

Opinion: Don't add pages to your website if you're not going to update them

Today I wanted to call out a common mistake that I see at websites all over the country. Don’t add pages to your website if you’re not going to update them.

I’m particularly talking about pages with names like “News”, “Articles”, “Blog Posts”, etc. They’re often added when someone first builds a website and is full of hope for how it will be used.

And then it isn’t.

Old News

I’ve lost count of how many sites I visit where there’s a News section and when I visit it, there are two or three entries, often years apart. Or worse, there are a few entries from five years ago when the website was first created.

2020-05-01

FIX: SQL Server Replication - Cannot alter column because it is 'REPLICATED'

FIX: SQL Server Replication - Cannot alter column because it is 'REPLICATED'

In SQL Server 2005, I really appreciated the addition of DDL replication.  Before that, I found that making schema changes to replicated tables was a pain in the neck. Almost every time, I was there dropping replication and later setting it up again. That’s easy but takes a long time, so sometimes, I’d resort to lots of manual hacking.

Using DDL replication, I could make a whole lot of different types of changes and the subscribers would be updated automatically, at least in most standard subscriptions.

2020-04-30