Solve the Problem of auto-padding space for CHAR type in Oracle

Source: Internet
Author: User
Tags rtrim

In Oracle, the CHAR field value is automatically filled with spaces. Therefore, when it is used as a condition, you may not be able to find the desired data.
If you cannot change the database design, use RTRIM. Example:

DB:
CREATE TABLETEST
(
TEST_IDVARCHAR2(64)NOT NULL,
TEST_CHARCHAR(10),
CONSTRAINTTEST_IDPRIMARY KEY(TEST_ID)
);
INSERT INTOTESTVALUES('1','A');
Test code:
Session session=Factory. getCurrentSession ();
Session. beginTransaction ();
List list=Session. createQuery ("FROM Test where rtrim (TEST_CHAR) = 'A'"). List ();
Session. close ();
AssertTrue (list. size ()> 0);
Output result (ORACLE ):
Hibernate:
Select
Test0 _. TEST_IDAsTEST1_0 _,
Test0 _. TEST_CHARAsTEST4_0 _
From
TEST test0 _
Where
Rtrim(TEST_CHAR)='A'
14:09:53, 171 DEBUG StringType:172 -Returning'1' As Column: TEST1_0 _
14:09:53, 187 DEBUG StringType:172 -Returning'A' As Column: TEST4_0 _
MySQL does not automatically fill in spaces, and the same code can also be used:
Output result(MySQL ):
Hibernate:
Select
Test0 _. TEST_IDAsTEST1_0 _,
Test0 _. TEST_CHARAsTEST4_0 _
From
TEST test0 _
Where
Rtrim(TEST_CHAR)='A'
14:09:01, 828 DEBUG StringType:172 -Returning'1' As Column: TEST1_0 _
14:09:01, 828 DEBUG StringType:172 -Returning'A' As Column: TEST4_0 _

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.