I am using mysql I want first letter of firstname capital and remaining characters in lower case the query i am using is
select UPPER(LEFT(FirstName,1))+LOWER(SUBSTRING(FirstName,2,LENGTH(FirstName))) FirstName from colliers;
this gives answer 0, but it works perfectly in SQL server ..
You have to use concat(). "Plus sign" concatenation doesn't work in MySQL. You will probably end up with something like this :
select CONCAT(UPPER(LEFT(FirstName,1)), LOWER(SUBSTRING(FirstName,2,LENGTH(FirstName)))) FirstName from colliers;
By the way you don't need LENGTH(FirstName) in the SUBSTRING() function call. When the third parameter is omitted SUBSTRING() assume you want the rest of the string.
You have to use CONCAT(), instead of +
SELECT CONCAT(UPPER(LEFT(FirstName,1)),LOWER(SUBSTRING(FirstName,2,LENGTH(FirstName)))) FirstName from colliers
Just one more way to solve the problem!
I would use concat(), ucase()/upper(), lcase()/lower(), mid()/substring()
SELECT CONCAT (
upper(mid(Firstname, 1, 1))
,lower(mid(Firstname, 2))
)
FROM colliers;
Related
I have a small mysql database with a column which has format of a field as following:
x_1_1,
x_1_2,
x_1_2,
x_2_1,
x_2_12,
x_3_1,
x_3_2,
x_3_11,
I want to extra the data where it matches last '_1'. So if I run a query on above sample dataset, it would return
x_1_1,
x_2_1,
x_3_1,
This should not return x_2_12 or x_3_11.
I tried like '%_1' but it returns x_2_12 and x_3_11 as well.
Thank you!
A simple method is the right() function:
select t.*
from t
where right(field, 2) = '_1';
You can use like but you need to escape the _:
where field like '%$_1' escape '$'
Or use regular expressions:
where field regexp '_1$'
The underscore character has special significance in a LIKE clause. It acts as a wildcard and represent one single character. So you would have to escape it with a backslash:
LIKE '%\_1'
RIGHT does the job too, but it requires that you provide the proper length for the string being sought and is thus less flexible.
Duh, I found the answer.
Use RIGHT (col_name, 2) = '_1'
Thank you!
I have table like this
I want get those record which content Unit Separator
I have try many things but not getting result.I try with char(31) and 0x1f and many other ways but not getting desired result.This is my query which i try
SELECT * FROM `submissions_answers` WHERE `question_id`=90 AND `answer` like '%0x1f%'
How can i do this? Please help me..
Problem
The expression you tried won't work because answer LIKE '%0x1f%' is looking for a string with literally '0x1f' as part of it - it doesn't get converted to an ASCII code.
Solutions
Some alternatives to this part of the expression that ought to work are:-
answer LIKE CONCAT('%', 0x1F, '%')
answer REGEXP 0x1F
INSTR(answer, 0x1F) > 0
Further consideration
If none of these work then there may be a further possibility. Are you sure the character seen in the strings is actually 0x1F? I only ask because the first thing I tried was to paste in ␟ but it turns out MySQL see this as a decimal character code of 226 rather than 31. Not sure which client you are using but if the 0x1F character is in the string, it might not actually appear in the output.
Demo
Some tests demonstrating the points above: SQL Fiddle demo
You can use:
SELECT * FROM submissions_answers WHERE question_id=90 AND instr(answer,char(31))>0
The keyword here being the INSTR MySQL function, which you can read about here. This function returns the position of the first occurrence of substring (char(31)) in the string (answer).
Yet another way...
SELECT * FROM `submissions_answers`
WHERE `question_id`=90
AND HEX(`answer`) REGEXP '^(..)*1F'
Explanation of the regexp:
^ - start matching at the beginning (of answer)
(..)* -- match any number (*) of 2-byte things (..)
then match 1F, the hex for US.
You could convert the answer column into a HEX value, and then look for values containing that hex string.
SELECT * FROM `submissions_answers`
WHERE HEX(`answer`) LIKE '%E2909F%'
I am using mySQL to query a field which would be LastName. I am looking for any errors in the field such as any special characters or numbers. I am not terribly familiar with SQL so this has been a challenge so far. I have written simple statements with REGEXP but I have run into some issues the REGEXP i was using was:
SELECT LastName FROM `DB`.`PLANNAME` where LastName REGEXP '^([0-9])'
now this turned up results where numbers were the first character in the string and i realized that if anything was in the middle of the string that started with a letter this would not pick it out.
To be clear i just need to find the errors not write a code to clean them out.
Any help would be greatly appreciated
Thanks
Pete
Something like this should do it for you.
SELECT column FROM table WHERE column REGEXP '[^A-Za-z]'
This will return any rows where a character that is not a-z. You might want to add in and '. For O'briens and von lansing etc. Any characters you think are acceptable should go in the character class [], http://www.regular-expressions.info/charclass.html.
Demo: https://regex101.com/r/nC9cG7/1
Maybe you are looking for something like this:
SELECT LastName FROM `DB`.`PLANNAME` WHERE NOT LastName REGEXP '[A-Za-z0-9]';
Here is a documentation on this:
Table 12.9 String Regular Expression Operators
select replace(lastname,'%%',firstname) as new1 from names;
When I run this, lastname is returned. Why? I expect it to search names.lastname for everything (%% wildcard) and return names.firstname.
All the syntax I have reviewed suggest I am doing this right, it seems so simple...
Why?
The expression REPLACE(lastname,'%%',firstname) will return lastname, whenever lastname doesn't contain two contiguous percent sign characters. Why? Because that's the documented behavior of the REPLACE function.
The '%' is a wildcard when used with LIKE. It's not a wildcard in the REPLACE() function.
The expression in your question will search the value of lastname for occurrences of two contiguous percent sign characters, and replace those occurrences with the value in firstname.
For example:
SELECT REPLACE('fee%%fi%%fo','%%','-dah ') AS foo
foo
-------------------
fee-dah fi-dah fo
(I believe this answers the question you asked.)
What are you trying to achieve?
I'm pretty sure wildcards are not allowed in REPLACE, but the way you have written it suggests that you want to SELECT the lastname with all its characters replaced with the string firstname, which is the same as SELECT firstname.
If you need to change the lastname in the table, you will need to run an UPDATE:
UPDATE names SET lastname=firstname
If you simply want a concatenation of the two then SELECT CONCAT(lastname,' ', firstname) is your game.
Can someone please help. I have been searching and encountered/modified this code I am getting a 1 or 0 as a result. 1 if there is something between () and 0 if there is not. I am looking to find exactly what is between them not if there is something. So if I have a string in afield that looks like this: "ABC (989) Hello" currently I get 1 as my result I would like to get "989". Any help would be greatly appreciated.
select , OUTCNTCTNOTE regexp '[(]|\\[)]' as test
from trcalls.callcoding;
To complete the first answer, because the third parameter passed to substr is the length of the substring, we need to subtract the index of the opening parantheses, so:
substr(columnname,instr(columnname,"(") + 1, instr(columnname,")") - instr(columnname,"(") - 1)
should do the trick
select substr(columnname,instr(columnname,"(") + 1, instr(columnname,")")) as temp from mytable
something close, I tested this. Please see if this helps!
Mysql's regexes don't support capturing or replacing. They're purely for matching. You'd need to use regular string operations to do the actual extraction:
SELECT ...string stuff here...
FROM yourtable
WHERE OUTCNTCTNOTE regexp ....
If your strings are fairly 'regular' and you don't have to worry about multiple sets of brackets in any field, then using LOCATE() and SUBSTR() would do the trick.