October 20, 2013 at 6:34 pm
sqlserver12345 (10/20/2013)
I do not want to use reverse function.please check the code I posted and correct what wrong I did.thanks
That's exactly what we're doing by showing you the right way! Assuming this is homework, you should be challenging your instructor by delivering outstanding results.
Easy if you happen to have a pattern-based string splitter in your toolbox.
WITH SampleData (palindrome) AS
(
SELECT 'Madam I''m Adam'
UNION ALL SELECT 'ABC'
UNION ALL SELECT 'ABBA'
UNION ALL SELECT 'A man, a plan, a canal - Panama!'
UNION ALL SELECT 'Madam in Eden, I''m Adam'
UNION ALL SELECT 'Doc, note: I dissent. A fast never prevents a fatness. I diet on cod'
UNION ALL SELECT 'Never odd or even'
),
SplitStrings AS
(
SELECT palindrome
,RemovePuncuation=
(
SELECT Item + ''
FROM SampleData b
CROSS APPLY dbo.PatternSplitCM(palindrome, '[A-Za-z]')
WHERE a.palindrome = b.palindrome AND [Matched] = 1
ORDER BY ItemNumber
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'
)
FROM SampleData a
)
SELECT palindrome
,IsPalindrome=CASE WHEN REVERSE(RemovePuncuation) = RemovePuncuation THEN 1 ELSE 0 END
FROM SplitStrings;
The PatternSplitCM FUNCTION can be found in the fourth link in my signature.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
October 20, 2013 at 8:44 pm
dwain.c (10/20/2013)
sqlserver12345 (10/20/2013)
I do not want to use reverse function.please check the code I posted and correct what wrong I did.thanksThat's exactly what we're doing by showing you the right way! Assuming this is homework, you should be challenging your instructor by delivering outstanding results.
Easy if you happen to have a pattern-based string splitter in your toolbox.
WITH SampleData (palindrome) AS
(
SELECT 'Madam I''m Adam'
UNION ALL SELECT 'ABC'
UNION ALL SELECT 'ABBA'
UNION ALL SELECT 'A man, a plan, a canal - Panama!'
UNION ALL SELECT 'Madam in Eden, I''m Adam'
UNION ALL SELECT 'Doc, note: I dissent. A fast never prevents a fatness. I diet on cod'
UNION ALL SELECT 'Never odd or even'
),
SplitStrings AS
(
SELECT palindrome
,RemovePuncuation=
(
SELECT Item + ''
FROM SampleData b
CROSS APPLY dbo.PatternSplitCM(palindrome, '[A-Za-z]')
WHERE a.palindrome = b.palindrome AND [Matched] = 1
ORDER BY ItemNumber
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'
)
FROM SampleData a
)
SELECT palindrome
,IsPalindrome=CASE WHEN REVERSE(RemovePuncuation) = RemovePuncuation THEN 1 ELSE 0 END
FROM SplitStrings;
The PatternSplitCM FUNCTION can be found in the fourth link in my signature.
The problem is, you used REVERSE which the OP said he couldn't do. Heh... you also forget that a Palindrome can contain numeric digits. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
October 20, 2013 at 8:50 pm
Jeff Moden (10/20/2013)
The problem is, you used REVERSE which the OP said he couldn't do. Heh... you also forget that a Palindrome can contain numeric digits. 😀
Nah... didn't forget. Ignored is more like it. We still haven't heard a good reason why REVERSE is not allowed.
Change the pattern matching string to [a-zA-Z0-9] to include digits. 😀
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
October 21, 2013 at 11:15 am
nothing just ...just learning some coding
October 21, 2013 at 1:31 pm
sqlserver12345 (10/21/2013)
nothing just ...just learning some coding
In that case, I strongly recommend that you drop the whole idea of using a WHILE loop and not using REVERSE. Might as well learn the right way to do something like this. 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
October 21, 2013 at 6:12 pm
Jeff Moden (10/21/2013)
In that case, I strongly recommend that you drop the whole idea of using a WHILE loop ...
Just in case the OP can't, I suppose there are lessons to be learned in doing things the wrong way...
DECLARE @Count INT = 1
,@String VARCHAR(8000) = 'ABBAXABBA';
SET @String = RTRIM(LTRIM(@String));
WHILE @Count <= LEN(@String) / 2 AND
RIGHT(LEFT(@String, @Count), 1) = LEFT(RIGHT(@String, @Count), 1)
SET @Count = @Count + 1;
IF @Count < LEN(@String)/ 2 PRINT 'Is NOT Palindrome';
ELSE PRINT 'Is Palindrome';
Obviously this ignores punctuation.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 6 posts - 16 through 20 (of 20 total)
You must be logged in to reply to this topic. Login to reply