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;
Related
Suppose I have an employees table in Postgres DB where I have to insert a value for an employee name which is john's
Since it's Postgres I will escape the single quote ' by doubling them up -> ''
So john's will become john''s
Now when I select that particular row/instance using select query I have to double the quote again. So to select the value john''s I have to write 'john''''s' and my query becomes -
select * from employees where name = 'john''''s'
Is this the best approach? or
Is there any alternative to this process of data insertion and selection for these particular type of data (contains quote)? Any suggestion ?
No you don't have to double the escaped quotes:
select *
from employees
where name = 'john''s'
I have an employees table and I want to add a third column valued as the concatenation of the first and last name called "FullName". How can I accomplish that without losing any data from either of the first two columns?
Quick preface: this answer was based on the originally incorrect tag that this question was relating to SQL Server. I'm no longer aware of its validity on Oracle SQL Developer.
ALTER TABLE Employees ADD FullName AS (FirstName + ' ' + LastName)
Although in practice I'd advise that you do that operation in your SELECT. That's somewhat personal preference, but I tend to think doing things in your end queries is a bit cleaner, more readable, and easier to maintain than storing extra, calculated columns.
Edit:
This was eventually found as the answer, and listed by the OP as a comment on this post. The following is appropriate syntax for Oracle Sql Database.
ALTER TABLE emps MODIFY (FULL_NAME VARCHAR2(50) GENERATED ALWAYS AS (first_name || ' ' || last_name) VIRTUAL);
If you need fullname column all time when you select from database then you can create computed column at the time of creation of your table employee.
for example:
CREATE TABLE Employee
(
FirstName VARCHAR(20),
LastName VARCHAR(20),
FullName AS CONCAT(FirstName,' ',LastName)
)
INSERT INTO Employee VALUES ('Rocky','Jeo')
SELECT * FROM Employee
Output:
FirstName LastName FullName
Rocky Jeo Rocky Jeo
It depends on your purpose, whether you really need to add a new column to your database, or you just need to query out the "full name" on an as-needed basis.
To view it on the fly, just run the query
SELECT firstname + ' ' + lastname AS FullName FROM employees
Beyond that, you also can create a simple Stored Procedure to store it.
(For single result use equal to in the where condition)
select *
from TABLE_name
where (Column1+Column2) in (11361+280,11365+250)
In addition to #Jacky 's answer, if you are trying to add this to a query and not the table, there is also the CONCAT() function that you can use in the select statement
SELECT CONCAT(FirstName, ' ', LastName) as FullName
FROM table_name
I have a table with around 100 columns, I could get for a single column by using the following query
Select * from tes_tbl where col_1 like ' %'
But how can I retrieve all the columns in a single query
Im using oracle database
P.s: l'm a beginner to SQL
You've not specified which sort of SQL so I'm going to assume MS SQL Server.
As others have pointed out you can generate a query along the lines of
select * from tes_tbl where col_1 like '% ' OR col_2 like '% '
However you've mentioned that you've got a lot of columns to query. (This is the MSSQL specific bit) You can query Information Schema to get a list of the columns in your table:
select 'OR [' + COLUMN_NAME + '] like '' % ''' from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'tes_tbl'
This will give you a list of OR clauses for each of the columns. You can use these to build your query (replacing the part of the WHERE clause in the query above).
You can also use this trick to explicitly name the columns in the select which is often a good idea for anything other than ad-hoc queries.
You can use OR operator as like below,
Select * from tes_tbl where col_1 like '%' OR col_2 like '%'
This will give you the result set which satisfies the conditions in WHERE clause
I am working with SQL. At my company, we have a table that shows the text that a technician has written while on a service call. However, when the text is saved in a table, IT separates it out by line. In other words, one call text could contain five or more records. I would like to write a query that will show the call number one time and the text that goes with it in one record. The problem is that the call text can be anywhere from 5 to 25 lines / records. Each record has the same call number. Is there a way in SQL to concatenate all the lines of text for one call?
In case you are using SQL server, you can use the following query:
select call_number,
stuff((SELECT distinct ' ' + text_column
FROM Table_Name T2
where T2.call_number = T1.call_number
FOR XML PATH('')),1,1,'')
from Table_Name T1
group by call_number
Try using GROUP_CONCAT
SELECT GROUP_CONCAT(description, '. ') FROM calls WHERE number = '123456';
I am using Postgresql and I need to run a query where I do a SELECT DISTINCT on a single string column. However I don't want to select the column as is, I need to substring it on the first occurrence of this string ' ('.
I do not know how to do this substring part..
Here is an example of the query without the substring part:
SELECT DISTINCT ON (Table.Column1) Table.Column2
FROM Table
ORDER BY Table.Column1
I am not sure what functions to use in postgres or possibly I need to use plpgsql to do this?
I managed to work this out. The function to be used is SPLIT_PART. Which takes three parameters ColumnName, Characters, And the substring occurance.
Here is an example of how I have used it.
SELECT DISTINCT ON (SPLIT_PART(Table.Column1, ' )', 1)) Table.Column2
FROM Table
ORDER BY SPLIT_PART(Table.Column1, ' )', 1)