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

How to join Values Expand / Collapse
Author
Message
Posted Monday, January 14, 2013 11:26 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, May 9, 2014 7:21 AM
Points: 98, Visits: 251
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
Post #1407037
Posted Tuesday, January 15, 2013 1:35 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:09 AM
Points: 6,750, Visits: 13,896
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1407077
Posted Tuesday, January 15, 2013 1:59 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: Today @ 3:30 AM
Points: 3,420, Visits: 5,348
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

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, May 9, 2014 7:21 AM
Points: 98, Visits: 251
Thanks Experts Its work !
Post #1407579
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse