Properly Formatting Tables in SQLPLUS - sql

Before this gets voted as a duplicate question, I have spent quite some time on S.O. trying to fix this. As you can see in the screenshot below, my tables are looking messy.
For the first and second table, you can see how there are 2 sets of column headings for each table. So how can I, for example, get all 5 customer records to be included in one table?
The other thing I can't figure out is how to get the column headers to be properly formatted(not truncated and all on the same row). *I have tried changing the linesize, SET WRAP OFF; etc.
If anyone could help explain this fix it would be greatly appreciated!

SQLPLUS layout mostly depends on :
what he thinks the width of your terminal is
either the length of the field that you ask it to display (as defined in the database structure), or the size of the field name
When the sum of the field lenghts is greater than the terminal size, each row is splitted over 2 or more lines... and the display gets messy.
But you do have control over these parameters.
First thing to do is to define a proper line size, ie one that fits well in your terminal screen. For example let’s make it 120 chars :
set linesize 120
You may then manually set the length of each field, if you are not satisfy with the length chosen by Oracle. For example for a varchar field you can do :
column first_name format a10
which allows a max length of 10 for the field called FIRST_NAME in the query result. The setting applies to all column with the same name, even in the subsequent queries, for the lifetime of the session. It can be removed with :
column first_name clear
You also have the option of formatting data types, which will apply to all columns having the concerned data type.
For more information see the SQLPLUS documentation.

Related

Is there a way to reduce gap between two column headings in DB2

I am working on IBM I series VR7, and running SQL(DB2) using CLLE.
I have a SQL procedure in a TXT file, having below command to create a table in QTEMP.
create table qtemp.FILE1 as (
select
Field1,Field2,Field3,.....Field10 from FILE2 ) with data;
I am calling the above procedure from CLLE using below command.
RUNSQLSTM SRCFILE(MyLib/MySrc) SRCMBR(Proc_txt) COMMIT(*NONE)
And then running below command to generate the spool.
RUNQRY QRYFILE((FILE1)) OUTTYPE(*PRINTER) OUTFORM(*DETAIL) FORMSIZE(60 132)
FORMTYPE(*STD) COPIES(1) LINESPACE(1)
The issue I am facing is that I am getting 2 white spaces between columns while creating the table using the create table command. When that table is converted into a spool file using above RUNQRY command, the fields on the right side truncates as my report width is 132 by default and I can not change it.
If the white spaces in the table created can be reduced to 1, my issue will be resolved.
The SQL I am using IBM i Series' default and DB2 as database. I don't have much idea about their version.
Edit2: Another issue I had was of report having a field in second line. Actually as per requirement a field had to be in the second row under another field. For example I needed field10 under field5. I have fixed it too, read my answer below.
Hope it helps people in need but I really doubt.
Edit1: I have updated the question as requested. Any help would be much appreciated. Thanks.
The short answer is that yes you can define the report to have 1 space between columns, but you have to define the Query400 object to do that. Unfortunately this is not a good place to write a tutorial for Query400. I can get you started though.
Type wrkqry, press enter.
Then put the cursor on the query name field, and press F4. You are now in the tool. You need to create a new query, and define everything about it in this tool. Play around with it, and see if that helps you.
I was able to get what I needed. As others have suggested, I have finally used WRKQRY to control the column spacing. Reduced the column spacing to 1 and was able to get the columns needed in the 132 width.
Another issue I had was of report having a field in second line. Actually as per requirement a field had to be in the second row under another field. For example I needed field10 under field5. So what I did was, I used the Line wrapping feature available in WRKQRY.
How I did:
Create a WRKQRY object and select the file needed.
Sequenced the field I needed in second line, to the bottom.
Go to Select Output Type and Output Form and take Y on Line Wrapping field. Put the
wrapping width equal to your report width. Leave other fields as required.
This way each record will have 10th field in next row, if it has data. You can add as
many as fields.
You may have to add some white spaces to the field for proper alignment. I would
suggest to create a new field and use concat(||) operator available in WRKQRY.
Thanks everyone for helping.

Is there a way to use variables in vba to identify MS-Access report fields?

I am not a programmer, but have been tasked with doing this anyway! We are working on a research project that involves testing properties of different samples. I am trying to create a form that will generate a custom report based on what the user chooses. So, I have multiple text boxes and check boxes to allow the user to define the query parameters (e.g. composition of the sample must contain at least 5% component A) and choose what data they are interested in seeing in said report (e.g. show pH, color, but not melting point). I have successfully created code to generate the query, then generate a report based on that query, but the report defaults to column widths that are generally too big (for example, the pH column width is 3 inches, it only needs to be about 1). I would like to be able to fix this, but have not been able to figure out how. At the same time, some of these fields contain numbers that are averages of multiple test results, so I would like to limit the number of digits shown, and display them as % where appropriate. I started with just fixing the column width issue:
I have tried to make a collection of the fields that are included, then loop through the collection and set column widths, but cannot figure out how to identify a field with a variable:
If I know the field name I can do this:
Reports("ReportName")!FieldID.Width = 200
But if I have a collection of names, FieldNames, or a string VariableName, none of these work, giving me an error that FieldNames or VariableName is not a valid field in the report:
Reports("ReportName")!FieldNames(1).Width = 200
Reports("ReportName")![FieldNames(1)].Width = 200
Reports("ReportName")![VariableName].Width = 200
Is there a way to reference a field name with a variable?
Alternatively, I thought there might be a way to loop through all fields and set widths - this would involve looking up a column width for each field, which I thought to do by adding a key to a collection of column widths. But I cannot find a way to do that, something like:
For each Field in Reports("Report")
Field.Width = ColumnWidthCollection(Field)
Next
This hangs up on the Field.Width line, with "invalid procedure call or argument", which brings me back to how to reference a field name with a variable.
Any help would be greatly appreciated!
Try with:
Reports("ReportName")(VariableName).Width = 200

Turn string variables into numeric representatives and store the strings elsewhere?

I don't know the best way to describe my problem and I'm just looking for a push in the right direction, or where to start. I'd be perfectly happy with an answer that's a very useful link or pseudo code.
My problem, I have a database that's about to hit the MS Access hard coded 2 GB database limit and I don't want to split the database.
What I think is a possible solution - make the database more efficient in it's data storage. I think, but don't know if this is true, that I could do this by turning some string fields into numeric fields. Stay with me...
For instance:
My database has several million records of a field we'll call TooLongString
Each value is about 50 characters
Every record has a value for this field
There's only 9 possible values for TooLongString
Would it decrease my database size to instead store a number that
represents one of the 9 possible values and store the text value in a small table? (So go from 50 characters to 1 character several million times)
Did I explain my issue correctly? Is my potential solution actually a solution? How would I go about doing this?
Thanks!
The short answer is yes, that would reduce the size of your database. You could have a second table that holds the nine possible values for "TooLongString" and just store the ID of the appropriate answer in the main table, as you suggested. You would then need to join these tables when pulling the data out in order to retrieve the actual text instead of the ID.
I would set up your new table first, then add a new column for the ID into your existing one. As there are only nine possible values, I'd be tempted to just manually run an UPDATE query nine times, e.g. if the first string in your new table is "MyFirstString" with ID 1, you could run "UPDATE existingTableName SET newColumn = 1 WHERE oldColumn = 'MyFirstString'". Do this for each of the nine values then you can remove the old string column from your table at the end.

Changing the length of Text fields in an Access linked table

I am exporting a file from a system as .csv. My aim is to link to this file as a table (which matches the output field for field) and then run the queries and export.
The problem I am having is that, upon import, all the fields are 255 bytes wide rather than what they need to be.
Here's what I've tried so far:
I've looked at ALTER TABLE but I cannot run multiple ALTER TABLE statements in one macro.
I've also tried appending the table into another table with the correct structure but it seems to overwrite the structure.
I've also tried using the Left function with the appropriate field length, but when I try to export, I pretty much just see 5 bytes per column.
What I would like is a suggestion as to what is the best path to take given my situation. I am not able to amend the initial .csv export, and I would like to avoid VBA if possible, as I am not at all familiar with it.
You don't really need to worry about the size of Text fields in an Access linked table that is connected to a CSV file. Access simply assigns each Text field the largest possible maximum size: 255. It does not mean that every value is actually 255 characters long, it just means that any values in those fields can be at most 255 characters long.
Even if you could change the structure of the linked table (which you can't), it wouldn't make any real difference except to possibly truncate longer Text values, and you could easily do that with a String function. For example, if a particular field had to be restricted to 15 characters then you could simply use Left([fieldName], 15) as a query column or as the control source in a report.
In the end, as the data set is not that large, I have set this up to append from my source data into a table with the correct structure. I can now run my processes against this table as per normal.

Use columns.add(...) in Word with non-uniform column widths?

Problem I'm having is that table.Columns.add(ref Object BeforeColumn) requires a reference to another column in the table. However, when I try to access the last column in the table to pass as a reference using table.Columns.Add(table.Columns[table.Columns.Count])
I get the error:
"Cannot access individual columns in this collection because the table has mixed cell widths."
As my current work around, I catch the error, and call table.Columns.DistributeWidth() to make sure the columns are uniform and run the rest of the code. However, I lose the formatting of my cell widths this way, which is unfortunate.
Is there any way I can workaround this without losing the cell width?
(I realize one way is to store every cell's width before running this process, and then re-applying the widths afterward, but this seems like a very costly solution to something that should be simpler)
I've found one way to do it. Here's how I approached it.
*Caution, I'm assuming that the table is uniform. i.e. The number of columns is the same across all the rows. (Note, the API has a Table.uniform function, but the description is not complete. In the API it says "True if all the rows in a table have the same number of columns." However, it also checks if the columns have uniform width).
Instead of using table.Columns.Add(table.Columns[table.Columns.Count]) to add a column before the last below, I select a cell in the table and used the insert command:
//assuming table is the name of the table you want to add columns to
table.Cell(1, table.Columns.Count).Select();
word.Selection selection = table.Application.ActiveWindow.Selection;
selection.InsertColumns();
This might actually be a better way to add columns, as the api gives you way more options on how to insert (i.e. use InsertColumnsRight to insert to the right of the column). The Columns.Add(...) function by default inserts to the left of the select