Add a column if it doesn't exist to all tables?

Using MSSQL I needed to add a column to every table in a legacy database because I wanted to see whenever a new record was added. Needed a 'createdate' field that defaulted to getdate(), Instead of having to do each table one at a time I was able to find a query that did it all for me. Saved me a couple of hours. Thanks to Add a column if it doesn't exist to all tables?

Here is the code:

view plain print about
1EXEC sp_MSforeachtable '
2if not exists (select * from sys.columns
3 where object_id = object_id(''?'')
4 and name = ''CreatedOn'')
5begin
6 ALTER TABLE ? ADD CreatedOn datetime NOT NULL DEFAULT getdate();
7end';

Earning my money now cause I just shaved off a couple hours of billable time for my client.

Microsoft SQL Server

Parsing Phone number in Sql Server Removing non numeric characters

I inherited a system that has a few quirks. Great system but some of the stuff is hard to deal with. I was asked to make the phone number search work better. They are having a hard time with it cause you have to enter the phone number exactly like it is in the field. i.e. (444) 843-2234 and if you don't include everything it doesn't return any results. Not very convenient.

What we wanted was to be able to type any portion of that phone number, including just a few digits, i.e. 444843 and have the matching records come up. So I need to do two things. Remove any non numeric characters from the inputted value and search the database field which had non numeric characters.

The solution, and it seems to work, even though it feels totally hackish the following:

To clean up the inputted value I use:

view plain print about
1varphone = rereplace(form.phoneNumber,"[^[:digit:]]","","ALL")

To search the database with that value I used:

view plain print about
1WHERE SUBSTRING(phonenumber,2,3) + SUBSTRING(phonenumber,7,3) + SUBSTRING(phonenumber,11,4) like '%#varphone#%'

Now this assumes a consistent phone format like this '(444) 843-2234' which is the case in our database.

That saved a lot of reworking code and it works like a charm.

Resetting Wordpress password

I recently inherited an old WordPress site that nobody had used for over a year. So nobody could remember their username and password. I needed the admin password to be able to go in and make the adjustments that they had asked me to make. I have access to the database on the back end and all the files on the server via ftp so I started searching for solutions.

I found this page that seems to be the goto place for resetting a wordpress password. I needed to reset the admin one. All the info seems to be here: Resetting Your Password

I tried several of those inlcuding FTP and the Emergency Password Reset Script. This site is using Microsoft SQL Server so some of the others didn't apply. I had tried simply adding my own email to the admin user account in the database but that didn't work. I got a mail error.

Then, searching google, I found this page: Is it possible to calculate MD5 hash directly in T-SQL language?

The solution was found on that page where there is the following SQL statement:

view plain print about
1UPDATE T_WHATEVER_YOUR_TABLE_NAME_IS
2 SET PREFIX_Hash = LOWER(SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('MD5', LOWER('yournewpassword'))), 3, 32) )

This worked and created the password and I was able to login.

More about varchar(max)

I am trying to understand the varchar(max) data type. I cam across a really old article but it had the exact answers I was looking for. Specifically when do I go from using varchar with some number i.e. varchar(700) to varchar(max)

From This blog post:

  • The size range for VARCHAR is now 1 to 8,000 or MAX. Note there is nothing between 8,000 and MAX.
  • VARCHAR(MAX) is more than a replacement from TEXT. It works just as well with short strings as well as long ones. Therefore, if there is any possibility that you'll exceed 8,000 bytes then use VARCHAR(MAX).
  • The maximum size for a VARCHAR(MAX) is 2^31 - 1 bytes / characters.

    I hope, even though this article was from 2006 that this stuff is all still relevant.

  • Microsoft SQL Server to remove text datatype in future release

    I was browsing google searching for information on data types as I was creating a new table in my db. Just wanted to make sure I was doing things right and I came across this snippet notifying of the following:


    Important

    ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

    Fixed and variable-length data types for storing large non-Unicode and Unicode string and binary data. Unicode data uses the UNICODE UCS-2 character set.


    So in this particular table I did use varchar(MAX) instead of text. Didn't even know there was varchar(max) or I forgot.

    Slow Query when filtered a certain way

    Wasn't sure how to title this post. My users were complaining of a slow lookup. Thing is, it was only slow in certain regions. Other regions were fine. I found the offending queries and compared two. I only changed the id which caused the lookup to filter based on region. For example. Find all companies in California, or find all companies in Texas.

    When filtering by Texas it was substantially slower. In SQL Server Management studio I choose "Query/Include actual Estimation Plan" and ran the two queries. It gives a lot of good info but in the header for the slow one I also see something about a Missing index. It is in green on my screen. Right clicking on that header area I have an option "Missing Index Details" which, when selected gives me the sql to create the query that I need. I did it manually but could have run that script to add the needed index.

    Adding this recommended index took the query time from 6 seconds to less than 1 second (almost instant). Sweet. Now off to optimize some other queries (could go on all day :-) ).

    SQL exporting to csv fields with line breaks

    So I am trying to export data from an old billing application we use to a new one. Having a problem transferring notes because there is one field that contains multiple messages separated by line breaks or something. So when I open a sample of the export in excel some lines are messed up because it kicks down to the next line when it encounters one of these line breaks. The solution was found here

    And here is the exact code I used to fix that particular column that was messed up:

    REPLACE(CAST(message AS VARCHAR(MAX)), CHAR(13) + CHAR(10), '') as msg

    SQL Server substring function

    I am trying to prepare some data for import and needed the zip and the zip +4 in separate fields. But in my data source they are one field. I remembered the "mid" function from way back and wondered what the sql alternative was. It is "substring". So what I needed was:

    view plain print about
    1left(zip,5) as zip, substring(zip, 6, 4) as zip4

    Problem with CFClientVars datasource

    All of the sudden today we started to get ajax errors on our website. We hadn't done anything or checked in any code, no database changes, nothing. Couldn't figure out what was happening but a lot of the ajax functionality we use is the built in coldfusion ajax stuff. Running one of these ajax calls by itself we got an error about the datasource. Turns out the log file for that database was HUGE. It was full and causing the problem. I contacted my hosting provider and they sent me a link to an article: http://www.sqlcleanup.com/2008/sql-2005-truncating-log-files-and-recovering-space/

    To summarize; the fix was easy and I have done this before, you just need to shrink the log file as explained in the article above:

    To figure out the name of your database and log files:

    view plain print about
    1use <yourdatabasename>
    2exec sp_helpfile

    Then, to actually truncate the log file for a database you run the following (This is all SQL)

    view plain print about
    1USE <yourdatabasename>
    2GO
    3BACKUP LOG <yourdatabasename> WITH TRUNCATE_ONLY
    4GO
    5DBCC SHRINKFILE (<yourdatabaselogfilename>, 1)
    6GO
    7DBCC SHRINKFILE (<yourdatabasedatafilename>, 1)
    8GO
    9exec sp_helpfile

    many-to-many database joins-don't use them

    In a previous post just a couple of days ago I explained how I returned some data from a many-to-many join. In reality I don't use a many-to-many join although I always consider it such. I use a many-to-one on each of the 2 outside tables and 2 one-to-many relations on the middle linking table. The rational is that many-to-many are hard and I usually need some real information from the linking table anyway, like, for example, the date the link was created.

    Looking at the Coldfusion ORM mailing list I came across a thread which included a post by Joe Rinehart that links to the hibernate documentation that tells you how they feel about them.

    Do not use exotic association mappings:

    Practical test cases for real many-to-many associations are rare. Most of the time you need additional information stored in the "link table". In this case, it is much better to use two one-to-many associations to an intermediate link class. In fact, most associations are one-to-many and many-to-one. For this reason, you should proceed cautiously when using any other association style.

    (http://docs.jboss.org/hibernate/core/3.3/reference/en/html/best-practices.html)

    More Entries