Friday, 11 September 2009

SQL Problem Solving #1

Problem:
I have a table with field values in double quotes, like: “blabla”.
Those quotes need to be removed for every field value, desired result: blabla

Tool:
SELECT '"TE"ST"',LEN('"TE"ST"')
SELECT REPLACE('"TE"ST"','"',''), LEN(REPLACE('"TE"ST"','"','')
)

Output:
(No column name) (No column name)
"TE"ST" 7
(No column name) (No column name)
TEST 4

Solution:
DECLARE @I INT
DECLARE @MAX INT
DECLARE @STMT NVARCHAR(MAX)
DECLARE @TABLE NVARCHAR(MAX)

SET @TABLE='table_name'
SET @MAX=number_of_columns
SET @I=0

WHILE @I<=@MAX
BEGIN SET @STMT='
UPDATE '+@TABLE+'
SET [COLUMN '+CAST(@I AS NVARCHAR)+'] = REPLACE([COLUMN '+CAST(@I AS NVARCHAR)+'],''"'','''')
WHERE 1=1'

EXEC(@STMT)
SET @I=@I+1
END

Monday, 7 September 2009

National Express Coach Driver Announcement

The bus moves off from Victoria Coach Station in London and the driver in a very lazy dire voice:

“Ping pong! This bus is to Liverpool and Manchester.” Pause…Passengers are puzzled and getting uncomfortable in their seats.

“I am only joking this bus is to Leeds. Now I have your attention, can I please quickly tell you about safety rules? Wear you seatbelt, it is for your own safety. There are emergency exits in the front and at the back, also two on the roof.” Chuckle…

“Please be considered to fellow passengers and make your phone calls short and sweet. We don’t want to know about your love life! Same goes for laptops, music players. Keep the volume down for others’ comfort. There is a toilet at the back, do use it seated! In case of emergency braking you wouldn’t want to walk down the aisle with your pants down. I hope you will enjoy your journey with National Express, if not you can always use Megabus.”

Chuckle…
“Thank you.”