SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Format number thousands separator with point


Format number thousands separator with point

Author
Message
cms9651
cms9651
SSC-Addicted
SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)

Group: General Forum Members
Points: 476 Visits: 144
Format number thousands separator with point
Hi there, I need your help.

I have this number in my database output result of query:
1013473

I need this output: 1.013.473

Can you help me?
Thanks in advance.
Cadavre
Cadavre
SSCrazy Eights
SSCrazy Eights (9.3K reputation)SSCrazy Eights (9.3K reputation)SSCrazy Eights (9.3K reputation)SSCrazy Eights (9.3K reputation)SSCrazy Eights (9.3K reputation)SSCrazy Eights (9.3K reputation)SSCrazy Eights (9.3K reputation)SSCrazy Eights (9.3K reputation)

Group: General Forum Members
Points: 9282 Visits: 8492
cms9651 (8/14/2012)
Format number thousands separator with point
Hi there, I need your help.

I have this number in my database output result of query:
1013473

I need this output: 1.013.473

Can you help me?
Thanks in advance.


You should probably be doing this in the presentation layer, rather than the database. But, here's one way you could do it: -

SELECT PARSENAME(CONVERT(VARCHAR(19), CONVERT(MONEY,randomBigInt), 1), 2)
FROM #testEnvironment;



How about performance?
BEGIN TRAN

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 'PARSENAME / CONVERT MONEY';
PRINT REPLICATE('=',80);
SET STATISTICS IO, TIME ON;
SELECT @HOLDER = PARSENAME(CONVERT(VARCHAR(19), CONVERT(MONEY,randomBigInt), 1), 2)
FROM #testEnvironment;
SET STATISTICS IO, TIME OFF;

ROLLBACK



================================================================================
PARSENAME / CONVERT MONEY
================================================================================
Table '#testEnvironment'. 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 = 609 ms, elapsed time = 610 ms.



--EDIT--

Sorry, I've just noticed that you asked for dot separators not commas.

Instead, use this: -
SELECT REPLACE(PARSENAME(CONVERT(VARCHAR(19), CONVERT(MONEY,randomBigInt), 1), 2) COLLATE Latin1_General_BIN2, ',', '.')
FROM #testEnvironment;



Performance goes down over the commas because of the additional replace: -
BEGIN TRAN

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;

ROLLBACK



================================================================================
REPLACE / PARSENAME / CONVERT MONEY
================================================================================
Table '#testEnvironment'. 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 = 985 ms, elapsed time = 987 ms.



Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
cms9651
cms9651
SSC-Addicted
SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)

Group: General Forum Members
Points: 476 Visits: 144
excellent, thank you
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42844 Visits: 19847
Sorry, I've just noticed that you asked for dot separators not commas

Wouldn't this be defined by the regional settings? If the settings affect the format for money, this can result problematic.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18259 Visits: 6431
Cadavre +1 for a very clever solution.


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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Cadavre
Cadavre
SSCrazy Eights
SSCrazy Eights (9.3K reputation)SSCrazy Eights (9.3K reputation)SSCrazy Eights (9.3K reputation)SSCrazy Eights (9.3K reputation)SSCrazy Eights (9.3K reputation)SSCrazy Eights (9.3K reputation)SSCrazy Eights (9.3K reputation)SSCrazy Eights (9.3K reputation)

Group: General Forum Members
Points: 9282 Visits: 8492
Luis Cazares (8/14/2012)
Wouldn't this be defined by the regional settings? If the settings affect the format for money, this can result problematic.


I don't believe so. Normally I'd just test it, but I'm having a bit of a busy day.

dwain.c (8/14/2012)
Cadavre +1 for a very clever solution.


Thank you. I was hoping someone would have a method that would blow it away (the performance isn't great) :-)


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18259 Visits: 6431
Cadavre (8/15/2012)

dwain.c (8/14/2012)
Cadavre +1 for a very clever solution.


Thank you. I was hoping someone would have a method that would blow it away (the performance isn't great) :-)


Don't think for one minute I didn't try! :-)


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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
adrian.facio
adrian.facio
Right there with Babe
Right there with Babe (740 reputation)Right there with Babe (740 reputation)Right there with Babe (740 reputation)Right there with Babe (740 reputation)Right there with Babe (740 reputation)Right there with Babe (740 reputation)Right there with Babe (740 reputation)Right there with Babe (740 reputation)

Group: General Forum Members
Points: 740 Visits: 386
Hi,

This is not as nice as cadavre, but is different.



DECLARE @Number AS BIGINT; SET @Number = 65432121235

SELECT ISNULL( CONVERT(VARCHAR,NULLIF(@Number/1000000000 % 1000,0))+'.','') +
ISNULL( CONVERT(VARCHAR,NULLIF(@Number/1000000 % 1000,0))+'.','') +
ISNULL( CONVERT(VARCHAR,NULLIF(@Number/1000 % 1000,0))+'.','') +
CONVERT(VARCHAR,NULLIF(@Number%1000,0))




Evil Kraig F
Evil Kraig F
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21233 Visits: 7660
Hm, neat. I see what you did there. The Parsename had me all sorts of scragged up until I broke it down and figured out what you were avoiding with it. Neat trick. Not much else you can do with that for speed, though, you're dealing with significant string manipulation at that point, not numerics.

For regional settings, afaik and what I can find in BoL, region will affect your default collations and default datetime components, but not directly selected money styles.


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18259 Visits: 6431
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!

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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search