I have just created a new database and I'm at the stage now where I am looking to input my data. I've created the tables and the constraints work fine, and I have inserted 1 row successfully. However when running the query select * from mytable; it returns the table once for every field.
I have spent the past 2 hours researching why but cant find anything. Can anyone help please?
Thanks in advance.
I assume that you're using SQL*Plus and that the columns in your table are rather wide (say varchar2(1000) for example).
In this scenario, SQL*Plus's width is to small to horizontally display multiple columns, hence it displays them vertically.
You could get around this with
select
substr(col1, 1, 20) col1_,
substr(col2, 1, 20) col2_
...
from
table;
or, when still in SQL*Plus, with a column format command:
column format col1 a20
column format col2 a20
...
select * from table;
Related
How can you execute the following Oracle/pl-sql code to display two columns side by side?
SELECT table_name, user_cons_columns.column_name
FROM user_cons_columns;
Currently, this is my output formatting:
This is the formatting I hope to see:
Solutions tried:
set long 1000
set linesize 200
Where long and linesize have been changed from 20 to 2000, unsuccessfully. I suspect it's just improper SQL code...but unsure. Thank you in advance!
This has nothing to do with the SQL code (and you should NOT change it, for example by truncating the strings in the SQL query, just to fix a formatting problem).
The issue is that the columns, in the table, are declared of a certain width, say VARCHAR2(1000), and then that's what SQL Plus will reserve by default. You can change that in SQL Plus itself, with SQL Plus commands. In this case, the COLUMN command.
SQL> column column_name format a30
SQL> column table_name format a30
These are SQL Plus commands, so don't end them in semicolon ( ; )
Change a30 to a40 if you want 40 characters per column. Etc.
It is not clear why, if in the output you wanted the table name to appear first, in the query you have the column name first. You should be able to fix that yourself. Also, if you select from just one table, there is no need to prefix column names with the table name. However, if you do, be consistent - do it for both columns. And if you do, it is better to give an alias to the table in the FROM clause, and use the alias in SELECT. These are all unrelated to your original question.
Select only the first N (20) characters from the column_name field.
SELECT SUBSTR(column_name, 1, 20) column_name, table_name
FROM user_cons_columns;
Our application is a Mainframe which is a IBM iSeries – DB2 database set up. Some of our table values have a range.
Ex: 100;105;108;110:160;180
-- UPDATE --
The above data is from a single row (Single column to be precise). In the same format there would be multiple values (on various rows)
It this case, individual values are delimited by a “;” but 110:160 is a range. It includes all the values from 110 to 160. Now, for the individual values we were using like statements obviously. Ex; if I have to query for 105.
The challenge here is, if I had to query 125 which is technically not present in the database. However, logically I need to retrieve that record.
The system (application) somehow was able to accomplish this, I am not sure how. I am not a mainframe developer, I just had to query the database to retrieve a specific record for some of the automation that we work on.
As a workaround, I could think of two things:
Expand the ranges and store it in a temp database programmatically.
Ex: 110:160 would be expanded to 110;111;112..160 (Yes, it’s tedious)
Reduce the number of records, by filtering through certain unique colums (the one’w which are without ranges) then programmatically apply a logic to identify the right record
As both are workarounds, I was so curious to how the system does it. (I reached out to dev’s of the app. So far, no luck). So is there a direct approach to achieve this ? Could it be a stored procedure ?
If i got your question right your example values are not in a single row but in multiple - otherwise some preprocessing has to be done.
I would destruct the combined value into its components with SQL - like:
with temp(id, text, value1, value2) as (
select id, text
,case when posstr(id,':') > 0
then substr(id, 1, posstr(id,':') - 1)
else id
end as value1
,case when posstr(id,':') > 0
then substr(id, posstr(id,':')+1 , length(id))
else id
end as value2
from testrange
)
select * from temp
where 125 between value1 and value2
I have a table in my SQL Server database. I am showing my table with some data in image called input.
And from this input I want to add one derived column in that column the data should be below format. I am showing my expected output in image called output.
How can I achieve my expected output with a SQL query?
We have lots of records in the table, but the maximum length of CODE column is 4. Means the last value in that column is 9999 only.
Please suggest how I can get my expected output with a simple SQL query.
Best Regards,
Phani Kumar.
SELECT *,
'C' + RIGHT('000'+CONVERT(VARCHAR(4),Code),4) [YourColumn]
FROM dbo.YourTable;
So I'm trying to get a part of a value from a column and insert that part into another column - new column. BOTH columns are in the same table. So what i want should look something like this:
id newColumn oldColumn
1 12 123 some text
2 24 246 some text
....
I know how to get 12 and 24 using SUBSTR, but how do i enter the data for each row in the table. Should i be using self-join or something else?
First you have to add new col using following command:-
ALTER TABLE TAB_NAME
ADD COLUMN COL_NAME(VARCHAR(10));
After that execute this command:-
UPDAET TAB_NAME
SET COL_NAME = SUBSTRING(OLDCOLUMN, 1, 2);
I think this might help you.
No need to join, it's just a plain UPDATE:
update tablename set newColumn = substring(oldColumn from 1 for 2)
substring is ANSI SQL, some dbms have substr and other versions.
The question is why you are doing this? What do you expect to find in newColumn if someone later updates oldColumn to another value? Maybe you should have a view instead, where newColumn always has up to date values?
Please get into the habit of ALWAYS specifying the DB engine you are using... It helps us to help you - we can provide more relevant answers.
You might want to consider using a calculated column as opposed to storing the information again.
In SQL Server you could do something like this
ALTER TABLE YourTable
ADD new_column as SUBSTRING(old_column, 1, 2);
This way you don't need to insert or update this column it is always consistent with the original column. and you just use it in your select statement in the usual way.
select new_column from YourTable
I have a basic database table. I would like to implement a functionallity that would allow to insert a select query within a random cell in the table. The result of this query would then be used as as any other cell of elementary type - in my case to compare it to an another value.
The problem is that I do not know in advance how those queries look like.
Here is an example. Say I have a an incoming parameter "score", which assumes some random integer values. I would like to see if the parameter "score" falls within the range defined between the values in Col1 and Col2, and if so happens, then to return the value in Col3.
Table1:
Col1 Col2 Col3
5 10 first row
10 15 second row
20 30 third row
* 50 forth row
* -> select avg(some_number) from Table2;
This random query can occur in any cell and is certain to return a single value. That is why I cannot use a simple JOIN statement.
Edit: Thanks Tim for suggesting to give an example.
You should look at CASE statements in SQL, and also at virtual or symbolic columns whose value is the result of an expression or function.