sqlschool.gr logo

articles

Articles of SQLschool.gr Team

How to get month name in different languge (culture) within a single query

Antonios Chatzipavlis
Wednesday 06 November 2013

Αυτό που λατρεύω πραγματικά και μπορώ να πω είναι και ο λόγος που μου αρέσει να κάνω μάθημα είναι οι ερωτήσεις που δέχομαι από τους συμμετέχοντες. Χθες κατά την διάρκεια του σεμιναρίου που κάνω (10774 - Querying Microsoft SQL Server 2012) μία συμμετέχοντας έθεσε μια ερώτηση η οποία ήταν η εξής:

Η ερώτηση

"Θέλω σε ένα query το οποίο περιέχει ένα πεδίο ημερομηνίας, από αυτό το πεδίο να δείχνω το λεκτικό του μήνα σε όποια γλώσσα επιλέξω. Πως αυτό μπορεί να γίνει;"

Η απάντηση

Η απάντηση μου ήταν άμεση καθώς από τον SQL Server 2012 και μετά έχω νέα καλούδια στην T-SQL σε συνδυασμό με το προγραμματιστικό μου παρελθόν (αν και αυτό είναι σχετικό καθώς όπως λέει η παροιμία πρώτα βγαίνει η ψυχή και μετά το χούι).

Στον SQL Server 2012 έχει προστεθεί μια CLR function γνωστή σε όλους γράφουν σε .NET και δεν είναι άλλη από την FORMAT function.

Η συγκεκριμένη function πέρα από τα standard formats μπορεί να δεχθεί και custom formats (ακριβώς όπως και στο .NET). Ακόμα μπορεί να πάρει και την γλώσσα (culture). Μπορείτε να δείτε περισσότερα για αυτή την function στα BOL στο link http://technet.microsoft.com/en-us/library/hh213505.aspx.

Έτσι με το παρακάτω query

SELECT orderid, orderdate
, FORMAT( orderdate, 'MMMM', 'en-US' ) AS englishmonth
, FORMAT( orderdate, 'MMMM', 'el-GR' ) AS greekmonth
, FORMAT( orderdate, 'MMMM', 'it-IT' ) AS italicsmonth
, FORMAT( orderdate, 'MMMM', 'es-ES' ) AS spainmonth
, FORMAT( orderdate, 'MMMM', 'zh-cn' ) AS chineesemonth<
FROM Sales.Orders; GO
Έχω το επιθυμητό αποτέλεσμα

image

 

Enjoy it and keep Rocking with SQL Server Knowledge

/*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.