Query to display the name of the user(s) having the longest name, sorted by the name of the user. Please note that there may be leading or trailing spaces in the names of the users.
I tried the following query:
select name, max(length(trim(name))) as length
from user
group by name
order by name;
but it gives me the length of all the values of the name column without spaces.
But I need only those values which have maximum length.
Suppose there are 15 names in the column and there are 5 names which are of longest length, so all those 5 names and corresponding length.
Table name: user
Column name and datatype:
name varchar(255)
SELECT *
FROM users
WHERE length(trim(name)) = (SELECT max(length(trim(name))) FROM users)
ORDER BY name
select name from user where length(name) = (select max(length(name)) from user) order by name;
this worked for me !!
Related
Suppose I have a tableX with the following columns accordingly -
id | name | description | contact_no
Now if I perform an insert operation
insert into tableX(id, contact_no, name, description) value(?,?,?,?)
[note the order of the coulmns]
In which order oracle will insert each column values?
i) according to the insert statement
ii) according to the column order in the tableX
iii) or oracle sort the columns alphabetically [id, contact_no, description, name] and put them accordingly.
It will put values based on column list:
insert into tableX(id, contact_no, name, description)
-- 1st 2nd 3rd 4th
values(1st, 2nd, 3rd, 4th);
If you omit column list then you will get "blind insert"(common anti-pattern).
According to the column you specified in the insert from left to right
insert into tableX(id, contact_no, name, description) value(1,2,3,4)
I have a LOG_TAB table where column COLLEC_VAL values are Abcd#123|Mnbv#1234|Poiu#1234|Asdf#1234|....... in oracle. These values are not fixed. I want to keep only latest 4 string or character in the column and rest of the values will be deleted, that is Abcd#1234 will be deleted checking with delimiter(|).
After deleting first one, below values will be available:
Mnbv#1234|Poiu#1234|Asdf#1234|.......
how it will check that already 4 or grater than 3 delimiter exist and delete the first one? Please help.
Try this
update log_tab set collec_val = substr(collec_val, instr(collec_val, '|', -1, 4)+1)
this will help for all your columns try this on a table,it show you sample emp table's data:
select SUBSTR(listagg(ename,'|') within group (order by ename),instr(listagg(ename,'|') within group (order by ename),'|',-1,4)+1) employee_name from emp;
I have table in mysql with columns "name" and "views". I want sum views from serial whose name is in "name" column and display list of serials with views by echo from the most popular.
I think you are looking for this query:
SELECT name, SUM(views) as total_views
FROM serial
GROUP BY name
ORDER BY total_views DESC
I want to create a check constraint on several thousand columns in a database, but all constraints needs a name that is unique in the database. I wanted to use a guid, but because of limitations in Oracle, the name can't be longer than 30 characters.
Here is an example of the syntax:
CREATE TABLE mytable
(
col1 DATE
DEFAULT to_date('19000101', 'yyyymmdd')
CONSTRAINT unique_name_needed CHECK(col1 = TRUNC(col1))
NOT NULL
)
We use a 3 letter short name for every table. These three letters we use to name our constraints. So the short name for mytable could be mtb.
Constraint names are then:
Primary: mtb_pk
Unique: mtb_uk
Foreign: mtb_otb_fk where otb is the short name of the other table.
The trick of course is to come up with unique short names for every table.
In SQL we will be having a sequence. But it should be appended to a variable like this
M1,M2,M3,M4....
Any way of doing this ?
Consider having the prefix stored in a separate column in the table, e.g.:
CREATE TABLE mytable (
idprefix VARCHAR2(1) NOT NULL,
id NUMBER NOT NULL,
CONSTRAINT mypk PRIMARY KEY (idprefix, id)
);
In the application, or in a view, you can concatenate the values together. Or, in 11g you can create a virtual column that concatenates them.
I give it 99% odds that someone will say "we want to search for ID 12345 regardless of the prefix" and this design means you can have a nice index lookup instead of a "LIKE '%12345'".
select 'M' || my_sequence.nextval from dual;