I recently was doing some development on my local machine and needed to pull data from a linked server. I set up the linked server and everything was working great. Executing queries to pull the data to my machine was fine, but a problem arose when I needed to execute a stored procedure from the other server. First of all, to call a stored procedure from a linked server you need to use a four...
Removing The Date From The End of a String
Occasionally there may be the need to remove the date from the end of a string. This can be accomplished by using PATINDEX along with SUBSTRING. We will use PATINDEX to find where the date begins and then SUBSTRING to remove the date from the end of the file. We will use ClientName03302010.csv and we want to get ClientName.csv from that to process. PATINDEX requires an expression to search...
Joining Tables That Contain NULL Values
As you can see we have two tables, with the same values. Joining on these tables is no problem at all. Simple, right? Well, take a look at what happens when we try to join on the column containing the NULL values. The results are the following: As you can see, we are missing a row. The row containing the NULL values did not get picked up by our join. We should have all three rows in the result...
Calculate the Date of Previous Sunday
There are a number of ways that you can calculate dates using T-SQL. A common practice is to find the first day of the week which can be accomplished easily by using the following function. SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) However, you will notice that this returns the date for Monday as SQL Server uses Monday for the start of the week. If your week starts on Sunday or you need to...
Use SQL from the Command Line
More often than not an execute SQL task in SSIS will do the trick. If not, then the data flow will accomplish whatever objective is set out. Occasionally those can become cumbersome in a package to accomplish something simple. In those cases using SQL from the command line could be the perfect fit. SQLCMD will allow SQL commands to be run from the command line in has the ability to define...
SQL Maintenance Plan Doesn't Delete Backups
I recently created a maintenance plan for our server to backup all the databases on a daily basis. In the cleanup step I set it to delete backups older than 7 day. I let the plan run for a week and checked on it to make sure the old backups were being deleted. This would be kind of a useless post if everything was working correctly, so as you guessed, they were not being deleted. I check...
SQL UPDATE Using a Join
You may need to do an update on joined tables to get a more conditional update. For instance, I have a Student table as well as an AcademicStatus table. The Student table contains all the students (profound, I know) and the AcademicStatus table tells if a student is in good standing, at risk, or has dropped out based on a StandingID. The Student table also lists a graduation date and a current...
Saving Changes Is Not Permitted
Have you ever created a table thinking everything looked amazing and was designed perfect only to come back and need to add to it? Maybe you need to change a data type, add a column, delete a column or move a column. It’s an easy fix, right. Just right click on the table, go to Design mode and make your changes. Well, this would be a pretty useless story if all that worked flawlessly. As...
Database Design
Designing a database can be a daunting task, especially on a large scale. There are several ways inside of SQL Server Management Studio to do design. You could create your database and right click on the ‘Tables’ node and enter all the information there. Column names are listed out followed by their data types with all the properties being set below. This is a quick and easy way to edit your...