Thiện Sinh
Thiện Sinh

Reputation: 559

Execute query with parameter work incorrect

I have Student table with two field(id: number, name: 10 char) The example value of name column is: 'William[2 space]', 'Ethan[5 space]' (The space will be add to meet the max length)

The queries below work fine. (hard code or string interpolation)

 select * from where name = 'William'
or select * from where name = 'William  '

But when i use parameter like below, it doesn't work

select * from where name = :Name

and then inject the parameters
var result = ctx.ExecuteStatement(query, new { Name = name })

So when name = 'William  ', it work.
But when name = 'William', it doesn't work.

=> I want it work in two case? Please help me address the issue.

So my temporary solution is trim the column before compare. But i think it just work around and not completely resolve the problem since oracle automatically ignore the whitespace(I showed in my first sample)

select * from where trim(name) = :Name

Upvotes: 0

Views: 152

Answers (2)

hotfix
hotfix

Reputation: 3396

do not use char as datatype to store strings with variable length. use varchar2 instead

Upvotes: 2

Marvin Schuchardt
Marvin Schuchardt

Reputation: 123

You could add whitespaces by

name = name.PadRight(10);

PadRight aligns the text to the left and fills the string with whitespaces to obtain the defined length.

Upvotes: 1

Related Questions