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 123»»»

Uncommon Table Expressions Expand / Collapse
Author
Message
Posted Saturday, August 09, 2008 11:47 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, April 17, 2014 2:13 PM
Points: 188, Visits: 424
Comments posted to this topic are about the item Uncommon Table Expressions



PeteK
I have CDO. It's like OCD but all the letters are in alphabetical order... as they should be.
Post #549795
Posted Monday, August 11, 2008 1:12 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, March 19, 2014 1:27 AM
Points: 2,366, Visits: 1,837
really liked the article...

"Keep Trying"
Post #550041
Posted Monday, August 11, 2008 1:49 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, April 14, 2014 1:25 AM
Points: 85, Visits: 256
I was surprised to see the implementation of CTE recursion (in 2005 at least) performed similarly too inbuilt functions for small string lengths (read napkin test of up to 400) for the last example compared to

 SELECT @str2 = SUBSTRING(@Str1,1,@offset-1) + @char + Substring(@Str1,@offset+1,len(@str1)) 

The elapsed time was comparable

The recursive reported measurable cpu time and 2 worktable scans
The inbuilt did not report anything

Forgive if I am new to this but does that mean that inbuilt functions don't add to reported CPU ?

The inbuilt in a scalar select didn't generate a query plan on execution; I guess it wasn't considered worth caching.
Post #550067
Posted Monday, August 11, 2008 3:04 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, March 20, 2014 7:32 AM
Points: 5,191, Visits: 1,368
Nice article :)


Post #550080
Posted Monday, August 11, 2008 3:47 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 10:46 AM
Points: 3,241, Visits: 4,987
Nice Article. I would definitely like to go with this technique as there are very limited scenarios where you need huge documents to get parsed character by character. I have checked this routine for about 20,000 characters and it gave results in approx. 1 sec. Quite good.

Nice and informative...


Atif Sheikh


----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sometimes, winning is not an issue but trying.

You can check my BLOG here

Post #550091
Posted Monday, August 11, 2008 4:03 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, May 07, 2009 6:48 AM
Points: 3, Visits: 32
Recursive CTE's are great for answering hierarchical questions, but usages such as this strike me as a bit lame. Despite your disclaimer, it looks as though less reasoning types will use this way beyond the narrow scope intended by the article.

I'd much rather see the Database Vendor support regular expressions in a more comprehensive manner (e.g perl-compatible etc).

select replace(PhoneNumber, '\D', '', 'g') as "Cleaned Phone Number"
from .......
......
Post #550097
Posted Monday, August 11, 2008 4:48 AM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Wednesday, January 02, 2013 12:15 PM
Points: 1,443, Visits: 711
Good article!
Post #550115
Posted Monday, August 11, 2008 5:27 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, March 25, 2014 10:26 AM
Points: 200, Visits: 611
Those who might use 'The REPLACE_Nth_CHAR Function' might also be interested in STUFF (deletes a specified length of characters and inserts another set of characters at a specified starting point).
Post #550137
Posted Monday, August 11, 2008 7:08 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Monday, April 14, 2014 1:34 PM
Points: 15,442, Visits: 9,588
The "replace Nth character" doesn't actually catch the Nth instance of a particular character, it just replaces the Nth character.

Try it on "Susy sells sea shells by the sea shore.", and try using it to replace the third "s". As written, it doesn't even have an input parameter (variable) for which character to replace, nor does adding one to the case statement accomplish this.

What it really does is simply replace the third character (or whichever character you use in the case statement). Since that can be done much more easily with Stuff, I don't see a purpose for that particular piece of code. If you really want to replace the third "s" in that string, here's how I'd go about it:

declare @String varchar(1000), @String2 varchar(1000),
@Letter char(1), @Occurance int, @Replace char(1)

select @String = 'Susy sells sea shells by the sea shore.',
@String2 = '',
@Letter = 's',
@Occurance = 3,
@Replace = 'X'

;with Chars ([Char], [Pos], Ord) as
(select substring(@String, number, 1),
row_number() over (partition by substring(@String, number, 1) order by number),
number
from common.dbo.smallnumbers
where number between 1 and len(@String))
select @String2 =
@String2 +
case
when [Char] = @Letter and [Pos] = @Occurance then @Replace
else [Char]
end
from Chars
order by ord

select @String2

As far as parsing out a string character-by-character, a Numbers table can do that just as easily and just as fast, and can deal with arbitrarily large strings. That's mentioned in the article, but I have to ask: Why have two different functions for the same thing? If you're going to have one based on a Numbers table, why also have one based on a CTE?

For the phone number and other regex type functions, speed tests have been done by members of this site, and we found that a CLR regex out-performs this CTE method. Testing and discussion at:

http://www.sqlservercentral.com/Forums/Topic470379-338-1.aspx

The article is okay, but there are better solutions for each of these things.

(In the Replace Nth CTE I built above, I reference my Common database and SmallNumbers table. I keep a database, called "Common" with a number of tables and functions that other databases reference. Much like a code library. The SmallNumbers table is 0 - 1,000.)


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #550216
Posted Monday, August 11, 2008 9:43 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 8:31 AM
Points: 2,285, Visits: 4,222

Forgive if I am new to this but does that mean that inbuilt functions don't add to reported CPU ?

CPU and IO for a UDF are NOT reported by setting statistics on.

The solution that I use is to save the values from the sysprocesses table for the @@spid and then after the SQL runs, calculate the difference from sysprocesses.

You will notice that there is a significant usage of CPU when a UDF is used.


SQL = Scarcely Qualifies as a Language
Post #550374
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse