Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent trailing spaces during insert?

Tags:

postgresql

I have this INSERT statement and there seems to be trailing spaces at the end of the acct_desc fields. I'd like to know how to prevent trailing spaces from occurring during my insert statement.

INSERT INTO dwh.attribution_summary
SELECT d.adic,
       d.ucic,
       b.acct_type_desc as acct_desc,
       a.begin_mo_balance as opening_balance,
       c.date,
      'fic' as userid
FROM  fic.dim_members d 
JOIN  fic.fact_deposits a ON d.ucic = a.ucic 
JOIN  fic.dim_date c ON a.date_id = c.date_id 
JOIN  fic.dim_acct_type b ON a.acct_type_id = b.acct_type_id
WHERE c.date::timestamp = current_date - INTERVAL '1 days';
like image 980
precose Avatar asked Jan 18 '23 00:01

precose


1 Answers

Use the PostgreSQL trim() function. There is trim(), rtrim() and ltrim().
To trim trailing spaces:

...
rtrim(b.acct_type_desc) as acct_desc,
...

If acct_type_desc is not of type text or varchar, cast it to text first:

...
rtrim(b.acct_type_desc::text) as acct_desc,
...

If acct_type_desc is of type char(n), casting it to text removes trailing spaces automatically, no trim() necessary.

like image 124
Erwin Brandstetter Avatar answered May 10 '23 12:05

Erwin Brandstetter