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

Format number thousands separator with point Expand / Collapse
Author
Message
Posted Tuesday, August 14, 2012 12:56 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, February 20, 2014 9:07 AM
Points: 52, Visits: 131
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.
Post #1344538
Posted Tuesday, August 14, 2012 1:33 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 5:46 AM
Points: 2,443, Visits: 7,557
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.



Not a DBA, just 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


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1344543
Posted Tuesday, August 14, 2012 11:33 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, February 20, 2014 9:07 AM
Points: 52, Visits: 131
excellent, thank you
Post #1344912
Posted Tuesday, August 14, 2012 12:05 PM


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 @ 10:00 AM
Points: 3,647, Visits: 7,966
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1344937
Posted Tuesday, August 14, 2012 7:28 PM


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: Yesterday @ 6:49 PM
Points: 3,648, Visits: 5,326
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!
Post #1345105
Posted Wednesday, August 15, 2012 5:00 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 5:46 AM
Points: 2,443, Visits: 7,557
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)



Not a DBA, just 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


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1345209
Posted Wednesday, August 15, 2012 5:33 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: Yesterday @ 6:49 PM
Points: 3,648, Visits: 5,326
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!
Post #1345215
Posted Thursday, August 16, 2012 5:55 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, August 4, 2014 1:50 PM
Points: 339, 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))



Post #1346298
Posted Thursday, August 16, 2012 6:19 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:18 AM
Points: 6,258, Visits: 7,445
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
Post #1346303
Posted Thursday, August 16, 2012 6:24 PM


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: Yesterday @ 6:49 PM
Points: 3,648, Visits: 5,326
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!
Post #1346304
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse