Tag: syndicated

  • Load Balanced IIS Machines

    I noticed a contest this week while working on the Database Weekly newsletter. It’s the Cloud Hero contest, with the chance to win a Surface Pro 3. I could always use another device, or at least a device I could give away, so I decided to enter.

    There are a few things you can do, all of which are interesting to me in terms of a direction that I, and Red Gate, want to move. I don’t know if Azure works everywhere, but we are considering moving SQLServerCentral, or perhaps parts of it, to Azure, so this was a good chance for me to try out some new Azure stuff.

    I’ve messed with a few things in Azure, but mostly on the PaaS side. That interests me more, and I’ve done little with IaaS. I certainly haven’t really worked with IIS much in Azure. I decided to go through the VM setup, to create two IIS machines, load balanced on the same URL. I used this blog post with a cartoon and demo to run through the process.

    It was a bit more than 10 minutes, mostly because some of the allocation stuff in Azure took time, and the responsiveness from the VM in Azure was slow. From the time I connected to the time Server Manager popped up was over two minutes for each machine. Since I was going through some of the steps sequentially, that meant it was slow to get going.

    The video and the portal bring to light some of the issues of Azure. It’s a great tutorial and I was able to get the two machines load balancing IIS in 20 minutes (or less). It was surprised how quickly it went, but I also had to stop and think. The load balancing and cloud services are different now than they were when the post was written.

    I’m sure that’s the case with lots of Azure content. In some sense, this means that we will have lots of issues with people trying to learn how to use Azure as they’ll find content and information that is woefully out of date, sometimes quickly. I wonder if we need to think about having some code on blogs for Azure that marks the content as potentially out of date after it’s been out for 6 months.

    It’s a challenge to keep the content up to date, and luckily the changes weren’t too different in the portal.

    I am glad that I was able to get to IIS machines up and load balanced, delete them, and bring them back. That makes me think I may find some use for this Azure stuff, yet. I have a few projects in mind, including rebooting my personal site. Perhaps Azure will be the place I give it a go.

  • RegEx Those Dates

    This is a series on SQL Data Generator, covering some interesting scenarios I’ve run into. If you’ve never tried it, SQL Data Generator is a part of the SQL Toolbelt. Give it a try today with an evaluation today.

    Recently I was working on transforming some dates, and wanted to generate a large n number of dates for testing. I decided to use SQL Data Generator, and a little RegEx to meet my needs.

    The format I needed was CYYMMDD, which is the century as a 0 or 1 (1900 or 2000) and then the yymmdd format. While there are some pre-made expressions to build dates, there wasn’t an easy one to handle the century like this. I could have used a date expression in T-SQL and randomly allocated a century, but I decided to play around with RegEx.

    I know that the brackets allow a choice of values to be used. The regular expression can choose any of the values to match. An example is for the first part of my date, the century. It can be zero or one, so I can do this:

    [01]

    When Data Generator runs, it will randomly build expressions that match this pattern, which in my case results in

    regex01

    That makes it easy for me to pick numbers, and I could do something like this for the years:

    [01][0-0][0-9]

    That works, as any number from 00, as in 2000, up to 99, as in 1999, is valid. That gets me this:

    regex02

    Months

    However that causes issues when I get to the month. I need a two digit month, but I can’t have some combinations of two digits. If I were to write [01][1-9], I’d get months like 18, which aren’t valid. Instead, I need a pattern that only matches a 0 with 1 to 9, and only allows a 1 with a 1 or 2.

    To do that, I’ll use an OR. That’s a pipe (|) in regular expressions. I’ll say (in pseudocode), give me a (01 to 09) OR a (10-12). The easy way to build that is like this:

    (0[1-9]|1[0-2])

    This says that if we match the first half (before the pipe), then we literally have a 0 there, with a second character in the range 1-9. That gives us 01 to 09. The second half, after the pipe, does the same thing, but it matches a literal “1”, and then a 0, 1, or 2. As you can see, I have random months (only showing this expression).

    regex03

    Days

    Now the hard part: days.

    Days are strange in the calendar because the possible days depend on the months. Years and months are consistently in ranges, but the days are not. Let’s start with the most common days: 31.

    I have 31 days in months 1, 3, 5, 7, 8, 11, 12. In order to match these up, I’ll need to combine the month and day items. Let’s first change our months to be just those particular months. That gives me:

    (0[13578]|1[02])

    With these months, I am going to allow up to 31 days. The patterns for the first 29 days of the month are the same. A 0, 1, or 2, with any combination of 1-9. Putting that together gives me:

    (0[13578]|1[02])([012][1-9])

    This handles the first 29. The next two, 30 and 31, are an OR expression like the months. I’ll use a literal 3 and a choice of zero or one. That gives me:

    regex04

    Whew! This is a lot of work, but it matches things up well. Now I need to handle 30 days. I’ll do that the same way, but I’ll now OR both expressions together. The expression is:

    ((0[13578]|1[02])([012][1-9]|3[01]))|((0[2469]|11)([012][1-9]|30))

    And the data:

    regex05

    That gets me almost all the months. The last part of February, the hardest. Now I could worry about leap years, but I’m not going to. Proper handling here means verifying the year (and century here) and doing math to ensure a leap year is viable. Instead, I’m going to just ignore the 30s and manage days 1 to 29.

    ((0[13578]|1[02])([012][1-9]|3[01]))|((0[2469]|11)([012][1-9]|30))|((02([012][1-9])))

    Now I have a nice set of random dates if I put everything together.

    [01][0-0][0-9](((0[13578]|1[02])([012][1-9]|3[01]))|((0[2469]|11)([012][1-9]|30))|((02([012][1-9]))))

    regex06

    References

    I leaned on a few examples to decode a few of the expressions and also to check that I wasn’t messing up.

  • T-SQL Tricks – Trigger Your Memory

    I was scanning Twitter the other day and saw a note from someone that they had written a query using an obscure T-SQL command and were glad it had worked. I exchanged a note with the person and they mentioned that they had to look up the command and syntax periodically when they had to write a similar query.

    I mentioned templates.

    If  you haven’t used these, you should, and I wrote a basic post about how to access them and one on customizing these for yourself. These templates are like Snippets in SQL Prompt (Which are way more useful to me), and they are a tool every DBA should use.

    Here’s one way I think they’re really helpful:

    Suppose I need to write a PIVOT query. I rarely do this, and it’s not too hard, but I write this query:

    select
        *
      from
        ( select
              runner
            , miles
            , mins
            from
              results
        ) as rawdata pivot ( avg(mins) for [miles] in ( [3], [5], [10] ) ) as pivotresults
    ;
    GO

    That’s easy enough, but it’s specific for my tables. However when I glance at it, I can see that there’s an aggregate columns, and I know the PIVOT requires that I list the values that are to be used in the columns.

    What if I change the query? I can do this:

    select
        *
      from
        ( select
              runner
            , <pivotcol, varchar, miles>
            , <aggcol, varchar, mins>
            from
              results
        ) as rawdata pivot ( avg(<aggcol, varchar, mins>) for [<pivotcol, varchar, miles>] in ( [3], [5], [10] ) ) as pivotresults
    ;
    GO

    Now if I make this a template:

    templates7

    I can drag this into a new query window. When I see it, I can CTRL+Shift+M and get this:

    templates8

    Now I change a few values and I have a pivot.

    templates10

    Of course, I need to actually enter the values I want, but this gets my PIVOTs done quickly without the need to decode BOL or swing by SQLServerCentral. Once I do that, I have a query I can use.

    templates11

    I’d encourage you to use templates. They’re very, very handy for quick sections of code that you use often, or want to remember in the future.

  • Converting Dates

    I ran across a post recently where someone had dates stored as characters (never good), but also in this format: CYYMMDD. I’d never seen that, and thought it was strange. The person was asking for a way to convert this to YYYY-MM-DD, which I think it fairly easy.

    Let’s set up some data:

    CREATE TABLE RandomDates
    (
        myid INT
        , mydate VARCHAR(7)
    );
    GO
    INSERT dbo.RandomDates
            ( myid
            , mydate
            )
        VALUES
            ( 1, '0600102' )
           , ( 2, '1121004' )
           , ( 3, '0920415' )
           , ( 4, '1040611' )
            ;
    GO

    The format for the data is 7 characters denoted as century, year, month, day. The century is encoded, with the single value representing:

    • 0 = 1900
    • 1 = 2000

    That’s pretty straightforward and it means that we have two dates in the 1900s and 2 in the 2000s. The dates are:

    • January 2, 1960
    • April 15, 1992
    • June 11, 2004
    • October 4, 2012

    Querying these dates and converting them is straightforward, but there are a couple ways to do this. If we are just looking to convert these to characters, I saw this solution from ZZartin.

     SELECT CONVERT(DATETIME, CASE 
                                WHEN LEFT(mydate, 1) = '1' 
                                    THEN '20' 
                                    ELSE '19' 
                                END 
                                + RIGHT(rd.mydate, 6)
                    , 112)
      FROM dbo.RandomDates AS rd;

    That’s fairly straightforward, and overall I like it. It uses simple functions and puts things together.

    I had another idea, mostly because I initially favored keeping the items separate as parts of the date in case I needed them. I thought about DATEFROMPARTS, which is a SQL Server 2012+ function. My thought was to calculate each of the parts and send them into the function like this:

    SELECT DATEFROMPARTS(
                        CASE WHEN LEFT(mydate, 1) = '0'
                            THEN 1900
                            ELSE 2000
                            END + SUBSTRING( rd.mydate, 2, 2)
                        , SUBSTRING( rd.mydate, 4, 2)
                        , SUBSTRING( rd.mydate, 6, 2)
                        )
     FROM dbo.RandomDates AS rd
     ;

    My idea has more function calls, and I’d think it would take longer to build, but I’m not sure. Let’s test.

    I’ll use Data Generator to insert a few million rows into this table. Then let’s run both pieces of code.

    The first code, from ZZartin, required about 19,000 logical reads and this execution plan when I ran it a few times. The CPU execution was in the 40k ms, and about 9 sec of real time.

    dates1

    The second code, mine, had about 30,000 logical reads, with only 8k CPU ms, but about 40sec of real time. The execution plan:

    dates2

    That’s interesting, and it matches what I’d expect. The first code is much simpler, intuitively, and it’s easy to read. With the format of the date essentially in order, it doesn’t make sense to try and "assemble" the date from parts. It’s easier to convert the first character to two (with the CASE) and then just cast this as a date.