HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Enforcing distinct number of trailing spaces

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
distinctenforcingnumberspacestrailing

Problem

I'm working on converting a SQL Anywhere (ASA) 5.5 based application and its database to Microsoft SQL Server.

Existing is a table with a primary key clustered on two columns, code_type char(4) and code char(30). I was able to build the table fine, but attempting to move over the data I ran into a conflict because the table contained ('cha', ' ') and ('cha', '') and speaking with another developer it sounds like this has a use case. Not really looking to retrain the users, what options are there to enforce a distinct number of spaces at the database level?

The database is for a single application so in the end I can rely on control of how data is inserted into the table but would like to enforce an intended distinction in the table definition.

If ('cha', '') exists in the table, I'd like to be able to insert ('cha', ' ') as it has more trailing spaces, but not ('cha', '') because it has an equal number of spaces (0) as an existing entry.

This question on UNIQUE Index outlines the behavior on unique index, SQL is applying right padding of spaces to make two strings equally sized before making the comparison per ANSI/ISO SQL-92 Section Section 8.2 General Rule #3. I've confirmed this behavior with the char type as the link used varchar. I'm not committed to using a key, just some way to enforce a distinct number of trailing spaces.

There are quite a few characters guaranteed not to show up in the second column. The field is either letter abbreviations or legal document citations. The legal document citations seem to be contain alphanumeric characters plus '(', ')', '-', '.', ' ', and '/'.

Solution

I wouldn't call this a good solution but it seems to work. You can create a computed column with an appended character and create a unique index on code_type and the computed column. For the appended character you can use a character which will never appear in your data or any string of at least 31 character length if you'd like to avoid making assumptions like that. It's probably easiest to go through an example.

This is the problem that you're having:

CREATE TABLE dbo.T226714 (
    code_type CHAR(4) NOT NULL,
    code VARCHAR(30) NOT NULL,
    PRIMARY KEY (code_type, code)
);

-- fails due to PK error
INSERT INTO dbo.T226714 VALUES ('cha', ' ');
INSERT INTO dbo.T226714 VALUES ('cha', '');


Instead, do something like this:

DROP TABLE dbo.T226714;

CREATE TABLE dbo.T226714 (
    code_type CHAR(4) NOT NULL,
    code VARCHAR(30) NOT NULL,
    code_with_end_char AS CAST(code + 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ' AS VARCHAR(61))
);

CREATE UNIQUE INDEX UI ON dbo.T226714 (code_type, code_with_end_char);

-- works
INSERT INTO dbo.T226714 VALUES ('cha', ' ');
INSERT INTO dbo.T226714 VALUES ('cha', '');

-- can't insert this as a third row due to the unique index:
INSERT INTO dbo.T226714 VALUES ('cha', ' ');

Code Snippets

CREATE TABLE dbo.T226714 (
    code_type CHAR(4) NOT NULL,
    code VARCHAR(30) NOT NULL,
    PRIMARY KEY (code_type, code)
);

-- fails due to PK error
INSERT INTO dbo.T226714 VALUES ('cha', ' ');
INSERT INTO dbo.T226714 VALUES ('cha', '');
DROP TABLE dbo.T226714;

CREATE TABLE dbo.T226714 (
    code_type CHAR(4) NOT NULL,
    code VARCHAR(30) NOT NULL,
    code_with_end_char AS CAST(code + 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ' AS VARCHAR(61))
);

CREATE UNIQUE INDEX UI ON dbo.T226714 (code_type, code_with_end_char);

-- works
INSERT INTO dbo.T226714 VALUES ('cha', ' ');
INSERT INTO dbo.T226714 VALUES ('cha', '');

-- can't insert this as a third row due to the unique index:
INSERT INTO dbo.T226714 VALUES ('cha', ' ');

Context

StackExchange Database Administrators Q#226714, answer score: 4

Revisions (0)

No revisions yet.