The Bit Bucket

SDU Tools: Time Period Dimension Columns in T-SQL

In a previous SDU Tools post, I described how to calculate the columns required for a date dimension. Another related tool that we have in our free SDU Tools for developers and DBAs, is a function that creates the columns needed for a time period dimension. It’s called TimePeriodDimensionColumns.

You supply the starting time, and the number of minutes per time period,  and it supplies the output columns.

You can see it in action in the main image above, and in this video:

2019-02-20

SQL: The outcome of how DateDiffNoWeekends should work

In a recent post, I discussed questions that had come up about how we should calculate the number of days between two given dates, but excluding the weekends i.e. people just wanted the number of weekdays.

This relates to one of our SDU Tools called DateDiffNoWeekends. Some customers using it felt that it didn’t calculate the days in the way they thought it should. And on reflection, I agreed.

Much of the discussion centred around how we should treat start and end dates, depending upon whether they were on the weekends or not.

2019-02-19

T-SQL 101: 5 Logging on to SQL Server

Before, you can access anything on SQL Server, apart from knowing what to call the server, the server itself needs to know who you are. This process is called authentication.

In SQL Server, a login is person that the server has identified.

Now access to the server by a login doesn’t mean that you can access anything much on the server, it’s just the first step where you get the server to trust who you are.

2019-02-18

SQL: Calculating Day of the Week Reliably in T-SQL

Some T-SQL functions like DATEPART work differently based upon your session settings. If you want to write reliable code, you should always check for these issues, and try to write code that’s impervious to these settings.

Let’s take finding the day of the week as an example. It might seem that to find out if a day is a Sunday, you could just do this:

DECLARE @DayToCheck date = ‘20190224’;

2019-02-15

Shortcut: Clear server list in SSMS connection dialog

SQL Server Management Studio (SSMS) keeps a list of the server names that you have connected to, and prompts you with those when you drop-down the list while making a connection:

Eventually, that list can either become messy, it can include servers that don’t exist anymore, and so on. You might want to clear up the list.

To do this in early versions of SSMS, you needed to locate the SqlStudio.bin file from the Documents and Settings area in your user profile.

2019-02-14

SDU Tools: Calculate Date Dimension Columns in T-SQL

Calculating the columns required for a date dimension using T-SQL is a pretty common request from SQL Server developers. So we decided to make it really easy to do. In our free SDU Tools for developers and DBAs, there is a table-valued function called DateDimensionColumns, for just this purpose.

You supply a date, and the month that your financial year starts in, and it supplies the output columns.

You can also easily combine it with our DatesBetween function to get dimension columns for a range of dates:

2019-02-13

Question: How should DateDiffNoWeekends work?

One of the tools we have in our free SDU Tools for Developers and DBAs is a version of DATEDIFF that excludes weekends. It’s DateDiffNoWeekends.

This has been a very popular function.

One of the customers who is using these tools found that it was returning different results to what he expected. And it got me re-thinking how it should work.

Let’s start by assuming that weekends are Saturday and Sunday. That’s what most people seem to assume.

2019-02-12

T-SQL 101: 4 What are Server Instances in SQL Server?

Before you can start to execute queries against SQL Server, you need to connect to the server. The first concept that you need to understand is what a server instance is.

You need to know is what name you should use to refer to the server. As an example, if I have a computer called SDUPROD and it has a copy of SQL server installed on it, we could often just connect to the name SDUPROD. If we do that, then what we’re doing is connecting that what’s called the default instance of SQL Server.

2019-02-11

SDU Tools: T-SQL tools for working with Chinese Calendars and Years in SQL Server

To celebrate Chinese New Year this week, I thought I should write about some options that we recently added to our free SDU Tools for developers and DBAs, for working with Chinese calendar concepts.

Let’s start with the basic one: when is Chinese New Year? We added a function called DateOfChineseNewYear. You just supply our year number (Gregorian calendar), and it will tell you when Chinese New Year is.

You can see it in use in the image above, along with the much more cute function that tells you what the Chinese Zodiac animal is for the year. It’s called ChineseNewYearAnimalName.

2019-02-08

Shortcut: Change default text in new query window in SSMS

In SQL Server Management Studio (SSMS), when you click New Query, a new query window is opened, but because it’s blank, what might not be immediately obvious is that it’s based on a template.

The location of the template depends upon the version, but for SSMS 17.6, you’ll find it in this folder:

C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\SqlWorkbenchProjectItems\Sql

The file is called SQLFile.sql.

One of the things that I often forget to do is to change my connection to the correct database. Let’s add a USE statement to make that obvious.

2019-02-07