Convert Text Value in SQL Server from UTF8 to ISO 8859-1
In SQL Server, text data can be stored in various encodings, including UTF8 and ISO 8859-1. This article addresses the need to convert text from UTF8 encoding to ISO 8859-1, a common challenge when dealing with character sets.
To successfully convert the text, follow these steps:
To illustrate the conversion process, consider the following scenario:
A SQL Server table has a column named "text_data" that contains UTF8-encoded text. The task is to convert this text to ISO 8859-1 encoding.
The following query can be used to achieve this:
-- Create a temporary table with UTF8-encoded text CREATE TABLE #temp_table (text_data varchar(max) COLLATE Latin1_General_BIN); -- Insert UTF8-encoded text into the temporary table INSERT INTO #temp_table (text_data) VALUES ('Olá. Gostei do jogo. Quando "baixei" até achei que não iria curtir muito'); -- Convert the text to ISO 8859-1 encoding UPDATE #temp_table SET text_data = CONVERT(varchar(max), text_data, 1252); -- Select and display the converted text SELECT text_data FROM #temp_table; -- Drop the temporary table DROP TABLE #temp_table;
By executing this query, the text in the "text_data" column will be converted from UTF8 to ISO 8859-1 encoding, and the converted text will be displayed.
Remember, when working with character sets and encodings, it is crucial to consider the compatibility of the encoding with other systems or applications where the data may be used or exchanged.
The above is the detailed content of How to Convert UTF8 Text to ISO 8859-1 in SQL Server?. For more information, please follow other related articles on the PHP Chinese website!