Uniqueidentifier string format

Discussion of open issues, suggestions and bugs regarding usage of dbExpress drivers for SQL Server in Delphi and C++Builder
Post Reply
rosa
Posts: 2
Joined: Wed 25 May 2011 07:29

Uniqueidentifier string format

Post by rosa » Wed 25 May 2011 07:42

Hello,

I noticed that dbExpress creates TStringField for the UniqueIdentifier fields. Can I change the format of the resulting string somehow? In particular I'd like to remove curly brackets.

AndreyZ

Post by AndreyZ » Wed 25 May 2011 11:36

Hello,

To remove curly brackets from the uniqueidentifier column, you can use persistent fields and the OnGetText event handler in the following way:

Code: Select all

procedure TMainForm.ClientDataSetuidGetText(Sender: TField; var Text: String;
  DisplayText: Boolean);
begin
  Text := Copy(Sender.AsString, 2, Length(Sender.AsString) - 2);
end;

rosa
Posts: 2
Joined: Wed 25 May 2011 07:29

Post by rosa » Thu 26 May 2011 08:46

AndreyZ wrote:Hello,

To remove curly brackets from the uniqueidentifier column, you can use persistent fields and the OnGetText event handler in the following way:

Code: Select all

procedure TMainForm.ClientDataSetuidGetText(Sender: TField; var Text: String;
  DisplayText: Boolean);
begin
  Text := Copy(Sender.AsString, 2, Length(Sender.AsString) - 2);
end;
This is of course possible, but I can't use such approach(persistent fields) in my case. The application would have to handle every uniqueidentifier field this way explicitly, but it simply doesn't know them:-(( Moreover, the described solution might work with dbGrid, but not when applying filters on the dataset - I guess.
I'd rather have dbExpress driver (or the underlaying sql server client library) configured to behave the required way for every uniqueidentifier field in the database implicitly - if possible.

AndreyZ

Post by AndreyZ » Thu 26 May 2011 12:19

You can cast uniqueidentifier fields to string in the following way:

Code: Select all

SQLQuery.SQL.Text := 'select cast(uid as varchar(40)) uid from tablename';
ClientDataSet.Open;

Post Reply