How do I extract the last name, regardless of the length of the name.
I'm using this code
select
customer_name as customer_name,
regexp_extract(customer_name, "(.*?) (.*?)", 1) as first_customer_name,
regexp_extract(customer_name, "(.*?) (.*?)", 2) as last_customer_name
from unificacao_cliente_incremental.cliente
limit 5;
but in the first query it returns the first name, in the second query it returns white
I expected it to return the last nameresult
Related
I have an mysql query like this:
select empcode from employee where empcode like 'LAMO%' order by empcode desc limit 1;
and it's result like this,
But my actual records is,
I need output like this LAMO10
The field empcode is having a data type corresponding to characters.
Here, LAMO1 is less than LAMO2 and so on, but observe that the string is compared character by character. Therefore, LAMO10 is smaller than LAMO2 because while comparing from the left, the first 4 characters LAMO are equal, the 5th character 1 in LAMO10 is smaller than the 5th character 2 in LAMO2. So, the order that you would get (if you removed the limit in your query) is:
LAMO9
LAMO8
LAMO7
LAMO6
LAMO5
LAMO4
LAMO3
LAMO2
LAMO10
LAMO1
This explains why you aren't getting your desired output LAMO10. To generate it, you need to order by only the numbers in your string. In this particular dataset, that number you are looking for appears to be everything onward from the 5th character. The corresponding query segment for ordering would be:
ORDER BY CAST(SUBSTR(empcode,5) AS UNSIGNED) DESC
So, putting it in your query:
SELECT empcode
FROM employee
WHERE empcode like 'LAMO%'
ORDER BY CAST(SUBSTR(empcode,5) AS UNSIGNED) DESC
LIMIT 1;
should get you your desired result.
You may use an order by clause which sorts on the numeric component of the employee code:
SELECT empcode
FROM employee
WHERE empcode LIKE 'LAMO%'
ORDER BY CAST(REPLACE(empcode, 'LAMO', '') AS UNSIGNED) DESC
LIMIT 1;
I have one such sql:
select name from A where id in (23,24,22,23)
When I run it in Navicat, the result only have one result of 23.
My question is, how to keep the number and order of the query results remains the same as (23,24,22,23).
If you want to maintain the order of the result then use order by clause like
select name from A
where id in (23,24,22)
order by id;
Again, assuming that id is a primary key column in your table A then there will be only one row with id = 23. How do you expect the same row to get repeated automatically unless you make it explicit by using a UNION ALL
If you really really want to fetch the records like this, you can use field function to get 23,24,22 and order by this sort:
select name from A where id in (23,24,22) order by field(id, '23,24,22')
then use union all get another 23:
(select name from A where id in (23,24,22) order by field(id, '23,24,22'))
union all
select name from A where id = 23
I have a table in which there is column name as mappedcloumnname and fieldname and my fieldcolumn contains address1, address2, city, state, customerid, country and mappedcolumn contanins c1-c20. I wrote a query to sort my data based on mappedcolumn name but the order what am getting is wrong one
SELECT * FROM customermetadata
WHERE OrgID = in_orgid
ORDER BY MappedColumnName;
You can give it a try:
SELECT
*
FROM
customermetadata
WHERE
OrgID = in_orgid
ORDER BY CAST(SUBSTRING(MappedColumnName FROM 2) AS UNSIGNED);
Note:
Here I've extracted the number from the MappedColumnName and sort the records based the extracted numbers.
I've created a demo where the table contains only two columns (id and col).
col column contains value like c1,c2,....
See demo if you order by col only.
See demo if you order by extracting number from col.
If your column MappedColumnName contains value from c1-c20, then it will be treated as String(VarChar) in SQL and data will not be sorted properly.
You should try in this way,
select * from customermetadata
where OrgID=in_orgid
Order By CAST(SUBSTR(MappedColumnName FROM 2 FOR LENGTH(MappedColumnName)) AS UNSIGNED); ;
NOTE: A longer number is also a bigger number for numbers of the same length, you can make a textual comparison, because '0' < '1' .... < '9' and it will short Alpha-numeric order.
MYSQL Query How to get first 10 char from $_GET from column customername from mytable.
SELECT * FROM mytable WHERE 'customername'=".mysql_real_escape_string($_GET['name'])"
Which operator to use ? TOP selects rows
I want to get first 10 characters of all records in column customername.
I have a big table, where one of the columns have text responses from many users (thousands of rows). I need to find the total number of occurance of a partcular word in that response column. If I use
SELECT count(*) from Table where Response like '%wordtobefound%'
it gives number of rows that contains the 'wordtobefound', but I need total number of occurances of the word 'wordtobefound' in all rows.
Note: I would prefer a user defined function that I can add to database and use again and again.
SELECT SUM(BINARY (LENGTH(field_name) - LENGTH(REPLACE(field_name, "your_word", "")))/LENGTH("your_word")) FROM table_name;
Try this :
SELECT
CAST((LENGTH(Response) - LENGTH(REPLACE(Response, 'wordtobefound', ""))) / LENGTH('wordtobefound') AS UNSIGNED) AS wordCount
FROM Table
You can use a SUM for the total :
SELECT
SUM(CAST((LENGTH(Response) - LENGTH(REPLACE(Response, 'wordtobefound', ""))) / LENGTH('wordtobefound') AS UNSIGNED)) AS wordCount
FROM Table