Monthly Archives: November 2012

Removing a common substring from multiple records

This one goes in my “to remember” file.

I created an application to insert records into a database and one of the fields was username. The initial implementation prepended the username with the user’s domain (i.e., “DOMAIN\user”) due to the method used to obtain the username.

After a while I added new functionality to the application that added records in a different way and this time the username was added without the prepended domain name. I wanted to figure out a way to update all the old records with a single query and came up with this:

UPDATE Machines SET UserName = (SUBSTRING(UserName,CHARINDEX(‘\’, UserName) + 1, LEN(UserName) – CHARINDEX(‘\’, UserName) + 1))
WHERE Username LIKE ‘DOMAIN\%’

(Domain name changed to protect the innocent…)

As is usual in cases like this, be careful with update statements. Test well before unleashing on production data.

 

Advertisements

Being a Writer

“I am beginning to see that the rest of my life could be spent as an author, churning out books one after another to the endless interest of, say, fourteen people with Kindles.” – Neil Young

Motivation

This past Friday I got a general physical for the first time in many years. (Usually I just find a doctor when I have a specific complaint.) The doctor told me that I have hypertension and wanted to put me on blood pressure meds. I asked him if there was another way and he said he didn’t agree with not going onto meds but he did relent and gave me a one month diet/exercise/supplement plan and said that he wouldn’t agree to no meds unless I promised to follow the plan strictly. If my numbers don’t look better at the end of the month I agreed to start the meds.

Given this background, I started thinking about doing things we know we should be doing but don’t. I know I should have been living healthier for a while but haven’t taken time and steps to make it happen. Now I will do it, but I wonder why we things have to get so bad until we take action. Maybe it doesn’t work this way for everyone…