Version info: I'm using ActiveState Perl, version 5.008008. The operating system is Windows XP Pro, SP2. The database is MS SQL Server 2000, SP3. The DBI version is 1.50. The DBD-ODBC version is 1.13. The code below runs stand-alone, e.g., not in a Web server.
(By the way, changing the column type from varchar to ncharvar doesn't affect the results.)
Here's the code. Be warned that the code contains some UTF-8 characters that are not Latin-1: If you want to test the program, just change the DBI->connect line.
use strict; use DBI; use charnames ':full'; use vars qw($trace); print "Perl version is $]\n"; # Output is "Perl version is 5.008008" my $text1 = 'Priorit' . "\N{LATIN SMALL LETTER A WITH DIAERESIS}" . 't'; my $text2 = 'Priorität'; if ($text1 eq $text2) {print 'Equal '}; if ($text1 ne $text2) {print 'Different '}; print length($text1) . ' ' . length($text2) . "\n"; # Output is "Equal 9 9" # Connect to the database and create a database handler my $dbh = DBI->connect('dbi:ODBC:dataSource', 'userID', 'password', { RaiseError => 0, AutoCommit => 0 } ) || die $DBI::errstr; do_sql(<<'--------'); CREATE TABLE dummy1 ( id1 int NOT NULL, str1 varchar(50) NOT NULL ) ON [PRIMARY] -------- do_sql("INSERT INTO dummy1 (id1, str1) VALUES(1, '$text1')"); do_sql("INSERT INTO dummy1 (id1, str1) VALUES(2, '$text2')"); # According to the SQL Query Analyzer program, the table contains: # 1, 'Priorität' (a 10 character value) # 2, 'Priorität' (a 9 character value) my $array_ref = $dbh->selectall_arrayref('SELECT * FROM dummy1'); for my $r (@$array_ref) { print ${$r}[0] . ': ' . length(${$r}[1]) . "\n"; } # Output is # 1: 10 # 2: 9 $dbh->commit; $dbh->disconnect || warn $dbh->errstr; sub do_sql { $dbh->do($_[0]); if ($dbh->errstr()) { print STDERR 'error number = ' . $dbh->err() . "\n"; print STDERR "The following SQL statement failed:\n$_[0]\n"; $dbh->disconnect || warn $dbh->errstr; die; } }
In reply to database stores UTF8 strings inconsistently by robv
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |