i really loves mysql virtual generated columns but am having a small issue with it am trying to make a virtual column from a varchar column where i extract the number as the following.
DB::statement('ALTER TABLE reservations ADD number_vc BIGINT AS (REVERSE(REVERSE(number) << 0)) ');
my issue is in this part REVERSE(REVERSE(number) << 0))
as if you tried now to run this part of sql in any sql editor as the following
SELECT REVERSE(REVERSE("A100") << 0)
it will generate the following
**What Am Trying To Achieve **
if the string was A100 after reverse i need 100 also the schema of the varchar not always one character it may be like this
A100 , AA100 , AB100
**Edit 2 :Am Afraid i did go production a week a go and here is the result **
With mySQL8+ you can do this :
SELECT REGEXP_REPLACE('A100', '\D', '');
It's basically replacing non digits characters with nothing using a regexp.
Related
I am new to the community so please bear with me. I am working on a sum function that will take the values of 3 columns (Exchange, Commission, Otherfees) and give me that total based on row. The datatypes for these 3 fields are VARCHAR. I started by using a CONVERT function and then addressed any NULLs. Please see the query below:
SELECT SUM(
(SELECT(SELECT
CONVERT(decimal(18,4), isnull(ExchangeFee,0)) AS decimal
FROM T_TABLE) as EXCHANGE_VALUE) +
(SELECT(
SELECT
CONVERT(decimal(18,4), isnull(Commission,0)) AS decimal
FROM T_TABLE) AS COMMISSION_VALUE) +
(SELECT(
SELECT
CONVERT(decimal(18,4), isnull(OtherFees,0)) AS decimal
FROM T_TABLE) AS OTHERFEES_VALUE) AS decimal) AS SUMMED_VALUE
When running this query, I get the message
'SUM' is not a recognized built-in function name.
Please let me know your thoughts.
You could start by using the correct data types for your fields.
ExchangeFee, Commission and OtherFees are all numeric, so why store them in a varchar?
If the values should never be NULL, and here these look like they probably probably shouldn't, set them as NOT NULL and default them to 0.
That said, mysql will convert strings to numbers in a numerical context so you only need to worry about any NULL values which COALESCE or IFNULL will deal with.
As for the query which you want to sum the rows, all of the data is coming from T_TABLE so the general structure of the query should be:
SELECT COALESCE(ExchangeFee,0) + COALESCE(Commission,0) + COALESCE(OtherFees,0) AS SUMMED_VALUE
FROM T_TABLE;
sorry for the repeated question. I already asked for help about this by the use of ORACLE database. But now I really wanted to know how can I split this using MySQL database
It is possible to split field values using a specific character? Here is my sample table
value
10uF
2K
1.0uF
200UF
I want it to split by this:
value capacitance/resistance
10 uF
2 K
1.0 uF
200 UF
Hope you can help me once again. Thanks! and more power!
You can use below code
create table temp
(value varchar(10)
);
insert into temp values ('10uF');
insert into temp values('2K');
SELECT value + 0 AS num
, REPLACE(value,value+0,'') AS unit
FROM temp
O/P
num letter
10 uF
2 K
The trick the query is using is to evaluate the column value in a numeric context, by adding a zero. MySQL will return the numeric value.
But this wont work in case of 10Uf10,2k3..
Hope all your data is digit + charachter
Fiddle for the same
Basically I have a table called 'Telephone' with over 100,000+ records.
In this database I have numbers such as:-
90123456789
91234567893
97126372319
Basically I want to Update the Telephone.DB and SET the numbers so it strips the first number only if it starts with the number '9', so the result would end up as follows:-
0123456789
1234567893
7126372319
Any idea how I can achieve this using MySQL?
Provided that the datatype is a string type, this should do it;
UPDATE numbers
SET number = SUBSTR(number, 2)
WHERE number LIKE '9%';
An SQLfiddle to test with.
As always, test yourself before running updates from random people on the Internet on your production database :)
Ok, this works for a VARCHAR column. You could always convert on the fly the value to varchar:
SELECT CONCAT(REPLACE(SUBSTR(PHONEFIELD,1,1),9,''), SUBSTR(PHONEFIELD,2)) AS PHONEWITHOUTNINE FROM TABLENAME
Check it out and tell me if it works.
I have a column named D1_VAL
The rows in this column have data that looks like this:
C:10
R:200
E:3
N:77
I'm trying to do a search on these values with a logical operator. I've tried this:
SELECT * FROM d1 WHERE CAST(`D1_VAL` AS UNSIGNED) > 5
It doesn't return any rows. I've also tried:
SELECT * FROM d1 WHERE (0 + `D1_VAL`) > 5
How would I do this properly?
This query will do,
SELECT * FROM d1 WHERE CAST(SUBSTRING(`D1_VAL`, 3) AS UNSIGNED) > 5
But its better to normalize the values like C:10 to two separate columns.
create table d1(
letter char(1),
number int,
// ... other columns
)
What is the comparison you are trying to do?
If compare the number (on the righ to colon) against 5, try something like this:
CAST(SUBSTRING(`D1_VAL`, INSTR(`D1_VAL`, `:`)) AS UNSIGNED) > 5
For more information on string functions, see here.
Also, I would suggest having a think about the database design - i.e. would the contents of this column be better off being stored in two columns - e.g. a char and an integer? That would be more efficient - requiring less disk space and allowing faster queries. If you have lots of rows, doing various string functions, casting etc. could make your queries run quite slowly.
I can't find a specific answer to this question online. If I take a tinyint field and cast it like so: '(DT_STR,2,1252)some_id' what happens with the single digit numbers? Does a 7 become '7' or '7 '?
I found some documentation on the MSDN site that the string is null terminated and truncates, but it didn't specifically say what happens to an int that is shorter than the cast.
Thanks!
In scenarios like this, I find it's easier to just test and observe.
OLE DB Source
Simple query that ensures we have the correct data type
SELECT cast(10 AS tinyint) AS src
UNION ALL SELECT cast(2 AS tinyint) AS src
Derived Column
Cast our tinyint to a string and add something at the end to view whether it's left or right padded.
(DT_STR,2,1252) [src] + "X"
LEN((DT_STR,2,1252)src + "X")
Results
I was actually surprised, I'd have expected it to be right padded but that's not case.
src Concated Length
10 10X 3
2 2X 2