Easy way to update certain field in database that is upper case? - mysql

I am looking to update the field 'prefix' in my course table to just have the FIRST letter capitalized and not the whole prefix.
Is there any easy way to do this is SQL?? Sample Output could look like 'Aadm' in the database for 'prefix'.
My table looks like:
Sample rows look like:
I have SQL that looks like:
WHERE CONCAT(prefix,code) LIKE '%". $keywords . "%'");
Is it possible to user LOWER on prefix here?

select prefix,
concat(upper(substring(prefix,1,1)),substring(lower(prefix) from 2)) as initcap
from course
try it in select form before update your field
if you're looking for prefixes where all chars are upper case use a regexp
where binary(prefix) regexp '^[A-Z]+$'
EDIT. Update query
update course
set prefix =
concat(upper(substring(prefix,1,1)),substring(lower(prefix) from 2))

Related

Can you strip a part of a value from a cell?

I have a lot of entries in a table, at a specific column that contains values such as:
VVÎVV
VEVÎÎ
ÎÎEVÎ
ÎÎEVÎ
..and so on.
What I'd like to do is run a query that would update that specific column, on every row, stripping out the &, the circ and the ; it finds - while preserving whatever else is in there.
Or just replacing Î with I while also preserving the other things in there? Whichever is easier. If replacing, then Î would get replaced with `I`
Can/how do you do that in MySQL?
I'm currently reading about LIKE and Wildcard but I'm not seeing how to put a query like that together so far.
Using MySQL, InnoDB table name is ghix and column name is zuff
If I understand your question properly then you want to replace Î with I, right?
If so then you can run below query to replace all in that column. Here my table name is temp. You can use yours.
UPDATE temp SET column_name = REPLACE(column_name, 'Î', '|');
Thank you.
You can do that with REGEXP expression.
UPDATE table name SET columname = columname REGEXP 'regexp'
should do the trick

SQL Query like that: "some string" LIKE "%"+column+"%"

I've got a table full of substrings, and I need to select all substrings, which are in the search string.
Basically it would be the mirrored form of a where-condition:
WHERE "SearchString" LIKE "%"+ currentColumnValue +"%"
I know, that this is not possible, but for performance reasons I don't want to iterate every single database entry.
Maybe you have got an idea how to solve this kind of problem?
You can do
where 'SearchString' like concat('%', columnValue, '%');
This will be very slow as it would do a table scan and a like-compare on each line, but the result is correct.
http://sqlfiddle.com/#!2/e2b066/1
You can try this
$value = //your value
' select * from tb_name where col_name like %".$value."% '

MySQL LIKE for two words

What would be the right SQL statement so that when I search two words, like for example 'text field' in a text box, it will return all results that has 'text' and 'field' in it using the LIKE statement?
I cant find the right terms to make a search.
EDIT : If possible, I want to make it dynamic. Like if a user search 5 words, all 5 words would be in the Like statement. I am trying to achieve a statement
SELECT * FROM table WHERE search (LIKE %searchterm1%) OR (LIKE %searchterm2%) OR (LIKE %searchterm3%) ....
The the words are unordered use a standard logical conjunction (aka AND)
LIKE '%word1%' AND LIKE '%word2%'
If the words are ordered use an implicit conjunction in the search term itself
LIKE '%word1%word2%'
Modify the like wildcards (and quotes) as needed; also consider if a full-text search might be more appropriate.
The correct syntax is;
SELECT * FROM table WHERE (column1 LIKE '%text%' AND column1 LIKE '%field%')
To allow the user to input multiple words, firstly take a look at the problems of SQL injection, but assuming you're using PHP you can explode an input string and implode the resulting array, like this;
$values = explode(" ", $input); // delimiter is a space
$query = "SELECT * FROM table WHERE (column1 LIKE '%" . implode("%' AND column1 LIKE '%",$values) . "%')";
Hope this helps!

SQL LIKE wildcard space character

let's say I have a string in which the words are separated by 1 or more spaces and I want to use that string in and SQL LIKE condition. How do I make my SQL and tell it to match 1 or more blank space character in my string? Is there an SQL wildcard that I can use to do that?
Let me know
If you're just looking to get anything with atleast one blank / whitespace then you can do something like the following WHERE myField LIKE '% %'
If your dialect allows it, use SIMILAR TO, which allows for more flexible matching, including the normal regular expression quantifiers '?', '*' and '+', with grouping indicated by '()'
where entry SIMILAR TO 'hello +there'
will match 'hello there' with any number of spaces between the two words.
I guess in MySQL this is
where entry RLIKE 'hello +there'
I know this is late, but I never found a solution to this in relation to a LIKE question.
There is no way to do what you're wanting within a SQL LIKE. What you would have to do is use REGEXP and [[:space:]] inside your expression.
So to find one or more spaces between two words..
WHERE col REGEXP 'firstword[[:space:]]+secondword'
Another way to match for one or more space would be to use [].
It's done like this.
LIKE '%[ ]%'
This will match one or more spaces.
you can't do this using LIKE but what you can do, if you know this condition can exist in your data, is as you're inserting the data into the table, use regular expression matching to detect it up front and set a flag in a different column created for this purpose.
I just replace the whitespace chars with '%'. Lets say I want to do a LIKE query on a string like this 'I want to query this string with a LIKE'
#search_string = 'I want to query this string with a LIKE'
#search_string = ("%"+#search_string+"%").tr(" ", "%")
#my_query = MyTable.find(:all, :conditions => ['my_column LIKE ?', #search_string])
first I add the '%' to the start and end of string with
("%"+#search_string+"%")
and then replace other remaining whitespace chars with '%' like so
.tr(" ", "%")
http://www.techonthenet.com/sql/like.php
The patterns that you can choose from are:
% allows you to match any string of any length (including zero length)
_ allows you to match on a single character
I think that the question is not asking to match any spaces but to match two strings one a pattern and the other with wrong number of spaces because of typos.
In my case I have to check two fields from different tables one preloaded and the other filled typed by users so sometimes they don't respect 100% the pattern.
The solution was to use LIKE in the join
Select table1.field
from table1
left join table2 on table1.field like('%' + replace(table2.field,' ','%')+'%')
if the condition:
WHERE myField LIKE '%Hello world%'
doesn't work try
WHERE myField LIKE '%Hello%'
and
WHERE myField LIKE '%world%'
this approach is helpful in a few specific use cases, hope this helps.

MySql pattern matching

I need a MySQL pattern to match a number, followed by a question mark.
I need something like
... like '%[0-9]?%'
but I have no idea how to create this regular expression.
http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html does not help.
Thanks!
you could try this:
SELECT * FROM YourTable WHERE YourField REGEXP '[0-9]\\?'
That will return rows where YourField contains a number followed by a ? anywhere in the value.
If you want it to only match if the whole field is a number followed by a ?. I.e. 9? then you could use this regex instead:
^[0-9]\\?$
I guess you're looking for something like this:
select * from table
where field rlike '[0-9]\\?'
Remember to escape the question mark. Otherwise, it will make the number optional.
Source.