Gone Again

I’m off again. By the time this publishes, I should be in Amsterdam (hopefully). I’m traveling to DataGrillen, but because I had surgery and in a boot, I’m moving slowly.

This week is traveling to DataGrillen and back, hopefully without any issues.

Back to normal blogging next week.

Posted in Blog | Tagged , | Comments Off on Gone Again

Daily Coping 31 May 2022

I started to add a daily coping tip to the SQLServerCentral newsletter and to the Community Circle, which is helping me deal with the issues in the world. I’m adding my responses for each day here. All my coping tips are under this tag.

Today’s tip is to share a quote you find inspiring to give others a boost.

“Get busy living or get busy dying.”Stephen King

As I get older, I feel the weight of time and the pressure to enjoy life more and focus on what I experience rather than what I have. This quote reminds of of that. I want to pack more things into life rather than letting it pass me by.

This doesn’t mean I’m racing through a bucket list, though I am trying to do things that I find intriguing and interesting. However, more I want to engage in life with friends, appreciate the time with others, and enjoy it to the fullest extent.

Less time with a screen where I can and more time listening and talking with others. More time looking at the world around me and changing my view. More time doing things that make me smile with my wife and kids.

Those can be standing at the fence and watching horses.

20220325_111809

It could be traveling to another country and staring at the vast sky in our world with my daughter.

20190604_154938_HDR

It could be standing on a sideline and coaching kids on how to strive for something and compete with others.

FB_IMG_1648477456207

This is about living life in a way that makes it better than it might be if you let it pass you by.

Posted in Blog | Tagged , , | Comments Off on Daily Coping 31 May 2022

Finding Memorial Day–#SQLNewBlogger

Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers. Here are some hints to get started.

It’s Memorial Day in the US. A holiday, though I’m off on a trip to Germany today.

I wanted to have a fun Memorial Day Question of the Day today, and I decided to write some code to calculate Memorial Day. This post looks at how the code works.

The Algorithm

Memorial day is always the last Monday in May. For me, I decided to find all the Mondays in May and then take the last one.  I started with a tally table to get a list of days. In any given year, we can find the first day of the year with this code:

DATEADD (yy, DATEDIFF (yy, 0, GETDATE ()), 0)

Now I use a DATEADD with my tally table to find the first 200 days of the year.  The end of May will always fall in this number of days. Here is the code for a list of the first 200 days of the current year:

WITH myTally (n)
AS ( SELECT n = ROW_NUMBER () OVER (ORDER BY
                                       (SELECT NULL))
      FROM
        ( VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) a (n)
        CROSS JOIN ( VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) b (n)
        CROSS JOIN ( VALUES (1), (2)) c (n) )
    , cteCurrYearDates (myDate)
AS ( SELECT DATEADD (DAY, n, DATEADD (yy, DATEDIFF (yy, 0, GETDATE ()), 0))
      FROM myTally)
    , cteMay (Mondays)

Once I have this, I now need to get the Mondays in May. I can use the DATEPART and MONTH functions to find this. Actually, I ought to use DATEPART to be consistent here, but my habit is MONTH() for the month.

I also need to set the DATEFIRST for this code, otherwise the day of the week will be inconsistent. Here’s the code for this:

SET DATEFIRST 7;
WITH myTally (n)
AS ( SELECT n = ROW_NUMBER () OVER (ORDER BY
                                       (SELECT NULL))
      FROM
        ( VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) a (n)
        CROSS JOIN ( VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) b (n)
        CROSS JOIN ( VALUES (1), (2)) c (n) )
    , cteCurrYearDates (myDate)
AS ( SELECT DATEADD (DAY, n, DATEADD (yy, DATEDIFF (yy, 0, GETDATE ()), 0))
      FROM myTally)
    , cteMay (Mondays)
AS ( SELECT cteCurrYearDates.myDate
      FROM cteCurrYearDates
      WHERE
        DATEPART (WEEKDAY, cteCurrYearDates.myDate) = 2
        AND MONTH(cteCurrYearDates.myDate) = 5
)

This gives me a list of Mondays in May for the current year. I want the last one, which isn’t easy to do in a result set. However, I can get the first one with a TOP 1 limit. The easy way to get the last one is reverse the order of the rows and then take the first one. I do this with an ORDER BY.

Here’s the complete code:

SET DATEFIRST 7;
WITH myTally (n)
AS ( SELECT n = ROW_NUMBER () OVER (ORDER BY
                                       (SELECT NULL))
      FROM
        ( VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) a (n)
        CROSS JOIN ( VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) b (n)
        CROSS JOIN ( VALUES (1), (2)) c (n) )
    , cteCurrYearDates (myDate)
AS ( SELECT DATEADD (DAY, n, DATEADD (yy, DATEDIFF (yy, 0, GETDATE ()), 0))
      FROM myTally)
    , cteMay (Mondays)
AS ( SELECT cteCurrYearDates.myDate
      FROM cteCurrYearDates
      WHERE
        DATEPART (WEEKDAY, cteCurrYearDates.myDate) = 2
        AND MONTH(cteCurrYearDates.myDate) = 5
)
SELECT --TOP 1
        Mondays
FROM cteMay
ORDER BY cteMay.Mondays DESC;

Now I can get Memorial Day for the current year.

SQL New Blogger

This is a great example of breaking down an algorithm and explaining it to the reader. If you write T-SQL code for a living, you might write a series of posts on how you solve various problems in T-SQL and explain the process. Link to places where you learn, and show some results that give a feeling for how you built the code.

This took about 10 minutes to write once I’d built all the code. I didn’t go into details with individual result sets, but you could easily do that to show how the code works.

Posted in Blog | Tagged , , | Comments Off on Finding Memorial Day–#SQLNewBlogger

Daily Coping 30 May 2022

I started to add a daily coping tip to the SQLServerCentral newsletter and to the Community Circle, which is helping me deal with the issues in the world. I’m adding my responses for each day here. All my coping tips are under this tag.

Today’s tip is to recall three things you’ve done that you are proud of.

I hope there are a lot of things I’m proud of that I have had accomplished in my life. Here’s a quick list of the first few that come to mind.

1. SQL Server Central – What started as a way to help others and build a business (and hopefully make some $$$) turned into a passion and an enjoyable experience for me on  daily basis. The editorials started out as a way to spice things up and turned into a part of my life I enjoy immensely. However, the things I am most proud of here is helping people. I’ve gotten lots of notes about how people have learned and grown from the site, which makes me proud.

2. Fundraising – I’ve raised funds a few times for friends who were going through a tough time. I am proud that I was able to help someone in need.

3. A few projects, but I see these often as I move around the ranch or leave/come home. I know my wife appreciate these, and we certainly could have paid someone to do this, but I enjoyed the projects and had some time with my kids who helped build the shelters.

20220420_091613

The horses use them in the winter. Apparently in the summer they’re too lazy to go inside.

Posted in Blog | Tagged , , | Comments Off on Daily Coping 30 May 2022