Sqlplus formatting width, as specified - sql

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;

Related

Filter unwanted characters from a column in Teradata

I have a Phone number column in my table with values only being numbers and no special characters. for one of the column I got a value coming in as ":1212121212".
I will need to filter this record and any records coming in with any special characters in teradata. Can anyone help on this.
I have tried the below solutions but it is not working
where (REGEXP_SUBSTR(column_name, '[0-9]+')<>1 or column_name is null )
In MS SQL Server DB's, you can use TRYCAST to find those entries having non numeric characters:
SELECT column_name
FROM yourtable
WHERE TRY_CAST(column_name AS INT) IS NULL;
In Teradata DB's, you can use TO_NUMBER:
SELECT column_name
FROM yourtable
WHERE TO_NUMBER(column_name) IS NULL;
If you want to stay close to your attempt, can use LIKE to find not numeric entries:
SELECT column_name
FROM yourtable
WHERE column_name LIKE '%[^0-9]%';
Note this could get slow when your table has very many rows.
Thanks Jonas. Since I need only numeric values and the length should be 10, I tried the below and it worked. This would ignore all the additional special characters.
(regexp_similar(Column,'[0-9]{10}')=1)

Show only column name in a HUE hive query

So in Hue I've entered a simple query(has to be as simple as possible, as others will run it too) to just get a limit of 20 records. The query is:
Select * from tablename Limit 20
The problem is that the query returns column names in this format: tablename.columnname
I need JUST the column name to be returned, NOT the table name referenced at all. How is this achieved without going into a large "from" statement spelling out all of the columns(only other way I currently know)?
Thanks in advance!
Not the best but you could do a right click on the '*' of SELECT * and then expand all the column names:

Truncate not workng in MySQL

I guess the correct way of truncating a value in MySQL is truncate(value,limit); but that doesn't seem to be working here it needs an extra table name;
select truncate(94204.27348,2);
ERROR at line1:
ORA-00923: FROM keyword not found where expected
Assuming you are using Oracle, you must supply a table reference on your select query.
Oracle select queries require a table reference always.
You can do this....
Select truncate(94204.27348,2) -- see further comment below
From dual
;
Dual is a special table that allows these sort of queries.
Also, I think you might mean to use the TRUNC function.
As others have pointed out, the TRUNCATE query is not quite the same, it will erase the contents of the table that you supply it.
In Mysql You should use round. Truncating table will remove the data from the table and reset all your auto increment values.
SELECT ROUND(94204.27348,2);
Result - 94204.27
This will round the value and display only two decimal places.

SQL code for retrieving values of column starting using wildcard

I want to look for values in variable/column which start with 'S' and has 'gg' in between.
For instance Staggered is a word which starts with alphabet S and has gg in between the word.
so what sql query to write to get the result.
Due to the fact that you did not provide much meta information (which database?), I'll just show the following:
SELECT * FROM <table>
WHERE <columnname> LIKE 'S%gg%';
Good luck :)
As the target database is not mentioned, I will answer with Oracle syntax:
select *
from TABLE_NAME
where COL_NAME like 'S%gg%'

Oracle, dynamically determine column name

Suppose I have a table of custom column names that all have the pattern COL##, where ## is any integer. So a typical query would be:
select COL12 from MyCustomTable;
So in another table, I have all those integers and I'd like to create a query using the table of integers to construct a dynamic query into MyCustomTable.
Something like:
select 'COL' || (select colId from IdTable where Id = 12) from MyCustomTable;
But instead of just returning the string 'COL12' for every row, return the actual values identified by the column name COL12.
Don't worry about my overall problem :) I'm just curious to know if I can do this from a sqldeveloper window directly without writing any code/procedures/functions, etc.
An obvious and absolutely insecure way of doing this would be usage of EXECUTE IMMEDIATE statement.
Another is to use SPOOL command to output results into temporary file, then set SPOOL OFF and execute this file with # directive.