sqlschool.gr logo

articles

Articles of SQLschool.gr Team

The QUOTENAME and CONCAT_WS string functions

Antonios Chatzipavlis
Sunday 14 June 2020

Overview

Αν και είναι ένα μικρό άρθρο αυτό, εντούτοις θεώρησα ότι πρέπει να το γράψω καθώς βλέπω ότι κάποιοι ταλαιπωρούνται αδίκως όταν γράφουν κώδικα ειδικά όταν αυτός πρέπει να περιέχει object names πχ tables, fields, views, stored procedures.

Sample

Ας πάρουμε ένα απλό παράδειγμα. Έστω ότι θέλουμε να δημιουργήσουμε ένα script που να δημιουργεί select statements για όλα τα table και views που έχω σε μία databases.

Η απλούστερη λύση είναι να ρωτήσουμε το INFORMATION_SCHEMA.TABLES view

SQL Script

SELECT *
FROM INFORMATION_SCHEMA.TABLES;

Οι περισσότεροι για να ικανοποιήσουν την ανάγκη αυτή θα γράψουν το παρακάτω:

SQL Script

SELECT 'SELECT * FROM ' + '[' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']' AS selectstm
FROM INFORMATION_SCHEMA.TABLES;
Result
selectstm
SELECT * FROM [Sales].[OrdersXXL]
SELECT * FROM [Sales].[CustomersEnlarge]
SELECT * FROM [dbo].[tbl_T]
SELECT * FROM [Sales].[OrdersEnlarge]
SELECT * FROM [HR].[Employees]
SELECT * FROM [Production].[Suppliers]
SELECT * FROM [Production].[Categories]
SELECT * FROM [Production].[Products]
SELECT * FROM [Sales].[Customers]
SELECT * FROM [Sales].[Shippers]

QUOTENAME

Αντί όμως για αυτό θα μπορούσαν να κάνουν χρήση της QUATENAME string function όπως παρακάτω:

SQL Script

SELECT 'SELECT * FROM ' + QUOTENAME(TABLE_SCHEMA)+'.' + QUOTENAME(TABLE_NAME) AS selectstm
FROM INFORMATION_SCHEMA.TABLES;
Result
selectstm
SELECT * FROM [Sales].[OrdersXXL]
SELECT * FROM [Sales].[CustomersEnlarge]
SELECT * FROM [dbo].[tbl_T]
SELECT * FROM [Sales].[OrdersEnlarge]
SELECT * FROM [HR].[Employees]
SELECT * FROM [Production].[Suppliers]
SELECT * FROM [Production].[Categories]
SELECT * FROM [Production].[Products]
SELECT * FROM [Sales].[Customers]
SELECT * FROM [Sales].[Shippers]

CONCAT_WS

Αλλά επειδή δεν θέλω να βλέπω και να βάζω πολλά + μπορώ να χρησιμοποιήσω και την CONCAT_WS string functions και να κάνω την ζωή μου καλύτερη.

Note

H CONCAT_WS είναι διαθέσιμη από τον SQL Server 2017.

SQL Script

SELECT 'SELECT * FROM ' + CONCAT_WS('.',QUOTENAME(TABLE_SCHEMA),QUOTENAME(TABLE_NAME)) AS selectstm
FROM INFORMATION_SCHEMA.TABLES;
Result
selectstm
SELECT * FROM [Sales].[OrdersXXL]
SELECT * FROM [Sales].[CustomersEnlarge]
SELECT * FROM [dbo].[tbl_T]
SELECT * FROM [Sales].[OrdersEnlarge]
SELECT * FROM [HR].[Employees]
SELECT * FROM [Production].[Suppliers]
SELECT * FROM [Production].[Categories]
SELECT * FROM [Production].[Products]
SELECT * FROM [Sales].[Customers]
SELECT * FROM [Sales].[Shippers]

Another sample

Εκτός όμως από αυτή την χρήση υπάρχουν και άλλες όπως για παράδειγμα το παρακάτω όπου θέλω να έχω το contact name και το contact title σε μια νέα κολώνα με την μορφή contactname (contacttitle).

SQL Script

SELECT contactname,contacttitle, CONCAT_WS(' ',contactname , QUOTENAME(contacttitle,'()')) as ContactNameTitle
FROM SALES.Customers
Result
contactname contacttitle ContactNameTitle
Allen, Michael Sales Representative Allen, Michael (Sales Representative)
Hassall, Mark Owner Hassall, Mark (Owner)
Strome, David Owner Strome, David (Owner)
Cunningham, Conor Sales Representative Cunningham, Conor (Sales Representative)
Higginbotham, Tom Order Administrator Higginbotham, Tom (Order Administrator)
Poland, Carole Sales Representative Poland, Carole (Sales Representative)
Bansal, Dushyant Marketing Manager Bansal, Dushyant (Marketing Manager)
Ilyina, Julia Owner Ilyina, Julia (Owner)
Raghav, Amritansh Owner Raghav, Amritansh (Owner)
Culp, Scott Accounting Manager Culp, Scott (Accounting Manager)

Note

H QUOTENAME δέχετε ακόμα μια παράμετρο την 'quote_character'
Is a one-character string to use as the delimiter. Can be a single quotation mark ( ' ), a left or right bracket ( [] ), a double quotation mark ( " ), a left or right parenthesis ( () ), a greater than or less than sign ( >< ), a left or right brace ( {} ) or a backtick ( ` ). NULL returns if an unacceptable character is supplied. If quote_character is not specified, brackets are used.

Conclusion

Είδαμε μερικά παραδείγματα για το πως μπορούμε να χρησιμοποιήσουμε αυτές, υπάρχουν πολλά ακόμα. Αυτό που λέω πάντα είναι ότι χρησιμοποιούμε πάντα αυτό που έχει την καλύτερη απόδοση στην εκάστοτε περίπτωση.


Antonios Chatzipavlis

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.

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-2024 All rights reserved

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