• As a great Jedi once said: "Do, or do not, there is no try..."

    SET NOCOUNT ON;

    --== SOME SAMPLE DATA ==--

    IF object_id('tempdb..#testEnvironment') IS NOT NULL

    BEGIN

    DROP TABLE #testEnvironment;

    END;

    --1,000,000 Random rows of data

    SELECT TOP 1000000 IDENTITY(INT,1,1) AS ID,

    ABS(CHECKSUM(NEWID())) AS randomBigInt

    INTO #testEnvironment

    FROM master.dbo.syscolumns sc1, master.dbo.syscolumns sc2, master.dbo.syscolumns sc3;

    --Holder variable to take display time out of the equation

    DECLARE @HOLDER VARCHAR(19);

    PRINT REPLICATE('=',80);

    PRINT 'REPLACE / PARSENAME / CONVERT MONEY';

    PRINT REPLICATE('=',80);

    SET STATISTICS IO, TIME ON;

    SELECT @HOLDER = REPLACE(PARSENAME(CONVERT(VARCHAR(19), CONVERT(MONEY,randomBigInt), 1), 2) COLLATE Latin1_General_BIN2, ',', '.')

    FROM #testEnvironment;

    SET STATISTICS IO, TIME OFF;

    PRINT REPLICATE('=',80);

    PRINT 'Dwain.C';

    PRINT REPLICATE('=',80);

    SET STATISTICS IO, TIME ON;

    SELECT @HOLDER = CASE WHEN randomBigInt > 999999999 THEN RIGHT(randomBigInt/1000000000, 3) + '.' ELSE '' END +

    CASE WHEN randomBigInt > 999999 THEN RIGHT(randomBigInt/1000000, 3) + '.' ELSE '' END +

    CASE WHEN randomBigInt > 999 THEN RIGHT(randomBigInt/1000, 3) + '.' ELSE '' END +

    RIGHT(randomBigInt, 3)

    FROM #testEnvironment;

    SET STATISTICS IO, TIME OFF;

    PRINT REPLICATE('=',80);

    PRINT 'Adrian.Facio';

    PRINT REPLICATE('=',80);

    SET STATISTICS IO, TIME ON;

    SELECT @HOLDER = ISNULL( CONVERT(VARCHAR,NULLIF(randomBigInt/1000000000 % 1000,0))+'.','') +

    ISNULL( CONVERT(VARCHAR,NULLIF(randomBigInt/1000000 % 1000,0))+'.','') +

    ISNULL( CONVERT(VARCHAR,NULLIF(randomBigInt/1000 % 1000,0))+'.','') +

    CONVERT(VARCHAR,NULLIF(randomBigInt%1000,0))

    FROM #testEnvironment;

    SET STATISTICS IO, TIME OFF;

    DROP TABLE #testEnvironment

    Ugly as mine is, I think it edges out Cadavre's solution slightly in the performance race.

    ================================================================================

    REPLACE / PARSENAME / CONVERT MONEY

    ================================================================================

    Table '#testEnvironment____________________________________________________________________________________________________0000000000A4'. Scan count 1, logical reads 2102, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

    SQL Server Execution Times:

    CPU time = 1404 ms, elapsed time = 1411 ms.

    ================================================================================

    Dwain.C

    ================================================================================

    Table '#testEnvironment____________________________________________________________________________________________________0000000000A4'. Scan count 1, logical reads 2102, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

    SQL Server Execution Times:

    CPU time = 1279 ms, elapsed time = 1310 ms.

    ================================================================================

    Adrian.Facio

    ================================================================================

    Table '#testEnvironment____________________________________________________________________________________________________0000000000A4'. Scan count 1, logical reads 2102, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

    SQL Server Execution Times:

    CPU time = 1794 ms, elapsed time = 1818 ms.

    Still ain't real fast though. I'm thinking it should perform in under 1000ms.


    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