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)
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 a query but it gives me error on concat of missing expression. i am building an insert query where one columne values are inserted from another table by concatenating the two columns and getting first letter from first column and whole value from second column.
Heres my query.
SQL> INSERT INTO MSGG_USER (USERNAME)
SELECT substr (GIVEN_NAME, 1,1)GIVEN_NAME, || '' || SURNAME
FROM MSGG_PEOPLE;
You don't need the second mention of GIVEN_NAME, just concatenate the call to SUBSTR() directly:
INSERT INTO MSGG_USER (USERNAME)
SELECT SUBSTR(GIVEN_NAME, 1, 1) || SURNAME;
Off the top of my head, it looks as though you might have been trying to alias the substring call with GIVEN_NAME. In any case, you would not need any aliases in your select statement, as the columns are directly feeding into an insert operation.
Edit:
If you want to insert the first initial and last name in all lowercase, you can try wrapping these terms in LOWER:
INSERT INTO MSGG_USER (USERNAME)
SELECT LOWER(SUBSTR(GIVEN_NAME, 1, 1)) || LOWER(SURNAME);
I want to put a condition in my query where I have a column that should contain second position as an alphabet.
How to achieve this?
I've tried with _[A-Z]% in where clause but is not working. I've also tried [A-Z]%.
Any inputs please?
I think you want mysql query. like this
SELECT * FROM table WHERE column REGEXP '^.[A-Za-z]+$'
or sql server
select * from table where column like '_[a-zA-Z]%'
You can use regular expression matching in your query. For example:
SELECT * FROM `test` WHERE `name` REGEXP '^.[a-zA-Z].*';
That would match the name column from the test table against a regex that verifies if the second character is either a lowercase or uppercase alphabet letter.
Also see this SQL Fiddle for an example of data it does and doesn't match.
agree with #Gordon Linoff, your ('_[A-Z]%') should work.
if not work, kindly add some sample data with your question.
Declare #Table Table
(
TextCol Varchar(20)
)
Insert Into #Table(TextCol) Values
('23423cvxc43f')
,('2eD97S9')
,('sAgsdsf')
,('3Ss08008')
Select *
From #Table As t
Where t.TextCol Like '_[A-Z]%'
The use of '%[A-Z]%' suggests that you are using SQL Server. If so, you can do this using LIKE:
where col like '_[A-Z]%'
For LIKE patterns, _ represents any character. If the first character needs to be a digit:
where col like '[0-9][A-Z]%'
EDIT:
The above doesn't work in DB2. Instead:
where substr(col, 2, 1) between 'A' and 'Z'
I feel like this should be simple enough to do, but have not found any solutions that didn't use replace so far. I have the following select statement I am running, and for some of the columns there are commas separating the values. I would like to replace these commas with semicolons, however I only want to do it in the select statement. I don't want it to alter the values in the tables at all. This is not a one off statement either, or I'd just replace all the commas with semicolons and then revert back.
SELECT a.Category_Id, a.Category_Name, ISNULL(b.Category_Alias, '') as Category_Alias,
ISNULL(b.SUPPORT_NAMES, '') as SUPPORT_NAMES
FROM Categories a
INNER JOIN CategoryInfo b on b.Category_Id=a.Category_Id
For the Category_Alias column, the records are actually stored like CS, Customer Support and I want that to show up as CS; Customer Support just for the select statement.
I believe you may be confused as to what the REPLACE function is doing. You can use REPLACE within your SELECT statement without altering the data in the database:
SELECT REPLACE(MyField, ',', ';') AS NewFieldName
FROM MyTable
I believe you don't want to replace the value physically in the table, but ok to replace on select
So you can
Select REPLACE(ColumnName,',',';')
From TableName
Most SQL servers implement an inline replace function. Most of them are named replace(), and can also be used in a select statement.
Example from MySQL:
SELECT field, REPLACE(field,',',';') FROM my_table;
I want to replace two things in one select statement. Now I can replace one character at a time. But as per my requirment, I need to replace two things in one statement.I want to replace one special character by Zero and another is this data is stored in multiple lines. I want to fetch this data in one line.
select replace(message,chr(ACII_Value),0)
from my_table -- I can replace special character by zero using this.
select (message,chr(10),' ')
from my_table -- I can put all data in single line using this.
Now I want to do this in one select statement.
select replace( replace( message, chr(ACII_Value), 0), chr(10), ' ')
from my_table;
An alternative is the TRANSLATE function:
select translate(message, chr(ASCII_Value)||chr(10), '0 ') from my_table;
Although if you're only replacing two characters, I'd go with #schurik's answer since it uses a simpler, more common function.