I have a column called people in my table which is a statement. I need to extract only the names from that column.
people:
Ramu is a good dancer.
Raj is the highest scorer in maths.
I need to extract only names (ramu,raj) from these statements.
Hint: names before is since all statements has a word is here.
I don't how to extract in postgresql
Use split_part():
with people(statement) as (
values
('Ramu is a good dancer.'),
('Raj is the highest scorer in maths.')
)
select split_part(statement, ' ', 1) as name
from people;
name
------
Ramu
Raj
(2 rows)
Related
I am using PostgreSQL and Windows.
I have column data that looks like this:
Table name: employees
I need to run an SQL query where the result will:
Display only FullName column.
Display ONLY THE FIRST WORD of each name in the column.
What would be the SQL query?
There are all sorts of string functions described here String operators/functions. One that would work is:
select split_part('Adrian Klaver', ' ', 1);
split_part
------------
Adrian
--So in your case
Select split_part("FullName", ' ', 1) from employees;
I am trying to run few queries on Oracle SQL developer
e.g
Select name AS CandidateName, age AS CandidateAge
from tbl_candidate_details
order by candidate_id desc
But In the result I am getting the column names as "CANDIDATENAME" AND "CANDIDATEAGE".
Is there a way where I can get this as camelcase characters what I have given in the select statement("CandidateName" and "CandidateAge") ?
If the column aliases are wrapped in double-quotes, SQL Developer will use those exact values as the column names in the query results:
SELECT name AS "CandidateName", age AS "CandidateAge"
FROM tbl_candidate_details
ORDER BY candidate_id DESC;
Otherwise, the column names in the query results are always displayed in upper case
SQL is case insensitive and the SQL standard requires to fold all un-quoted identifiers to uppercase. If you want to preserve the case of your identifiers you need to quote them:
Select name AS "CandidateName",
age AS "CandidateAge"
from tbl_candidate_details
order by candidate_id desc
I'm using Hora Keep Tool for SQL queries. When I use operations for example like
avg(age) to build the average value of a few values for the "group by" functionality, than it changes during the query in the output the original column name from "age" to "avg(age)".
I know that a string after the operation for example:
avg(age) age
would rename the column name, but I have a lot of rows, which I have to change than like this.
Is there a function which would not allow that during the query the column name would change from age to avg(age) ?
I would be happy if somenone could give me helpfully tips.
Thank you forward.
You can use ' as ' keyword in sql to name a column e.g
select user_name as username from users;
this query will result the column name as username.
---------------
USERNAME
---------------
hello
hello1
hello3
I want to find the longest VARCHAR in a specific column of a SQL Server table.
Here's an example:
ID = INT IDENTITY
DESC = VARCHAR(5000)
ID | Desc
---|-----
1 | a
2 | aaa
3 | aa
What's the SQL to return 3? Since the longest value is 3 characters?
Use the built-in functions for length and max on the description column:
SELECT MAX(LEN(DESC)) FROM table_name;
Note that if your table is very large, there can be performance issues.
For MySQL, it's LENGTH, not LEN:
SELECT MAX(LENGTH(Desc)) FROM table_name
Watch out!! If there's spaces they will not be considered by the LEN method in T-SQL. Don't let this trick you and use
select max(datalength(Desc)) from table_name
For Oracle, it is also LENGTH instead of LEN
SELECT MAX(LENGTH(Desc)) FROM table_name
Also, DESC is a reserved word. Although many reserved words will still work for column names in many circumstances it is bad practice to do so, and can cause issues in some circumstances. They are reserved for a reason.
If the word Desc was just being used as an example, it should be noted that not everyone will realize that, but many will realize that it is a reserved word for Descending. Personally, I started off by using this, and then trying to figure out where the column name went because all I had were reserved words. It didn't take long to figure it out, but keep that in mind when deciding on what to substitute for your actual column name.
Gives the Max Count of record in table
select max(len(Description))from Table_Name
Gives Record Having Greater Count
select Description from Table_Name group by Description having max(len(Description)) >27
Hope helps someone.
For SQL server (SSMS)
Option 1:
-- This returns number of characters
select MAX(LEN(ColumnName)) from table_name
Option 2:
-- This returns the number of bytes
select MAX(DATALENGTH(ColumnName)) from table_name
If you're using VARCHAR, use DATALENGTH. More details
SELECT TOP 1 column_name, LEN(column_name) AS Lenght FROM table_name ORDER BY LEN(column_name) DESC
Many times you want to identify the row that has that column with the longest length, especially if you are troubleshooting to find out why the length of a column on a row in a table is so much longer than any other row, for instance. This query will give you the option to list an identifier on the row in order to identify which row it is.
select ID, [description], len([description]) as descriptionlength
FROM [database1].[dbo].[table1]
where len([description]) =
(select max(len([description]))
FROM [database1].[dbo].[table1]
SELECT MAX(LEN(Desc)) as MaxLen FROM table
select * from table name from where length( column name) =(select MAX(Length(column name) from table name);
I am using subquery its 100 % work try this.
For IBM Db2 its LENGTH, not LEN:
SELECT MAX(LENGTH(Desc)) FROM table_name;
I have a table, users, in an Oracle 9.2.0.6 database. Two of the fields are varchar - last_name and first_name.
When rows are inserted into this table, the first name and last name fields are supposed to be in all upper case, but somehow some values in these two fields are mixed case.
I want to run a query that will show me all of the rows in the table that have first or last names with lowercase characters in it.
I searched the net and found REGEXP_LIKE, but that must be for newer versions of oracle - it doesn't seem to work for me.
Another thing I tried was to translate "abcde...z" to "$$$$$...$" and then search for a '$' in my field, but there has to be a better way?
Thanks in advance!
How about this:
select id, first, last from mytable
where first != upper(first) or last != upper(last);
I think BQ's SQL and Justin's second SQL will work, because in this scenario:
first_name last_name
---------- ---------
bob johnson
Bob Johnson
BOB JOHNSON
I want my query to return the first 2 rows.
I just want to make sure that this will be an efficient query though - my table has 500 million rows in it.
When you say upper(first_name) != first_name, is "first_name" always pertaining to the current row that oracle is looking at? I was afraid to use this method at first because I was afraid I would end up joining this table to itself, but they way you both wrote the SQL it appears that the equality check is only operating on a row-by-row basis, which would work for me.
If you are looking for Oracle 10g or higher you can use the below example. Consider that you need to find out the rows where the any of the letter in a column is lowercase.
Column1
.......
MISS
miss
MiSS
In the above example, if you need to find the values miss and MiSS, then you could use the below query
SELECT * FROM YOU_TABLE WHERE REGEXP_LIKE(COLUMN1,'[a-z]');
Try this:
SELECT * FROM YOU_TABLE WHERE REGEXP_LIKE(COLUMN1,'[a-z]','c'); => Miss, miss lower text
SELECT * FROM YOU_TABLE WHERE REGEXP_LIKE(COLUMN1,'[A-Z]','c'); => Miss, MISS upper text
SELECT *
FROM mytable
WHERE FIRST_NAME IN (SELECT FIRST_NAME
FROM MY_TABLE
MINUS
SELECT UPPER(FIRST_NAME)
FROM MY_TABLE )
for SQL server where the DB collation setting is Case insensitive use the following:
SELECT * FROM tbl_user WHERE LEFT(username,1) COLLATE Latin1_General_CS_AI <> UPPER(LEFT(username,1))