The Bit Bucket

T-SQL 101: 81 Applying styles while using CONVERT

In a previous post about CAST and CONVERT, I mentioned that when you use CAST, you don’t get the option to specify the format you want to use. SQL Server offers an extension to the SQL standard with CONVERT and it lets you do just that.

CONVERT has an option parameter that lets you apply a style to the data type conversion that you’re doing.

In the example shown above, I’m converting the current date and time (based upon the SYSDATETIME() function) to a varchar(8), using style 112.

2021-01-18

SDU Tools: Converting to/from Unix time in SQL Server T-SQL

I do a lot of data conversion work each month. Many systems store date and time values in Unix format, so I often need to convert to/from these in T-SQL. Fortunately it’s easy.

A common way that Unix systems store the date and time value, is to store the number of seconds since the beginning of 1970.

The DateTime2ToUnixTime and UnixTimeToDateTime2 functions in our free SDU Tools for developers and DBAs, does this.

2021-01-15

Opinion: User Groups must avoid ever increasing depth

I’ve attended a lot of user groups over the years. They’ve been data-related, developer-related, and all sorts of other topics. I’ve also organized a lot of user groups over the years. One problem that I fell into early on, and that I see many others falling into, is the problem of increasing depth.

User group leaders often have a very skewed view of the content that’s been presented at their groups. That’s because they’ve often seen almost every single presentation that’s ever been given.

2021-01-14

BI: Can you explain where your analytic data came from?

I’ve seen many challenges with analytics over the years. One that’s painful is an inability to explain where analytic data came from. Someone looks at a report, sees a value, and says I don’t believe that number. Don’t put yourself in that position !

Lineage

I load analytics from data warehouses. Most of my data warehouses are SQL Server databases of some type. Currently, they’re almost always Azure SQL Databases. I like to include information in the database, about how the data got there i.e. the lineage of the data.

2021-01-13

SQL: GREATEST and LEAST - awesome new additions to Azure SQL Database

I still get really excited when T-SQL has new language elements added. I’m not talking about new data definition or management statements. I’m talking about new functions and logic. So, as expected, I was really happy when I saw some posts saying that GREATEST and LEAST now seemed to be working in Azure SQL Database.

They’ve been in other database engines before but weren’t part of T-SQL.

I tried them, and sure enough, they work as expected. NOTE: Intellisense hasn’t caught up yet, so they’re still flagged as syntax errors.

2021-01-12

T-SQL 101: 80 Converting data types with CAST and CONVERT

The rich set of data types in SQL Server and the T-SQL language are great. But sometimes, you need to change a value from one data type to another. The output from the commands above is:

CAST

ANSI SQL provided a way to do this.

CAST is the ANSI SQL way to convert from one data type to another. The syntax is basically:

CAST(ValueToConvert AS NewDataType)

And you can use it where you would have used an expression or constant.

2021-01-11

Opinion: Reports aren't tables

I love the way that tools like Power BI have made creating analytics approachable for a much wider range of users. The skills acquired when doing this are a great start for using analytics in enterprises, but an area that I still see missing is data modelling.

What I see users doing is this:

  • Decide what the output reports, etc. should look like
  • Design tables that match the reports.

And this is where things go wrong.

2021-01-07

SDU Tools: Strip diacritics from strings in SQL Server T-SQL

I try to allow strings in my applications to use all the richness of the available character sets. If someone has an accent in their name, I really want to repeat it back that way. But I’ve been asked many times if there’s a simple way to strip out all the accents, graves, etc. in T-SQL. These are called diacritics, and we’ve created a function to help you remove them if you decide you really must. (But as I said, really consider keeping them).

2021-01-07

BI: Azure Data Factory Copy Activities Won't Start (Queued)

I love working with Azure Data Factory (ADF). There are times though, when I’m stuck wondering what’s going on, and it takes me a moment to discover it.

One that had me puzzled recently was a Copy activity that just wouldn’t seem to start. The pipeline had started ok. Previous activities in the pipeline had run just fine. But when it got to the Copy activity, it said “Queued” and just kept saying that seemingly endlessly.

2021-01-06

SQL: Create a list of columns for a table using T-SQL and STRING_AGG

I often need to create a list of columns for a table. Sometimes it’s because I’m generating scripts but there are other times that I need it for dynamic code.

Let’s use WideWorldImporters as a sample. Imagine I need to get a list of columns for the Sales.Orders table.

The wrong way

I’ve seen people doing this the wrong way. They try to use a SELECT statement that joins a variable to itself like this:

2021-01-05