Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 How to join Values Rate Topic Display Mode Topic Options
Author
 Message
 Posted Monday, January 14, 2013 11:26 PM
 SSC Journeyman Group: General Forum Members Last Login: Friday, November 29, 2013 2:49 AM Points: 92, Visits: 241
 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,D1010D111D99D0But I have to show result as like below,D1010D1110D99000So please suggest me how to do this ?Regards,KRAj
Post #1407037
 Posted Tuesday, January 15, 2013 1:35 AM
 SSCertifiable Group: General Forum Members Last Login: Today @ 9:41 AM Points: 6,748, Visits: 12,843
 `SELECT 'D' + LEFT(CAST(num AS VARCHAR(4))+'0000',4) FROM abcSELECT 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 ShawFor 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 ModenExploring Recursive CTEs by Example Dwain Camps
Post #1407077
 Posted Tuesday, January 15, 2013 1:59 AM
 Hall of Fame Group: General Forum Members Last Login: Today @ 6:01 PM Points: 3,590, Visits: 5,096
 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!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?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!
Post #1407087
 Posted Tuesday, January 15, 2013 9:33 PM
 SSC Journeyman Group: General Forum Members Last Login: Friday, November 29, 2013 2:49 AM Points: 92, Visits: 241
 Thanks Experts Its work !
Post #1407579

 Permissions