Multiple String REPLACEs with T-SQL CTEs

December 11, 2009

I had to solve the problem to update data in a table of NVARCHAR strings where several substrings had to be replaced with their respective counterparts, or translations.

Of course the easy way is to apply as many REPLACE functions as you need, but why not try a more generic way?

The string table in its simplest form looks like this:

CREATE TABLE #Data (
	data NVARCHAR(50)
)

INSERT INTO #Data (data) VALUES ('banana')
INSERT INTO #Data (data) VALUES ('apples')

and these are the translations:

CREATE TABLE #T (
    old	NVARCHAR(50),
    new NVARCHAR(50)
)

INSERT INTO #T VALUES('p', 'Q')
INSERT INTO #T VALUES('s', 'Z')
INSERT INTO #T VALUES('a', 'A')

I came up with the following CTE statement:

The CTE stores the original strings, their intermediate translations, and their final translations. A level counter is used to find the entry with the highest count of translations for each string, indicating the final result.

The CTE starts with the original data in all three columns, and level set to zero.

From the original data, all REPLACE results of all translatable texts are calculated. Replacing strings are compared using a case-sensitive collation:

WITH CTE (org, calc, DATA, level) AS
(
    SELECT data, data, data, 0
    FROM	#Data

    UNION ALL

    SELECT CTE.org, CTE.data,
        CONVERT(NVARCHAR(50), REPLACE(CTE.data, #T.old, #T.new)), CTE.level + 1
    FROM	CTE
    INNER JOIN #T ON CTE.data LIKE '%' + #T.old + '%'
        COLLATE Latin1_General_CS_AS
)
SELECT DISTINCT org, data, level
FROM CTE
WHERE level =
    (SELECT MAX(level) FROM CTE c WHERE CTE.org = c.org)

Finally, clean up the temp tables:

DROP TABLE #Data
DROP TABLE #T

This code returns the expected result:

org data level
apples AQQleZ 3
banana bAnAnA 1

100.000 Views

December 10, 2009

According to WordPress Blog Stats.

;)


Listing ON DELETE CASCADE Constraints in MS SQL Server

December 4, 2009

To check which tables have foreign key constraints with referential actions on other tables, use the sys.foreign_keys system view like this:

DECLARE @t NVARCHAR(128)
SET @t = 'My_Table_Name'

SELECT p.name, fk.delete_referential_action_desc, t.name
FROM sys.foreign_keys fk
INNER JOIN sys.objects t ON fk.parent_object_id = t.object_id
INNER JOIN sys.objects p ON fk.referenced_object_id = p.object_id
WHERE p.name = @t
ORDER BY 1, 3

If you want not only master-detail, but also grandparent-parent-child relations, you need a join over 3 tables and 2 foreign keys:

SELECT DISTINCT p.name, fk.delete_referential_action_desc, t.name, 
   fk2.delete_referential_action_desc, tt.name
FROM sys.foreign_keys fk
INNER JOIN sys.objects t ON fk.parent_object_id = t.object_id
INNER JOIN sys.objects p ON fk.referenced_object_id = p.object_id
INNER JOIN sys.foreign_keys fk2 ON fk2.referenced_object_id = t.object_id
INNER JOIN sys.objects tt ON fk2.parent_object_id = tt.object_id
WHERE p.name = @t
AND t.object_id <> p.object_id
ORDER BY 1, 3, 5

The filter on t.object_id <> p.object_id exludes circular relations in the first level which show up in the second level anyway.


Free Unicode Fonts

December 1, 2009

This seems to be a re-occurring topic on Stack Overflow: where can you get free fonts for Unicode characters?

There may be some misunderstandings on Unicode and fonts and Unicode fonts which cause the question, and makes the answer more difficult than you would expect:

From the Unicode website:

The Unicode Standard is a character coding system designed to support the worldwide interchange, processing, and display of the written texts of the diverse languages and technical disciplines of the modern world.

The standard defines 107,361 characters or code points as of version 5.2. Unicode also defines a set of properties for each character, and algorithms such as line breaking.

Fonts that “support Unicode” promise to the operating system or to the application that uses them that they provide an accurate graphical representation (glyph) of the code points they implement. A “Unicode font” typically does not cover each and every Unicode code point, but rather only subsets of the standard (Unicode Blocks).

Typically it is the operating system’s responsibility to find a matching font when displaying Unicode text.

So how can you find a free Unicode font supporting most of the characters? Here’s a list of links:

Wikipedia

Alphabetical scripts

CJK fonts

  • Hanazono (52,809 CJK characters, full Ext A, Ext C, partial Ext B)

Complete

Font Lists


Follow

Get every new post delivered to your Inbox.

Join 65 other followers