Trailing whitespaces are removed from varchar

48 views
Skip to first unread message

Rikard Swahn

unread,
Oct 31, 2019, 8:56:39 AM10/31/19
to H2 Database
Hello, 

Trailing spaces seem to be removed by H2 on varchar columns. 

Example, run this in dbconsole:

CREATE TABLE TEST(ID INT PRIMARY KEY,
   NAME VARCHAR(255));
INSERT INTO TEST VALUES(1, 'Hello ');
SELECT * FROM TEST;

name is then returned as 'Hello'

Tested on H2 1.4.200 (2019-10-14)

Is there a setting to disable this?

Evgenij Ryazanov

unread,
Oct 31, 2019, 9:03:17 AM10/31/19
to H2 Database
Hello.

They aren't removed by H2. Web browser ignores them when it displays a table.

Perhaps H2 Console should use some style such as white-space: pre to make them visible.

Rikard Swahn

unread,
Oct 31, 2019, 9:16:06 AM10/31/19
to H2 Database
No, it seems like they are removed. If I edit the value, the input box also is missing the space.

If I insert with two trailing spaces, it is persisted: INSERT INTO TEST VALUES(2, 'Hello2  ');

This is a real issue I have in a groovy application, where I use jdbc.

Evgenij Ryazanov

unread,
Oct 31, 2019, 9:32:16 AM10/31/19
to H2 Database
This is the same issue. H2 Console reads the formatted value with JavaScript where spaces were already removed by the browser; you can add white-space: pre style to cells (if your browser has development tools) as a workaround.
The database is not affected.

try (Connection c = DriverManager.getConnection("jdbc:h2:mem:1")) {
   
Statement s = c.createStatement();
    s
.execute("CREATE TABLE TEST(NAME VARCHAR(255)); INSERT INTO TEST VALUES 'Hello '");
   
try (ResultSet rs = s.executeQuery("TABLE TEST")) {
        rs
.next();
       
System.out.println('"' + rs.getString(1) + '"');
   
}
}

"Hello "


Evgenij Ryazanov

unread,
Oct 31, 2019, 9:13:29 PM10/31/19
to H2 Database

Rikard Swahn

unread,
Nov 1, 2019, 4:55:26 AM11/1/19
to H2 Database
Thanks.

It was actually a separate issue in Grails, where trailing spaces are automaically trimmed by default.
Reply all
Reply to author
Forward
0 new messages