SQL Server: Like with Underscores

There are times when you need to do a LIKE based search and need to find an underscore.  I was surprised to find the underscore is reserved as a single character wild card.

There are two ways to get this done.  First is the explicit ESCAPE definition as follows where the backslash is the escape character.

-- looking for anything with an underscore
SELECT  *
FROM    dbo.SomeTable
WHERE   ColumnName like '%\_%' ESCAPE '\'

To me, a better approach is surrounding the character is square brackets.

-- looking for anything with an underscore
SELECT  *
FROM    dbo.SomeTable
WHERE   ColumnName like '%[_]%'

The second approach may get a little confusing when looking for brackets though.

-- Looking for '[' + sometext + ']'
SELECT  *
FROM    dbo.SomeTable
WHERE   ColumnName like '%[[]%[]]%'

Hope this helps.

ASP.NET Code: Register Script in Head Section

Registering a client-side script in ASP.NET is normally done with the Page.RegisterClientScript or Page.RegisterClientScriptInclude.  Both of these add the appropriate script reference into the resulting body of the HTML.  However there are times when it is nice to have the script added to the head section of the page.  I have written two extension methods to accomplish the task using the same naming convention as the Microsoft provided functions.

        /// <summary>
        /// Registers the client script include in the head section.  If the
        /// same ID is sent, the source file is replaced with the new one.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="id">The control ID to make sure there aren't two
        /// registered under the same ID</param>
        /// <param name="src">The file name.</param>
        public static void RegisterHeadClientScriptInclude(this Page page, string id, string src)
        {
            HtmlGenericControl si = null;
            foreach (Control c in page.Header.Controls)
            {
                if (c.ID != id) continue;
                si = (HtmlGenericControl) c;
                break;
            }
            if (si == null)
            {
                si = new HtmlGenericControl { ID = id, TagName = "script" };
                si.Attributes.Add("type", "text/javascript");
                si.Attributes.Add("src", src);
            }
            else
            {
                si.Attributes["src"] = src;
            }
            page.Header.Controls.Add(si);
        }

        /// <summary>
        /// Registers the client script code block in the head section of the page.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="id">The id.</param>
        /// <param name="code">The code.</param>
        public static void RegisterHeadClientScriptBlock(this Page page, string id, string code)
        {
            HtmlGenericControl si = null;
            foreach (Control c in page.Header.Controls)
            {
                if (c.ID != id) continue;
                si = (HtmlGenericControl)c;
                break;
            }
            if (si == null)
            {
                si = new HtmlGenericControl { ID = id, TagName = "script" };
                si.Attributes.Add("type", "text/javascript");
            }
            si.InnerText = code;
            page.Header.Controls.Add(si);
        }

Please comment if you find it interesting or crap, I would like to know either way.

Pump-Up a Bagel

I love everything bagels but the local place sometimes runs out but I have a solution to the problem.   McCormack makes these spice grinders and I found the Italian Herb grinder to be perfect.  So here’s the recipe:

Bagel, cream cheese, McCormack Italial Herb Seasoning Grinder

Smear up the bagel as normal, grind on the seasoning and flavor love is on the way.

Links:
McCormack Site: The Italian Herb Seasoning Grinder