Sunday, March 4, 2007

Table Column To CSV

The following query converts a column of data in a SQL table to a comma-delimited string. This works great for tables with a few hundred rows and limited to the size of the VARCHAR data type. A TEXT field can also be use if needed for large tables, but requires a little more work.

DECLARE @CSV VARCHAR(1000)
DECLARE @Delimiter CHAR(1)

SET @Delimiter = ','

SELECT
@CSV = COALESCE(@CSV + @Delimiter, '') + lastname FROM dbo.employees

PRINT @CSV

Reorder data in a SQL table

Recently a friend asked whether I had any T-SQL code that would reorder data in a given table, such as the table in Listing 1 below. The table controls the order that certain reports are printed.

PK ID REPORT
100 1 Balance Sheet
101 2 Cash-Flow Statement
102 3 Expense Report
103 4 Income Statement
104 5 Profit and Loss Statement
105 6 Quarterly Financial Report
106 7 Securities and Exchange Commission
107 8 Statement of Capital
108 9 Statement of Earnings
109 10 Statement of Retained Earnings
Listing 1: Reports Table

The user would specify the id to change (oldid) and value to change it to (newid). However, it's not just updating the oldid to the new one, but also changing any impacted ids. For example, changing id 6 to 3 would result in 3 changing to 4, 4 changing to 5 and 5 changing to 6. The process would be reverse if changing id 3 to 6.

The following is one approach that works.

-- Add additional checks
IF( @oldid = @newid ) RETURN

IF( @oldid < @newid ) BEGIN
-- Decrement the ids
UPDATE Reports SET id = id - 1 WHERE id > @oldid AND id <= @newid
END ELSE BEGIN

-- Increment the ids
UPDATE Reports SET id = id + 1 WHERE id >= @newid AND id < @oldid
END


-- Change the specified id to the new value
UPDATE Reports SET id = @newid WHERE
pk = @pk

If the table doesn't have a primary key the following approach can be used:

-- Add additional checks
IF( @oldid = @newid ) RETURN

-- Change the oldid to an arbitrary value
UPDATE Reports SET id = -1 WHERE
id = @oldid

IF( @oldid < @newid ) BEGIN
-- Decrement the ids
UPDATE Reports SET id = id - 1 WHERE id > @oldid AND id <= @newid
END ELSE BEGIN

-- Increment the ids
UPDATE Reports SET id = id + 1 WHERE id >= @newid AND id < @oldid
END

-- Change the specified id to the new value
UPDATE Reports SET id = @newid WHERE
id = -1

Monday, February 26, 2007

Solve Instead of Selling

A few years back, I was hire to do sales support. However, what was really needed in my region was a salesperson, and I ended up doing sales and sales support for almost two years. The tips below, by Erin Flynn (http://www.flynnmedia.com/) in The CostCo Connection, January 2007, hit the nail on the head and were a few of the tenets that allowed me to make head way in the world of sales as well as software development.

Most people don't like sales, but whether you realize it or not we are all selling something, whether it's a product, a service, an idea or just trying to convince someone else of an alternative approach.

So you are not comfortable selling. Then stop selling. People love to buy but hate to be sold. What people like is having their problems solved. The number-one rule of selling is: The business or salesperson that solves the customer's problems, needs and wants in the easiest way will get the sale.

Customers don't want to be sold. So stop selling and start solving the customer's problems, needs and wants. Don't sell me a refrigerator; sell me the solution to keeping my food cold. Don't sell me a car; sell me a comfortable way to get from one place to another.

Make sure you are the expert about your products and services, and show your customers how purchasing your products and services will solve their problems, needs and wants.

by Bob Janet (www.BobJanet.com), sales consultant/trainer, speaker and author of Join the Profit Club


Most sales professionals wait to handle objections until prospects bring them up, but that strategy – or lack of strategy – isn’t nearly as effective as eliminating objections before making your presentation.

As a “sales doctor”, your first overall step with a potential client is to perform an examination – a qualifying interview – to find out where it hurts. During the examination, you find out if the prospect has a budget. Only after the examination is well under way do you offer a prescription by making a presentation. In the interview process, you effectively diagnose and dispose of the majority of objections.

The first two steps of the interview process are pre-call planning, and bonding and establishing rapport, which means doing your homework on the person you’re calling, and, once on the phone, getting that person to feel comfortable with you.

by Brian Azar (
www.SalesDoctor.com), coach, author, speaker and trainer


To help with "doing your homework", Harvey Mackay created the Mackay 66, which is the most popular handout downloaded from his Web site, www.harveymackay.com. Check it out if you are not already familiar with it. According to Harvey, its help him and his entire national sales force at Mackay Envelope Company humanize their selling strategy.