How can I add values into MySQL if the value is either string or float? [closed] - mysql

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am parsing an excel file and adding the values im getting into a database. But one of the columns I am parsing can be either a float or a string. Like such: 0,45 or Contact Support. What is the best way for me to add this as it is, to my database? Or more how should I format my database column for this?

use varchar datatype(in MySQL) for the column you want to parse and you can save both the integer and character values.

I recommend you create a float type column and a varchar column. Write your parser so that valid float values are filled to the float column and the rest to the varchar one. This will allow you to both sort your database by the value of the float type column and run queries on fields that are missing that value: SELECT * FROM mytable WHERE myfloat IS NULL

first am with Kaivosukeltaja answer.
but if you can't do that, make a varchar column to save the strings or doubles.
BTW when you get data from a database all the data comes in string format then you can cast it to any type you want.
if you are using PHP:
$x = $row['string_double_column'];
if(is_numeric($x))
return doubleval($x);
else
return $x;

Related

How to extract the domain in a string by using regex? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I got a string representing users data.
What is the proper regex to extract domain in this string?
I know that I have to find all strings with 2 characters matching the condition that it comes after the last "." after a "#".
However I still failed to implement it.
import re
regex = r"#.+\.([a-z]{2}),"
your_string = ("001,Francisca,Dr Jhonaci,jhonadr#abc.com,32yearsold,120.238.225.0\n"
"002,Lavenda,Bocina,lavenboci#banck.ac.uk,50yearsold,121.186.221.182\n"
"003,Laura,Eglington,elinton#python.co.jp,26yearsold,36.55.173.63\n"
"004,Timo,Baum,timobaum#tennis.co.cn,22yearsold,121.121.110.10")
matches = re.finditer(regex, your_string, re.MULTILINE)
for match in matches:
result = match.group(1)
print(result)
The comma seems to be the delimiter in the string.
To not cross-matching a comma (to prevent matching too much), and also not cross-matching a second # char you can use a negated character class starting with [^
If the entry can also be at the end of the string, you can assert either a , or the end of the string.
#[^#,]*\.([A-Za-z]{2})(?=,|$)
Regex demo
import re
regex = r"#[^#,]*\.([A-Za-z]{2})(?=,|$)"
s = ("001,Francisca,Dr Jhonaci,jhonadr#abc.com,32yearsold,120.238.225.0\n"
"002,Lavenda,Bocina,lavenboci#banck.ac.uk,50yearsold,121.186.221.182\n"
"003,Laura,Eglington,elinton#python.co.jp,26yearsold,36.55.173.63\n"
"004,Timo,Baum,timobaum#tennis.co.cn,22yearsold,121.121.110.10")
print(re.findall(regex, s, re.M))
Output
['uk', 'jp', 'cn']
Use the comma after the email instead of the last point.
Using this regex
#.+\.(\w+)(?<!com),
the capturing group will contain the info that you want.

Mysql query to return regex match between two columns [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have two columns in a database I want to compare. One is email addresses and the other is username.
I'm looking to return results if the first part of username matches the first component of the email address (stuff before the # sign).
Sample data:
username emailaddress
badzzycshulzey9802 badzzycshulzey#gmail.com
trogleddg1919 trogleddg#gmail.com
Tried a variety of queries but I can't seem to get this one.... thanks!
You don't really need a regexp for this, strings functions can do it:
select *
from mytable
where username like concat(substr(emailaddress, 1, locate('#', emailaddress) - 1), '%')
locate('#', emailaddress) gives you the position of the arobas in the email address, and substr(emailaddress, 1, locate(...) - 1) extracts everything before that. Then, we can check if the username starts with that part of the email address using like with a wildcard on the right side.

What is the condition to check if a string contains any string of a column and vice versa? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I saw this question and tested the answers but noticed that executing SELECT ... WHERE column LIKE "%string%" OR string LIKE CONCAT("%", column, "%")
string LIKE CONCAT("%", column, "%") is not secure if the value of the column contains % and secondly if the column is null it returning true which is not correct since the column contains nothing.
You can just escape the percent signs, if any exist:
SELECT column1
FROM table
WHERE (
column2 LIKE "%string%"
OR string LIKE CONCAT("%", REPLACE(column2, '%', '|%'), "%") ESCAPE |
)
AND column2 IS NOT NULL;
The default escape character is a backslash but this is not ANSI compliant and can be a pain to work with if you're building a query in another language. So I use the LIKE ... ESCAPE syntax to specify my own escape.
CONCAT() returns NULL if any of its arguments are null, so if you're concerned about that, just check for it.

how to select the result using REGEXP in mysql,not contain a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
MySQL does not support the Negative Lookahead. How can I find the result not containing a string using REGEXP.
I am using 'NOT REGEXP' but the result is unexpected.
there is a 'Content' column in my table,i want to find the rows which the Content column contains '' label,but i still want some src to be excluded.
here is the sql:
Content REGEXP '.' AND Content NOT REGEXP '.(test.mywebsite1.com/|img.mywebsite.com/face/|test.mywebsite.com/phoneIcon.jpg).*'
but when the Content contain both and it works unexpected;
Test your REGEXP on a known set, get that working, and verify it is working.
Then add the NOT to get the boolean inverse.
Note that a MySQL boolean expression will return one of three possible values: TRUE, FALSE and NULL.
And note that NOT expr will also return one of three possible values: TRUE, FALSE and NULL.
When expr returns NULL, then NOT expr will also return NULL.
It's not really productive to attempt to provide any other assistance, absent an actual question and more details of what you are attempting to do.

how to put comma in a money data type such like Decimal [duplicate]

This question already has an answer here:
mysql select int as currency or convert int to currency format?
(1 answer)
Closed 9 years ago.
I have a column by the name of Currency in which I store monetary values. I have set the data type of this column to decimal. My question is can I have comma's after 3 digits when i am displaying the money on a webpage.
For example I would like to display it as: USD980,443,22.00
Currently its displaying like this: USD98044322.00
Is it possible, or are there any other ways to do that, because I have seen it in websites lots of times.
Typically you would want to do that in your server-side language so as to take the load off the DB, for example in PHP:
USD<?php echo number_format($dollarsAndCents, 2)?>
But if you're dead set on doing it in MySQL, here's how:
SELECT Format(dollarsAndCents, 2);