How to join Values

  • Hi Professional ,

    Create Table,

    CREATE TABLE abc

    (num int)

    Insert Into

    insert into abc values (1010)

    insert into abc values (111)

    insert into abc values (99)

    insert into abc values (0)

    SELECT 'D'+ CAST(num AS NVARCHAR) FROM abc

    Now I am getting Result as below,

    D1010

    D111

    D99

    D0

    But I have to show result as like below,

    D1010

    D1110

    D9900

    0

    So please suggest me how to do this ?

    Regards,

    KRAj

  • SELECT 'D' + LEFT(CAST(num AS VARCHAR(4))+'0000',4)

    FROM abc

    SELECT ISNULL('D' + LEFT(CAST(NULLIF(num,0) AS VARCHAR(4))+'0000',4),'0')

    FROM abc

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Or like this:

    SELECT CASE num WHEN 0 THEN '' ELSE 'D' END +

    RIGHT(num*POWER(10, 3-FLOOR(LOG10(CASE num WHEN 0 THEN 1 ELSE num END))), 4)

    FROM abc

    What??? ๐Ÿ˜›


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    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

  • Thanks Experts Its work !

Viewing 4 posts - 1 through 3 (of 3 total)

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