Palindrome-SQL

  • Hi Experts,

    I want to know whether a string stored in a table is palindrome or not.

    for example,

    Table: Emp

    Eno Ename Status

    1 Karthik

    2 MADAM

    3 Matt

    4 Steve

    5 LEVEL

    6 Jhone

    7 LIRIL

    8 Toner

    9 ROTATOR

    10 Gila

    I want to update the status to 'PALINDROME' (or) 'NOT PALINDROME' based on the letter format.(I hope all our members knows about Palindrome)

    Thanks in advance.

    karthik

  • UPDATE Table1

    SET Status = CASE WHEN Col1 = REVERSE(Col1) THEN 1 ELSE 0 END


    N 56°04'39.16"
    E 12°55'05.25"

  • UPDATE Emp

    SET Status = CASE WHEN Ename = REVERSE(Ename) THEN 'Palindrome' ELSE ' No palindrome' END


    N 56°04'39.16"
    E 12°55'05.25"

  • I dont want to use REVERSE() Function. I need pure sql code logic which could check whether it is palindrome or not.

    karthik

  • Why don't you want to use REVERSE? Is this a homework question?

    John

  • We can use REVERSE() Function.I agreed.

    But my friend faced the above question in his recent interview.Interviewer asked him not to use REVERSE() Function.But he failed to answer. Thats why i mentioned.

    karthik

  • Karthik

    Try this.

    John

    DECLARE @word varchar(20)

    DECLARE @length smallint

    SET @word = 'AbleWasIEreISawElba'

    SET @length = LEN(@word)

    IF EXISTS (

    SELECT *

    FROM master.dbo.spt_values

    WHERE type = 'P'

    AND number between 1 and @length/2.0 + 1

    AND SUBSTRING(@word,number,1) <> SUBSTRING(@word,@length-number+1,1)

    )

    PRINT 'Not palindrome'

    ELSE PRINT 'Palindrome'

  • John,

    Can you explain me the logic ?

    karthik

  • C'mon, Karthik - do some of the work yourself! What don't you understand?

    John

  • @length/2.0 + 1

    I am not able to understand the above statement.Why are you dividing it by 2 and add with 1.

    karthik

  • Karthik

    We're checking that the first character is equal to the last, the second is equal to the second to last, and so on. You only need to do that for the first n characters of the word, where n is length/2 if there are an even number of characters, or length/2 + 1 if odd. As an example, if there are 10 characters, it does it where number < 6 (ie 5 times), and if there are 11 characters it does it where number < 6.5 (ie 6 times). (In fact, it really only needs to do it (length-1)/2 times, since the middle character will always be equal to the middle character.) The reason for dividing by 2.0 instead of 2 is that this gives you a result accurate to one decimal place, instead of just an integer.

    Hope that helps

    John

  • Karthik,

    This is what I spoke of on one of the other posts... it's also why people get so angry with you. You banter the "Senior Software Engineer" label all over the place, but you do not think like one. Take some time and analyze the code instead of asking what the logic is... it'll take you longer, but you'll learn more... a lot more.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • karthikeyan (12/21/2007)


    @length/2.0 + 1

    I am not able to understand the above statement.Why are you dividing it by 2 and add with 1.

    Are you a front-end developer or back-end developer?

    What do you do in your organisation?


    Madhivanan

    Failing to plan is Planning to fail

  • Jeff Moden (12/21/2007)


    Karthik,

    This is what I spoke of on one of the other posts... it's also why people get so angry with you. You banter the "Senior Software Engineer" label all over the place, but you do not think like one. Take some time and analyze the code instead of asking what the logic is... it'll take you longer, but you'll learn more... a lot more.

    I agree with you


    Madhivanan

    Failing to plan is Planning to fail

  • I think some times people expect the FORUMS to give them the FINE results which saves them all the work. It looks like they dont want to learn fishing, but just want to get a fish when they need it.

    It happened to me in one of the MSDN forums that I asked the user some clarification about the problem while trying to help him out. He replied in rude (I guess that is what a message in UPPER CASE letters mean) which said something like "IF YOU HAVE READ MY POST CAREFULLY ......". The message looked like he is very busy and does not have enough time to explain his problem clearly. But he expected some one to provide a clean solution without he being involved.

    🙂

    .

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic. Login to reply