sqlschool.gr logo

articles

Articles of SQLschool.gr Team

Converting numbers in a string with various format

Antonios Chatzipavlis
Friday 15 January 2016

Μερικές φορές κάποια πράγματα που επαναλαμβάνονται δίνουν κάποιο σημάδι που δεν πρέπει κανείς να αγνοεί. Ίσως είναι η πρώτη φορά που μου συμβαίνει κάτι τέτοιο καθώς μέσα σε διάστημα 12 ώρων μου τέθηκε πάνω από 10 φορές (σε παραλαγγές) η παρακάτω ερώτηση.

The Question

"Έχω προβληματιστεί σχετικά με το πώς μπορώ να έχω μία ενιαία πολιτική στο format με το οποίο οι δεκαδικοί αριθμοί αποθηκεύονται στους πίνακές μου, και μιλώ ειδικότερα στο decimal separator.
Έχω πίνακες με data που προέρχονται από import text, excel, csv αρχείων  καθώς και πίνακες που γεμίζουν με calculations που γίνονται σε data αλλων πινάκων. Ανάλογα με το data type (decimal, float, money  …) έχω κόμμα ή τελεία"

The Answer

Η απάντηση στο πρώτο σκέλος είναι στο SQL Server οι αριθμοί έχουν μόνο decimal separator και αυτός είναι η τελεία. Οτιδήποτε άλλο δεν είναι αποδεκτό. Thousand separator δεν κρατάει ο SQL Server.

Στο δεύτερο μέλος εφόσον είμαι σε SQL Server 2012 και πάνω (και αν δεν είστε καιρός είναι να πάτε) έχω τις functions PARSE και TRY_PARSE.

Ειδικά την δεύτερη αν την χρησιμοποιήσω έξυπνα και συνδιαζόμενη με την COALESCE παίρνω ακριβώς αυτό που θέλω όπως ακριβώς δείχνω στο παράδειγμα που ακολουθεί.
declare @value1 as nvarchar(100) ='1.234,56';
declare @value2 as nvarchar(100) ='1,234.56'

select    @value1 as original_value,
        COALESCE (
                    TRY_PARSE(@value1 as decimal(10,2) using 'en-us')
                 ,    TRY_PARSE(@value1 as decimal(10,2) using 'el-gr')
                 ) as value_for_sql;

select    @value2 as original_value,
        COALESCE (
                    TRY_PARSE(@value2 as decimal(10,2) using 'en-us')
                 ,    TRY_PARSE(@value2 as decimal(10,2) using 'el-gr')
                 ) as value_for_sql;
original_value   value_for_sql
---------------- ---------------------------------------
1.234,56         1234.56


original_value   value_for_sql
---------------- ---------------------------------------
1,234.56         1234.56


/*antonch*/

Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios Chatzipavlis is a highly experienced Data Solutions Consultant and Trainer. He has been working in the IT industry since 1988, holding various roles such as senior developer, IT Manager, Data & AI Solutions Architect and Consultant.

Since 1995, Antonios has focused on modern technologies and software development tools, primarily by Microsoft. He has specialized in Data & AI since 2000, with expertise in Microsoft Data Platform (SQL Server, Azure SQL Databases, Azure Synapse Analytics, Microsoft Fabric, Power BI, AI) and Databricks.

Antonios is also a Microsoft Certified Trainer (MCT) for over 25 years, has been recognized as a Microsoft Most Valuable Professional (MVP) in Data Platform since 2010 and he is in the Data Expert 40 Powerlist 2024 by Boussias. He is the co-founder and visionary behind XLYTiCA, a company dedicated to Data & AI solutions.

Episode

Task Flows in Microsoft Fabric

image

More Episodes...

Tip

Get Certified: Become a Fabric Data Engineer

More Tips...

Become a member

If you want to receive updates from us become a member to our community.

Connect

Explore

Learn

sqlschool.gr © 2010-2025 All rights reserved

This site uses cookies for operational and analytics purposes only. By continuing to browse this site, you agree to their use.