cancel
Showing results for 
Search instead for 
Did you mean: 

Remove quotes in field names with sql

0 Kudos
1,382

For some reason, some field names in a table are escaped with quotes. The field names cannot be changed. What I wonder is if I via SQL with execute immediate statement etc., can remove these quotes in the field names?

Today it looks like this:

Select * from test

Mat----'Field1'----'Filed2'----'Field3'
--------------------------------------
AB01------xd---------ab----------aa--

But I want it to be this way:

Mat-----Field1-----Filed2-----Field3-
-------------------------------------
AB01------xd---------ab---------aa---

Accepted Solutions (1)

Accepted Solutions (1)

Breck_Carter
Participant
CREATE TABLE test ( 
   Mat        VARCHAR ( 10 ),
   "'Field1'" VARCHAR ( 10 ),
   "'Filed2'" VARCHAR ( 10 ),
   "'Field3'" VARCHAR ( 10 ))
INSERT test VALUES ( 'AB01', 'xd', 'ab', 'aa' );
COMMIT;
Select * from test;
ALTER TABLE test RENAME "'Field1'" TO Field1;
ALTER TABLE test RENAME "'Filed2'" TO Filed2;
ALTER TABLE test RENAME "'Field3'" TO Field3;
Select * from test;

1 row(s) affected
Mat        'Field1'   'Filed2'   'Field3'   
---------- ---------- ---------- ---------- 
AB01       xd         ab         aa         
(1 rows)


Mat        Field1     Filed2     Field3     
---------- ---------- ---------- ---------- 
AB01       xd         ab         aa         
(1 rows)
0 Kudos

Thanks!

I wrote wrong in the description. Sorry for that. I mean that the field names cannot be changed. But when I think about it, I can do that by first creating a temporary table. Then I can do according to your principle. However, there are hundreds of fields, can I make ALTER TABLE test RENAME more automated without entering the field names?

Breck_Carter
Participant
0 Kudos

This doesn't deal with "field names can't be changed", but it does take care of "hundreds of fields".

CREATE TABLE test ( 
   Mat        VARCHAR ( 10 ),
   "'Field1'" VARCHAR ( 10 ),
   "'Filed2'" VARCHAR ( 10 ),
   "'Field3'" VARCHAR ( 10 ))
INSERT test VALUES ( 'AB01', 'xd', 'ab', 'aa' );
COMMIT;
Select * from test;
BEGIN
DECLARE @SQL LONG VARCHAR;
FOR f_fetch AS c_fetch NO SCROLL CURSOR FOR
SELECT SYSTABCOL.column_name AS @column_name
  FROM SYSTAB 
          INNER JOIN SYSTABCOL
                  ON SYSTABCOL.table_id = SYSTAB.table_id
 WHERE SYSTAB.table_name = 'test' 
  AND USER_NAME ( SYSTAB.creator ) = 'DBA'
FOR READ ONLY
DO
   IF LEFT ( @column_name, 1 ) = '''' THEN
      SET @SQL = STRING (
         'ALTER TABLE test RENAME "', 
         @column_name, 
         '" TO ', 
         SUBSTR ( @column_name, 2, LENGTH ( @column_name ) - 2 ) );
      EXECUTE IMMEDIATE @sql;
   END IF;
END FOR;
END; 
Select * from test;

1 row(s) affected Mat 'Field1' 'Filed2' 'Field3'


AB01 xd ab aa
(1 rows)

Mat Field1 Filed2 Field3


AB01 xd ab aa
(1 rows)

VolkerBarth
Contributor

Certainly a better way than to live with weird column names in base tables...

Vlad
Product and Topic Expert
Product and Topic Expert
0 Kudos

The best solution I have ever read!!!! Just to fix the things, that were done wrong.

0 Kudos

Many thanks! If I use a temporary table first via "into local temporary table". Can I access the temporary table via SYSTAB as well?

VolkerBarth
Contributor
0 Kudos

No, local temporary tables are not contained in the system catalog. However, you can use select into with permanent tables, too.

0 Kudos

Ok, then I don't get this logic to work with temporary tables. Right? Can I use something similar to replace the column names directly in a sql with EXECUTE IMMEDIATE?

0 Kudos

Something like this:

declare @sql long varchar;

SET @SQL  = string('select ',(select list(if left(string(name), 1) = '''' then substr(string(name), 2, length(string(name) ) - 2) else string(name) endif ) from sa_describe_query('select * from test') ), ' from test');

execute immediate with result set on @sql; 
end;

But this does not work... Can I get results in a temporary table with execute immediate?

Breck_Carter
Participant
0 Kudos

You cannot use ALTER TABLE on a local temporary table.

0 Kudos

Ok, I can do something similar and use local temporary table or directly in the sql?

VolkerBarth
Contributor
0 Kudos

So what are you trying to achieve? I guess we might be more able to help on your actual goal instead of commenting on valid/invalid syntax...

0 Kudos

Yes, you are right. I apologize for that. What I want to achieve is to remove quotes in the field name of my sql when these occur. The table contains the field names with quites, but I cannot change these. What I then thought was to first make a temporary table and use Breck's suggestions, but it does not work on temporary tables. So what I want is to remove quites from the field names in a sql.

Breck_Carter
Participant
CREATE TABLE test ( 
   Mat        VARCHAR ( 10 ),
   "'Field1'" VARCHAR ( 10 ),
   "'Filed2'" VARCHAR ( 10 ),
   "'Field3'" VARCHAR ( 10 ))
INSERT test VALUES ( 'AB01', 'xd', 'ab', 'aa' );
COMMIT;
Select * from test;
CREATE VIEW vtest (
   Mat,
   Field1,
   Filed2,
   Field3 )
AS SELECT * FROM test;
Select * from vtest;

Mat        'Field1'   'Filed2'   'Field3'   
---------- ---------- ---------- ---------- 
AB01       xd         ab         aa         

Mat        Field1     Filed2     Field3     
---------- ---------- ---------- ---------- 
AB01       xd         ab         aa         
Breck_Carter
Participant

With EXECUTE IMMEDIATE to CREATE VIEW...

CREATE TABLE test ( 
   Mat        VARCHAR ( 10 ),
   "'Field1'" VARCHAR ( 10 ),
   "'Filed2'" VARCHAR ( 10 ),
   "'Field3'" VARCHAR ( 10 ))
INSERT test VALUES ( 'AB01', 'xd', 'ab', 'aa' );
COMMIT;
Select * from test;

BEGIN
DECLARE @SQL LONG VARCHAR;
SELECT STRING ( 
          'CREATE VIEW vtest ( ',
          LIST ( IF LEFT ( SYSTABCOL.column_name, 1 ) = ''''
                    THEN SUBSTR ( SYSTABCOL.column_name, 
                                  2,  
                                  LENGTH ( SYSTABCOL.column_name ) - 2 )
                    ELSE SYSTABCOL.column_name
                 END IF,
                 ', '
                 ORDER BY SYSTABCOL.column_id ), -- column order 
          ' ) AS SELECT * FROM test' )
  INTO @sql
  FROM SYSTAB 
          INNER JOIN SYSTABCOL
                  ON SYSTABCOL.table_id = SYSTAB.table_id
 WHERE SYSTAB.table_name = 'test' 
  AND USER_NAME ( SYSTAB.creator ) = 'DBA';
SELECT @sql;
EXECUTE IMMEDIATE @sql;
END;
Select * from vtest;

Mat        'Field1'   'Filed2'   'Field3'   
---------- ---------- ---------- ---------- 
AB01       xd         ab         aa         

@sql                                                 
----------------------------------------------------------------------- 
CREATE VIEW vtest ( Mat, Field1, Filed2, Field3 ) AS SELECT * FROM test

Mat        Field1     Filed2     Field3     
---------- ---------- ---------- ---------- 
AB01       xd         ab         aa         
VolkerBarth
Contributor
0 Kudos

With v17 you could also use TRIM to remove leading/trailing quotes, so it would just be

... LIST ( TRIM ( SYSTABCOL.column_name, ''''), ', ' ORDER BY...

Breck_Carter
Participant
0 Kudos

> TRIM ( string-expression [ , trim-char-set ] )

Awesome! ...that's gonna be useful.

Answers (0)