T/SQL Splint String/Text

One common task performed in database applications is given a full name, how can this be split into the first name and last name.  In this article, three different methods are discussed to perform this task using three different functions in SQL Server.  It is assumed in this article that the full name is just made up of one first name and one last name. Using SUBSTRING Function The first method is the use of the SUBSTRING string function,…

Read More

Tips for Effective SQL Server Database Maintenance

“ I have been asked several times for advice on how to effectively maintain a production database. Sometimes the questions come from none-DBAs who are implementing new solutions and want help fine-tuning maintenance practices to fit their new databases’ characteristics. As with the majority of tasks and procedures in the IT world, there isn’t an easy one-size-fits-all solution for effective database maintenance, but there are some key areas that nearly always need to be addressed. My top five areas of…

Read More

T-SQL: RIGHT, LEFT, SUBSTRING and CHARINDEX Functions

This article explains the base functionality and uses of the LEFT, RIGHT, SUBSTRING and CHARINDEX functions in SQL. Samples script: Sample Results: Variable Data Results ——————– HELLO WORLD RIGHT Results ————- RLD LEFT Results ———— HEL CHARINDEX Results —————– 6 SUBSTRING Results —————– LO WO Combined Results ——————– HELLO  

Read More

UPDATE From SELECT Statement – Using JOIN in UPDATE Statement – Multiple Tables in Update Statement

In recent times I have seen a developer writing a cursor to update a table. When asked the reason was he had no idea how to use multiple tables with the help of the JOIN clause in the UPDATE statement. However, the easiest and the most clean way is to use JOIN clause in the UPDATE statement and use multiple tables in the UPDATE statement and do the task. Let us see the following example. We have two tables Table 1…

Read More

SQL Joins

SQL joins are used to combine rows from two or more tables. SQL JOIN An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them. The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN return all rows from multiple tables where the join condition is met. Let’s look at a selection from the “Orders” table: OrderID CustomerID OrderDate 10308 2 1996-09-18 10309 37…

Read More

Querying the tables using JOIN Keyword

The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match. The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The…

Read More

Posted in Reference, SQL Tagged Comments Off on Querying the tables using JOIN Keyword
SELECT columns using the LIKE keyword

Using the WHERE clause to return on the records in the Employee table where the employees are a manager of some type. This example uses the % character for any number of characters. Using the LIKE keyword to return records from the Person table where users’ first names end in “ary”. This example uses the _ characters to represent a single character. Using the LIKE keyword to return records from the Person table where users’ first names end in…

Read More

Selecting Domain from Email Address

Recently I came across a quick need where I needed to retrieve domain of the email address. The email address is in the database table. I quickly wrote following script which will extract the domain and will also count how many email addresses are there with the same domain address.

Read More

Microsoft SQL Insert Trigger to Get Current Datetime Value

This script gets the datetime value in this format MM/DD/YYYY:HR:MI:SSPM or AM. I could have used the CAST or CONVERT option but the did not reproduce the result that I wanted.  There are some option in CONVERT option but they converts into a 24hr format. Here is my trigger for insert event. USE dwdata GO /* —————————————————————————– www.myw0.com, Version 1.0, Creation Date: 11/05/2014 Functionality: Generate Datetime values into a text field —————————————————————————– */ if exists (select * from sysobjects…

Read More