Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Tally Table Uses - Part II


Tally Table Uses - Part II

Author
Message
Sioban Krzywicki
Sioban Krzywicki
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1134 Visits: 8067
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
ron.carlton 41182
ron.carlton 41182
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
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.
Nadrek
Nadrek
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1029 Visits: 2673
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.
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21055 Visits: 18258
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Adam Haines
Adam Haines
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2324 Visits: 3135
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search