Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Tally Table Uses - Part II Expand / Collapse
Author
Message
Posted Tuesday, August 3, 2010 9:39 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 11:44 AM
Points: 2,751, Visits: 7,174
Charles Kincaid (8/3/2010)
Great article. If you have to parse strings in SQL then having the tally table is great.

I would have appraoched the problem with a different tool set.
SELECT '|' + CountryName + '|' FROM Country

This would have shown me that there were hidden characters. Pasting a snipet into my favorite text editor and hovering over one of the bad apples whould have gotten me to writing the replace statement.

Yet since you have to show the value of Tally I think that you did it very well. I'm looking forward to the next installment. I'm still trying to sell certain managament of the value of Tally and Dates.


You're looking forward to the next installment? I'd better come up with one then! : -) I'm sure I'll find more uses in what I'm doing currently and I'll write them up when I do.


--------------------------------------
When you encounter a problem, if the solution isn't readily evident go back to the start and check your assumptions.
--------------------------------------
It’s unpleasantly like being drunk.
What’s so unpleasant about being drunk?
You ask a glass of water. -- Douglas Adams
Post #962933
Posted Tuesday, August 3, 2010 12:01 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, July 24, 2013 3:24 PM
Points: 37, Visits: 68
Excel is not a friendly place to "clean things up". I have frequently found Excel to insert unfriendly characters into data, and often to truncate data strings.
Post #963029
Posted Tuesday, August 3, 2010 5:02 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Thursday, July 24, 2014 8:41 AM
Points: 861, Visits: 2,357
Charles Kincaid (8/3/2010)
Great article. If you have to parse strings in SQL then having the tally table is great.

I would have appraoched the problem with a different tool set.
SELECT '|' + CountryName + '|' FROM Country

This would have shown me that there were hidden characters. Pasting a snipet into my favorite text editor and hovering over one of the bad apples whould have gotten me to writing the replace statement.



The above is a reasonably simple way of doing it; also, cut and pasting from a cell in the grid into the editor in SSMS/Query Analyzer does the same thing; you can see that there's something else there.

From the article, however, I spot what I consider to be the real issue:
"I found the list for countries on Wikipedia. I copied the first 4 columns of the table, dropped them into Excel to clean them up and imported the result into a SQL Server table. I created the table and used the wizard to pull the data in."

While the tally table exercise was entertaining, what I see as the real issue is that the data that was imported into SQL Server was never actually inspected. In cases like this, I generally have two "standard" ways of doing things:
1) Generate a text file for a BULK INSERT/BCP... then pull it up in a hex editor (HxD or your favorite) to see what's what. This instantly shows you everything, right down to end of lines, end of pages, and so on.
2) Use EXCEL to generate INSERT statements, i.e.
=CONCATENATE("INSERT INTO table VALUES ('",A1,"')")
Again, you'd see the extra characters instantly between the tick marks in your CREATE statement.

I try to always look at the source data first; looking at the end result generally takes long.

That said, an interesting use of the tally table character splitter technique.
Post #963179
Posted Tuesday, August 3, 2010 11:11 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 10:18 AM
Points: 21,221, Visits: 14,922
Thanks Stefan, another great article on uses for a tally table.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #963234
Posted Wednesday, August 4, 2010 3:12 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, July 25, 2014 10:06 AM
Points: 2,278, Visits: 3,052
Thanks for the article. I have actually used this technique myself and find it very useful. I do a little more filtering to get the unwanted characters. Here is something similar to what i use.

DECLARE @t TABLE(col VARCHAR(MAX));
INSERT INTO @t VALUES ('zzzzzzzzz' + CHAR(13));
INSERT INTO @t VALUES ('zzzzzzzzz' + CHAR(11) + CHAR(13));
INSERT INTO @t VALUES ('zzzz');
INSERT INTO @t VALUES ('zzzz-');
INSERT INTO @t VALUES ('zz.zz');
INSERT INTO @t VALUES ('zz?zz');

SELECT
col,
n AS Pos,
SUBSTRING(col,n,1) AS [ASCII_Char],
ASCII(SUBSTRING(col,n,1)) AS [ASCII_Cd]
FROM @t t
INNER JOIN dbo.[Numbers] n ON n.n <= LEN(col)
WHERE SUBSTRING(col,n,1) LIKE '[^A-Za-z0-9/-.?]' ESCAPE '/' --use escape to build your exception list





My blog: http://jahaines.blogspot.com
Post #963847
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse