## Tally Table Uses - Part II

 Author Message Sioban Krzywicki SSC-Insane Group: General Forum Members Points: 22260 Visits: 8146 Comments posted to this topic are about the item Tally Table Uses - Part II --------------------------------------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 Andrew G SSChampion Group: General Forum Members Points: 10971 Visits: 2378 Thanks, interesting way of looking at the problem. Also you could change the WHERE clause to `WHERE N <= DATALENGTH(CountryName)` instead of using the SUBSTRING comparison. The example given in Books Online uses a WHILE loop for a similar problem.. It would be nice to know the real world speed differences in these 2 approaches for this particular dataset.`SET TEXTSIZE 0SET NOCOUNT ON-- Create the variables for the current character string position -- and for the character string.DECLARE @position int, @string char(15)-- Initialize the variables.SET @position = 1SET @string = 'Du monde entier'WHILE @position <= DATALENGTH(@string) BEGIN SELECT ASCII(SUBSTRING(@string, @position, 1)), CHAR(ASCII(SUBSTRING(@string, @position, 1))) SET @position = @position + 1 ENDSET NOCOUNT OFFGO` Stuart Davis SSC Veteran Group: General Forum Members Points: 262 Visits: 23 Nice example. Although I have encountered this problem many times and normally start by doing something even simpler, just selecting out the column and then len(column) - if the character length is higher than the characters I see I know I have a hidden/non-ascii character issue and it's usually when I've imported data from elsewhere and normally char 160 is the culprit! Mike Dougherty-384281 SSCrazy Group: General Forum Members Points: 2236 Visits: 944 We store html fragments in varchar fields. I learned that smartquotes and emdash need special handling to display properly, so instead I applied special handling to remove them. I did not know the position or the character code in thousands of rows of content. I wrote the following to identify rows to be fixed and also what and where a fix was needed:(assumes markup table has "content" varchar column; Tally has int field N)`select top 1000 m.[PKId] ,[position] = n.[N],[character]= substring(m.[markup], n.[N],1),[ascii] = ascii( substring(m.[markup], n.[N],1) )from markup m join (select [N] from Tally where [N] <= 255 ) n on ascii( substring(m.[content], n.[N],1) ) in ( select [ascii] = [N] from Tally where [n] not between ascii('A') and ascii('Z') and [n] not between ascii('a') and ascii('z') and [n] not between ascii('0') and ascii('9') and ( '@.-_' not like '%' + char([n]) + '%' or char([n]) = '%' ) )` kenglish-729097 SSC Rookie Group: General Forum Members Points: 36 Visits: 6 Interesting application of Tally, but when I am looking for issues like this I just cast into varbinary and compare the resulting hex strings. This seems much simpler than rotating all the text character by character. Sioban Krzywicki SSC-Insane Group: General Forum Members Points: 22260 Visits: 8146 kenglish-729097 (8/3/2010)Interesting application of Tally, but when I am looking for issues like this I just cast into varbinary and compare the resulting hex strings. This seems much simpler than rotating all the text character by character.What does that tell you other than that they don't match? How do you use the hex strings to see what is different? I'll try this myself, but I'd love to hear the explanation as well. --------------------------------------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 Frank Bazan SSCarpal Tunnel Group: General Forum Members Points: 4588 Visits: 1087 What a cool application of the tally table. When I have string manipulation issues, I've always taken the educated guess / trial and error approach much like other posters, but this is a great way to visualise what you're working with.Thanks for the article Kindest Regards,Frank Bazan Sioban Krzywicki SSC-Insane Group: General Forum Members Points: 22260 Visits: 8146 Frank Bazan (8/3/2010)What a cool application of the tally table. When I have string manipulation issues, I've always taken the educated guess / trial and error approach much like other posters, but this is a great way to visualise what you're working with.Thanks for the articleThanks. Being able to see the granular detail all at once is a big part of why I like this approach. --------------------------------------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 David Lester SSCrazy Group: General Forum Members Points: 2839 Visits: 1542 Great article Stefan. A few months ago I was given a project to find all the symbols in our entire system's data. Specifically to find SQL symbols (%,@ etc). We have around 40 Gb of data, and the majority of it is character strings. I used this same approach, and it suprised me at its speed. I was expecting to have to parse through several billion characters. The process ran for around 5 hours, which was much better than I expected. This was a one time run, so I did not spend a lot of time optimizing it, but it does make me wonder just how fast this could be tweaked to. Charles Kincaid SSChampion Group: General Forum Members Points: 10703 Visits: 2384 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. ATBCharles Kincaid

## 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
 SQL Server 2017      SQL Server 2017 - Administration      SQL Server 2017 - Development SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 15 - Administration      SQL Server 15 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions      CosmosDB      Azure Data Lake      Azure Machine Learning Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization      Security and Auditing SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      Question of the Day (QOD)      SQL Server Security Skills Microsoft Access      Microsoft Access Products and Books      Third Party Products         CA         SQLCentric         Extreme Technologies.         Innovartis         Embarcadero         Sonasoft         Golden Gate Software         Idera         Lumigent         Red Gate Software         Quest Software         ApexSQL         SQL Sentry      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss T-SQL Books          Discuss DTS Books          Discuss SQL Server 2000 Books         Discuss SQL Server 7.0 Books         Discuss Data Warehousing Books  Notification Services      Administration Article Discussions Future Versions      SQL 12