Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8953819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:11:08+00:00 2026-06-15T14:11:08+00:00

I’m trying to use a German-style CSV file with DBI and DBD::CSV . This,

  • 0

I’m trying to use a German-style CSV file with DBI and DBD::CSV. This, in turn, uses Text::CSV to parse the file. I want to query the data in that file using SQL.

Let’s look at the file first. It is separated by semicolons (;), and the numbers in it look like this: 5,23, which is equivalent to the English 5.23.

Here’s what I’ve got so far:

use strict; use warnings;
use DBI;

# create the database handle
my $dbh = DBI->connect(
  'dbi:CSV:',
  undef, undef,
  {
    f_dir => '.',
    f_schema => undef,
    f_ext => '.csv',
    f_encoding => 'latin-1',
    csv_eol => "\n",
    csv_sep_char => ';',
    csv_tables => {
      foo => {
        file => 'foo.csv',
        #skip_first_row => 0,
        col_names => [ map { "col$_" } (1..3)  ], # see annotation below
      },
    },
  },
) or croak $DBI::errstr;

my $sth = $dbh->prepare(
  'SELECT col3 FROM foo WHERE col3 > 80.50 ORDER BY col3 ASC'
);
$sth->execute;

while (my $res = $sth->fetchrow_hashref) {
  say $res->{col3};
}

Now, this looks quite nice. The problem is that the SQL (meaning SQL::Statement, which is somewhere down the line from DBI and DBD::CSV) does not regard the data in col3, which is a floating-point value with a comma in the middle, as a float. Instead, it treats the column as an integer, because it doesn’t understand the comma.

Here’s some example data:

foo;foo;81,90
bar;bar;80,50
baz;baz;80,70

So the above code with this data will result in one line of output: 81,90. Of course, that is wrong. It used the int() part of col3 with the comparison, which is right, but not what I want.

Question: How can I tell it to treat the numbers with the comma as float?

Things I’ve thought about:

  • I’ve not found any built-in way in Text::CSV to do this. I’m not sure where in Text::CSV I could hook this in, or if there is a mechanism in Text::CSV to put such things in at all.
  • I don’t know if it poses a problem that DBD::CSV wants to use Text::CSV_XS if possible.
  • Maybe I can do it later, after the data has been read and is already stored away somewhere, but I’m not yet sure where the right access point is.
  • I understand that the stuff is stored in SQL::Statement. I don’t yet know where. This could be handy somehow.

Changing the source CSV file to have dots instead of commas is not an option.

I’m open for all kinds of suggestions. Other approaches to the whole CSV via SQL thing are welcome, too. Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:11:08+00:00Added an answer on June 15, 2026 at 2:11 pm

    You need to write a user-defined function using SQL::Statement::Functions (already loaded as part of DBD::CSV).

    This program does what you want. Adding 0.0 to the transformed string is strictly unnecessary, but it makes the point about the purpose of the subroutine. (Note also your typo in the f_encoding parameter to the connect call.)

    use strict;
    use warnings;
    
    use DBI;
    
    my $dbh = DBI->connect(
      'dbi:CSV:',
      undef, undef,
      {
        f_dir => '.',
        f_schema => undef,
        f_ext => '.csv',
        f_encoding => 'latin-1',
        csv_eol => "\n",
        csv_sep_char => ';',
        csv_tables => {
          foo => {
            file => 'test.csv',
            #skip_first_row => 0,
            col_names => [ map { "col$_" } (1..3)  ], # see annotation below
          },
        },
      },
    ) or croak $DBI::errstr;
    
    $dbh->do('CREATE FUNCTION comma_float EXTERNAL');
    
    sub comma_float {
      my ($self, $sth, $n) = @_;
      $n =~ tr/,/./;
      return $n + 0.0;
    }
    
    my $sth = $dbh->prepare(
      'SELECT col3 FROM foo WHERE comma_float(col3) > 80.50 ORDER BY col3 ASC'
    );
    $sth->execute;
    
    while (my $res = $sth->fetchrow_hashref) {
      say $res->{col3};
    }
    

    output

    80,70
    81,90
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
i want to parse a xhtml file and display in UITableView. what is the
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.