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 is a Data Solutions Consultant and Trainer. He has been working in IT since 1988. In his career, he has worked as senior developer, IT Manager, Solutions Architect and IT Consultant. Since 1995 he has been devoted on new technologies and software development tools, mainly by Microsoft, either by training company staff and colleagues or assisting them in design, development and implementation as a consultant or chief developer. He has focused in Databases and Data Science since 1995. He specialized in Microsoft SQL Server since version 6.0 in areas like SQL Server Internals, Database Design and Development, Business Intelligence and in 2010 he has started working with Azure Data Platform, NoSQL databases, Big Data Technologies and Machine Learning. He is an active member of many IT communities in Greece, answering colleagues' questions and writing articles in his web site. He is the owner of SQLschool.gr which is a community portal with a lot of information about Microsoft SQL Server. He has been a Microsoft Certified Trainer (MCT) since 2000. Microsoft honored him as MVP on Data Platform due to his activities in SQL Server since 2010. He holds a large number of Microsoft Certifications and Microsoft SQL Server Certifications since version 6.5.

Episode

Task Flows in Microsoft Fabric

image

More Episodes...

Tip

What's New in SQL Server 2022 - Episodes

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.