Category: Blog

  • Changing Values in T-SQL–#SQLNewBlogger

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

    Recently I ran across a question posted by a beginner on the Internet and thought this would be a good, basic topic to cover. The question was: how can I replace a value in a comma separated string in a table?

    This post covers the basics of this task.

    Scenario

    Suppose you have some strings in a table, and they contain multiple values. I see this often when application developers serialize some data. For example, I might create a table like this:

    CREATE TABLE mytable
    (   mykey INT NOT NULL CONSTRAINT mytablepk PRIMARY KEY
       , myval VARCHAR(100));
    GO
    
    INSERT dbo.mytable
         (mykey, myval)
    VALUES
         (1, 'apple,pear,banana')
       , (2, 'pear,peach,melon');
    GO
    
    SELECT * FROM dbo.mytable AS m;

    This has a few rows of multiple values in a field.

    2021-01-04 12_09_31-SQLQuery17.sql - ARISTOTLE.sandbox (ARISTOTLE_Steve (84))_ - Microsoft SQL Serve

    Imagine now I need to change pear to grape in all rows. I want a simple solution to do this.

    Solution

    I have seen some people try to use complex substring calls paired with other functions to do this, but T-SQL gives you a really simple solution. We have a REPLACE() function that allows us to change a string without parsing it.

    The simple way to do this is like this:

    SELECT
          m.mykey
        , m.myval
        , REPLACE(m.myval, 'pear', 'grape') AS newstring
    FROM dbo.mytable AS m;

    Always run a SELECT before an UPDATE, but in this case, I can see that pear has been removed and grape is in its place.

    2021-01-04 12_17_05-SQLQuery17.sql - ARISTOTLE.sandbox (ARISTOTLE_Steve (84))_ - Microsoft SQL Serve

    REPLACE() works by passing in a string as the first parameter, then a second string to search for, pear in this case, and finally a replacement. I could then put together an UPDATE statement to change my table.

    UPDATE dbo.mytable
      SET myval = REPLACE(myval, 'pear', 'grape')
    FROM dbo.mytable AS m;

    If I run this, the results shown above for newstring will replace the myval string for all rows.

    SQLNewBlogger

    This is an example of a basic type of T-SQL solution that is simple, with a quick explanation. I answered this for someone and then spent 10 minutes writing this up.

    A good story to have ready for an interview.

  • Daily Coping 6 Jan 2021

    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 be kind to the planet. Use less energy today.

    This isn’t always easy to do, but I am trying to be a little better about this. As my kids leave the house, with the last one going in a couple weeks, I know that there should be less energy usage around here. I’ve made it a point to shut down my computer more often, and also kill more electronics that don’t need to be plugged in and running. There are some we rarely use, so flipping a power switch on a strip is the best way to reduce some usage.

    I’m also trying to make less trips to the gym and store, combining things when I can. Small changes, but lots of small changes add up.

  • Daily Coping 5 Jan 2021

    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 call a relative who is far away to say hello and have a chat.

    This is an easy one this time of year. If you didn’t make time during the holidays, do so now. For me, I try to call my Mom every week or two and chat a bit. We also email and text, but it’s good to keep in touch.

    My brother is busy, with three kids in high school now, and we don’t talk enough, but I reached out a few times across the last few weeks to chat and say hi. We even had a family video chat on Christmas day.

    I also joined my wife in reaching out to her sisters, who we haven’t seen in a long time because of the pandemic.

    I’m not a big talk-on-the-phone person, but it was good to reach out and touch a few others.

  • Saving PASS Pro Videos

    PASS will close down their websites on 15 Jan 2021. That’s just a couple weeks away, and if you’re someone that paid for a year of PASS Pro access, like me, you might want to save the videos so you can still go through the courses. I have no idea if someone will buy the assets and make them available, but I did contract for a year of access, so I believe I can save them for personal use.

    NOTE: DO NOT PUBLISH OR DISTRIBUTE THESE TO ANYONE ELSE. That would be illegal and a copyright violation. It may also break other laws.

    Here’s what I did to save off my own personal copies.

    First, you need to log into the PASS site. In your dashboard, you should see a PASS Pro section. Click that.

    2021-01-04 10_36_40-PASS _ MYPASS

    This take you to the PASS Pro site. Here, you need to click “Learn” and then pick the item you want. I was more concerned with the current videos than older ones, so I chose the Educational Series.

    2021-01-04 10_37_06-Educational Series - PASS

    From here, I pick one. For example, I was interested in Dr. Greg Low’s Cloud Transformation. For the series, you need to go through it quickly, but there are some assessment gates. For example, I couldn’t easily pick Item 4, because I hadn’t started item 3.

    Design issues aside, click on an item and view the video.

    2021-01-04 10_38_31-PASS_ Cloud Transformation

    This opens a popup with the video. Click Play, and once it starts, you can right click and “Save Video As”.

    2021-01-04 10_38_09-PASS_ Cloud Transformation

    If you don’t click Play, you’ll be saving the HTML page, which isn’t what you want.

    I went through the series, and it took just a few seconds to start a video and save it off. I could then close it and do the next one. Once I’d started a video, even for 3-4 s, I could get to the next one.

    2021-01-04 10_47_46-PASS_ Cloud Transformation

    The caveats here are that there are assessments, and you cannot get past them until you go through them. Take some guesses, as you have a few chances, but note your answers. Maybe you can answer well enough to get past them. If not, here were my quick guesses on module 1 for cloud:

    2021-01-04 10_50_52-PASS_ Cloud Transformation

    2021-01-04 10_51_17-PASS_ Cloud Transformation

    I passed:

    2021-01-04 10_51_26-PASS_ Cloud Transformation

    On to module 2.

    Hopefully this helps. If someone gets an automated way to grab these, I’ll update this post.