Actually in my case I need to select street number from a address string, which means if the string is '1234 dummy789 road', I only want to get '1234', not '1234789' Another example is 'Plot 111 dummy 1220' then i want only '111'. and if the string is '111/2 dummy' then i want to get '111/2'
I tried following:
SELECT CASE WHEN substr(address , 1, 1) between '0' and '9'
THEN substr(address , 1, 1)
ELSE 'False'
END as add
from test
<?php
$ab = "1225584454 red 1555 blue";
$result = explode(" ", $ab, 2);
print_r($result);
?>
in this case this will gives you first string in your variable.
Assuming that you have civil number followed by space and street name I would suggest the following:
Put WHERE statement with REGEXP to get those, which start with digit-followed-by-space. And in returned field get only numeric portion with substring.
Something like this:
SELECT SUBSTRING(address, 0, LOCATE(' ', address)) FROM items WHERE `address` REGEXP '^[0-9]+ '>0;
Correction:
SELECT TRIM(LEFT(address, LOCATE(' ', address))) FROM items WHERE `address` REGEXP '^[0-9]+ '>0;
Related
I have a simple sql query like below
SELECT REPLACE('TEST ABC XYZ NO JJJG', 'DEF', 'YES')
This will return same string as old one. I want here 'TEST ABC XYZ NO JJJG YES' as out put. If text exists, it should replace the text, else it should be appended at the end of the string.
Assuming column contains 'TEST ABC XYZ NO JJJG', then you could write a query like this:
SELECT
IF(
REPLACE(column, 'DEF', 'YES') = column, -- If the replacement yielded no changes
CONCAT(column, ' ', 'YES'), -- Simply append the text
REPLACE(column, 'DEF', 'YES') -- Otherwise returned the replaced result
) AS replaced_column
EDIT: To your comment (Simply change REPLACE() with INSTR()):
SELECT
IF(
INSTR(column, 'DEF') = column, -- If the column contains the text
REPLACE(column, 'DEF', 'YES'), -- Replace the result
CONCAT(column, ' ', 'YES') -- Otherwise append the text
) AS replaced_column
I have a table which consists of 5000 rows. I need SQL command that could update each row by removing all values in "(...)" if only one couple of () is found.
Basically, if I have a name in a row:
Name surname (extra info)
I need to remove "(extra info)" and leave only Name surname
But if there is no additional couple of ()
If there is a row
Name Surname(data) (extra info)
The script should not amend this name
In simple words, I need to update a name where is only one ( or ) symbol
Many thanks
can you try to find first '(' and substring to it ?
don't forget to use case for none ( in string
update userlogin
set fullname = case position('(' in FullName)
when 0
then fullname
else substring(Fullname,1,position('(' in FullName) - 1)
end
This is an implementation of my question. I used select to let me check the result before applying it to update request. The script shows a table of
3 columns: Id, Name, UpdatedName, where Name is what we have and UpdatedName what we will obtain
SELECT `Id`,`Name`,
(case when ((LENGTH(`Name`)-LENGTH(REPLACE(`Name`, '(', ''))) = 1)
THEN
SUBSTRING_INDEX(`Name`, '(', 1)
ELSE
'-'
END)
as updated_name,
FROM table
WHERE (LENGTH(`Name`)-LENGTH(REPLACE(`Name`, '(', ''))) = 1
LIMIT 0,1500
P.S. I used Id to allow me to amend values
SELECT CASE
WHEN fname = 'correct' THEN 'your condition'
WHEN sname = 'correct' THEN 'your second condition'
ELSE 'baz'
END AS fullname
FROM `databasetable`
or you can do as like also
CASE
WHEN action = 'update' THEN
UPDATE sometable SET column = value WHERE condition;
WHEN action = 'create' THEN
INSERT INTO sometable (column) VALUES (value);
END CASE
I have a User entity with firstName and lastName fields. How can I search full name in any order. For example search 'John Doe', 'Doe John', 'John D' and get relevant results
Here is my current query. But I can search by only one column at a time, i.e either get result matching to 'firstName' or 'lastName'. For Example, I get results for 'John' or 'Doe', but not for 'John Doe'
$likeName = $name.'%';
$qb = $this->createQueryBuilder('user')
->where($qb->expr()->orX(
$qb->expr()->like('user.firstName', "'$likeName'"),
$qb->expr()->like('user.lastName', "'$likeName'")
);
$qb->where($qb->expr()->andX(
$qb->expr()->orX(
$qb->expr()->like(
$qb->expr()->concat('employee_alias.firstName', $qb->expr()->concat($qb->expr()->literal(' '), 'employee_alias.lastName')),
$qb->expr()->literal($phrase.'%')
),
$qb->expr()->like(
$qb->expr()->concat('employee_alias.lastName', $qb->expr()->concat($qb->expr()->literal(' '), 'employee_alias.firstName')),
$qb->expr()->literal($phrase.'%')
)
))
);
try this:
$likeName = $name.'%';
$qb = $this->createQueryBuilder('user')
->where($qb->expr()->orX(
$qb->expr()->like($qb->expr()->concat($qb->expr()->concat('user.lastName', ' '), 'user.firstName')
, "'$likeName'"),
$qb->expr()->like($qb->expr()->concat($qb->expr()->concat('user.firstName', ' '), 'user.lastName')
, "'$likeName'")
));
Maybe try create repository class http://symfony.com/doc/current/doctrine/repository.html and something like this query:
$this->getEntityManager()
->createQueryBuilder('user')
->andWhere('user.firstName LIKE :firstName')
->andWhere('user.lastName LIKE :lastName')
->setParameter('firstName', $firstName)
->setParameter('lastName', $lastName)
->getQuery()
->getResult();
I have a table with fullname column. I want to make a query for finding a person via his last name but his last name is in the full name column.
Would it matter if it accidentally returned someone whose first name matched your query?
A simple query would be:
SELECT *
FROM TABLE
WHERE fullname LIKE '%insertlastname%'
If you want to define the last name as the name after the last space:
SELECT substring_index(fullname, ' ', -1) as lastname
FROM TABLE
WHERE lastname='insertlastname'
Two suboptimal answers, but some answers at least.
enter code here You can use this if you want to fetch by query:
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX( `fullname` , ' ', 2 ),' ',1) AS b,
SUBSTRING_INDEX(SUBSTRING_INDEX( `fullname` , ' ', -1 ),' ',2) AS c FROM `users` WHERE `userid`='1'
But you can also try by PHP to fetch last name. You just use explode function to fetch last name.
Exm:
$full_name = "row moin";
$pieces = explode(" ", $fullname);
echo $first_name = $pieces[0]; // row
echo $last_name = $pieces[1]; // moin
A simple answer for this is like this suppose we have a name
Charles Dickens
:
SELECT * FROM TABLE_NAME WHERE SUBSTRING_INDEX(FULLNAME,' ',-1) like '%Dickens';
I've seen a few of these questions asked but haven't spotted one that's helped!! I'm trying to select the first part of a postcode only, essentially ignoring anything after the space. the code I am using is
SUBSTRING(PostCode, 1 , CHARINDEX(' ', PostCode ) -1)
However, I am getting:
Invalid length parameter passed to the LEFT or SUBSTRING function
There's no nulls or blanks but there are some the only have the first part. Is this what causing the error and if so what's the work around?
That would only happen if PostCode is missing a space.
You could add conditionality such that all of PostCode is retrieved should a space not be found as follows
select SUBSTRING(PostCode, 1 ,
case when CHARINDEX(' ', PostCode ) = 0 then LEN(PostCode)
else CHARINDEX(' ', PostCode) -1 end)
CHARINDEX will return 0 if no spaces are in the string and then you look for a substring of -1 length.
You can tack a trailing space on to the end of the string to ensure there is always at least one space and avoid this problem.
SELECT SUBSTRING(PostCode, 1 , CHARINDEX(' ', PostCode + ' ' ) -1)
This is because the CHARINDEX-1 is returning a -ive value if the look-up for " " (space) is 0. The simplest solution would be to avoid '-ve' by adding
ABS(CHARINDEX(' ', PostCode ) -1))
which will return only +ive values for your length even if CHARINDEX(' ', PostCode ) -1) is a -ve value. Correct me if I'm wrong!
One of the selected column is null or empty.
Something else you can use is isnull:
isnull( SUBSTRING(PostCode, 1 , CHARINDEX(' ', PostCode ) -1), PostCode)