How to update columns in a table for specific rows? - mysql

I want to update my columns for rows specified by WHERE command, but I want to update my field in a way that it extracts number part of the string from each specified field, multiplies that with a number (that I will specify) and give number output in all those specific fields extracted by WHERE command in that column.
For example, assume I want to update all my fields in a column which are like (5.6 AUD/1000, 4.5 AUD/1000, 9.7 AUD/1000), so I want to first identify fields ending with /1000 and update only those fields in the column by multiplying the number part of the string (which is 5.6, 4.5, 9.7) with any number (let's say 10). I want that other fields on the column remains unchanged.
SELECT * from sorted WHERE Column8 REGEXP '/1000$';
gives me all the specific fields that I wish to update. But I want to update them in the way I specified above, which is that I want to extract number part from the string and multiply that with a number and update those fields only.
I am able to extract all the fields with the condition I mentioned, I'm facing difficulty in update these fields in the column.
SELECT * from sorted WHERE Column8 REGEXP '/1000$';
SELECT CAST(Column8 AS UNSIGNED)*10 FROM sorted
wHERE
column8 REGEXP '/1000$';
The above code gives me required updated fields, but I want them reflected in my column.
I expect my output to be a column where only those fields ending with '/1000' should get updated in a way that the number part of the string is multiplied with 10.

I have casted the varchar field named string to decimal type and multiplied with static value 10 . I have checked in sql server.
DECLARE #temp TABLE
(
string NVARCHAR(50)
)
INSERT INTO #temp (string)
VALUES
('5.6 AUD/1000'),
('4.5 AUD/1000'),
('9.7 AUD/1000')
select cast(left(string, patindex('%[^0-9./]%', string) - 1) As decimal(18,2))* 10
from #temp

Related

Extra a string value and update the string value in Postgres SQL

There is a column in one of my tables which contains the value like
{"firstname":"aaa","secondname:"bbb","lastname":"ccc","age":"18","Address:"xxxxxxxxxxxxxx"}
I am in need of SQL query to extract the value secondname (here the value will be bbb)
and I also need a update query to set the second value as empty
the output will be like
{"firstname":"aaa","secondname:"","lastname":"ccc","age":"18","Address:"xxxxxxxxxxxxxx"}

Is it possible to query an integer range within a string field?

Say I have a simple mysql table containing a name field which is just a varchar. The name field contains a string of the following format. "channelname,unix_timestamp,unix_timestamp". e.g. "bbc1,123456789,123456889". I need to select all rows, where a channelname matches, and where a given timestamp falls within the range of the 2 timestamps. For example, given the timestamp 123456800, and the channelname 'bbc1' The above record should be selected.
How I would accomplish this is to first select all records with "name like 'bbc1,%' split out the two timestamp fields in the calling code, and filter the results there to those containing the given timestamp. Is there a better, more efficient way. My DB could have a very large number of records which match "name like 'bbc1,%'", and it's only expected to grow as time goes on.
I unfortunately don't have the ability to alter the table to add the two timestamp fields, the only thing I have to go on is that single name field. It's also possible that the name field may contain some arbitrary string not of the given format for some records, however all records which start with the given channel name should match this format.

How would you add a number to every piece of data in a column in SQL?

So if I have one column of data called credit_debt that has ten different numbers in it, and I wanted to add 100 to each of those, how would I do that? I know that I could do it manually one by one, but how would I do it all in one command?
To update all of the rows in a table, we can issue an UPDATE statement without a WHERE clause.
We can reference the current values stored in columns in the UPDATE statement.
Assuming that credit_debt column is a numeric datatype (e.g. INT, DECIMAL, DOUBLE, et al.)
UPDATE mytable
SET credit_debt = credit_debt + 100
;
Before running an UPDATE like that, I always ensure that I have a good backup, and a way to restore to the current state. And I test my expressions in a SELECT, so I won't have to do a restore. Before running that UPDATE, I'd run a SELECT like this:
SELECT credit_debt
, credit_debt + 100 AS _new_credit_debt
FROM mytable
ORDER BY ...
;
And the verify that the value returned for _new_credit_debt is the value I want to assign to the column. (We can add whatever other expressions to the SELECT list we want, so we can verify the results.

LPAD with leading zero

I have table with invoice numbers. Guidelines say that numbers should have 6 or more digits. First of all tried to do:
UPDATE t1 SET NUMER=CONCAT('00000',NUMER) WHERE LENGTH(NUMER)=1;
UPDATE t1 SET NUMER=CONCAT('0000',NUMER) WHERE LENGTH(NUMER)=2;
UPDATE t1 SET NUMER=CONCAT('000',NUMER) WHERE LENGTH(NUMER)=3;
UPDATE t1 SET NUMER=CONCAT('00',NUMER) WHERE LENGTH(NUMER)=4;
UPDATE t1 SET NUMER=CONCAT('0',NUMER) WHERE LENGTH(NUMER)=5;
but that isn't efficient, and even pretty. I tried LPAD function, but then came problem because function :
UPDATE t1 SET NUMER=LPAD(NUMER,6,'0') WHERE CHAR_LENGTH(NUMER)<=6 ;
returns ZERO rows affected. Also googled and they say that putting zero into quotes will solve problem, but didn't, any help ? It's daily import.
EDIT:
Column NUMER is INT(19) and contain already data like :
NUMER
----------
1203
12303
123403
1234503
...
(it's filled with data with different length from 3 to 7 digits by now)
I think you should consider that the guidelines you read apply to how an invoice should be displayed, and not how it should be stored in the database.
When a number is stored as an INT, it's a pure number. If you add zeros in front and store it again, it is still the same number.
You could select the NUMER field as follows, or create a view for that table:
SELECT LPAD(NUMER,6,'0') AS NUMER
FROM ...
Or, rather than changing the data when you select it from the database, consider padding the number with zeros when you display it, and only when you display it.
I think your requirement for historical data to stay the same is a moot point. Even for historical data, an invoice numbered 001203 is the same as an invoice numbered 1203.
However, if you absolutely must do it the way you describe, then converting to a VARCHAR field may work. Converted historical data can be stored as-is, and any new entries could be padded to the required number of zeros. But I do not recommend that.
UPDATE t1 SET NUMER=LPAD(NUMER,6,'0') WHERE CHAR_LENGTH(NUMER)<=6 ; will not do what you expect since the NUMER field is an int. It will create the string '001234' from the int 1234 and then cast it back into 1234 - that is why there is no change.
Change NUMER to type int(6) zerofill and MySQL will pad it for you each time you read it.
If you really want zeros stored in the database, you have to change the type to CHAR/VARCHAR, then your LPAD update statement will work.
The field in the table is an int column so it just stores a number. There's no way to pad out the data in the table. 1 == 001 == 000000000001. This is the same number.
You should do the padding at the application level (the system that pulls the data out of the table). What happens when the order number goes above 999999? You would then have to update all the data in the table to add an extra 0. This kind of thing should not be done at the database level.
You could also select the data out with an LPAD:
SELECT LPAD(NUMER,6,'0'), [other_columns] FROM t1;
Alternative, As CBroe mentioned you could change the datatype to be INT(6) ZEROFILL so that it displays correctly but this will have to be modified if it goes above 999999 as mentioned above..

Computed Column Specification Based on Primary Key

I have a field that I want to compute based on a string and the ID generated when a record is inserted. Basically when a record is save with ID = 1, I need the computed field to read 'string_1' and so on. I am trying this is my formula (('PV'+'_')+ID) where PV is the string and ID is the primary key field in the same row as the data inserted but I'm getting a formula error. If I add quotes around ID then I just get PV_ID which is wrong. Any idea how I can reference the ID field in my formula and fetch the value?
here is my table row structure(ID,Computedfield,data1,data2). i need computedfield to have the value of the ID field concatenated with a string. any help appreciated
EDIT
Using SQL SERVER 2008 R2 Standard
Your question isn't totally clear on whether that prefix string is a string literal, or the contents of another column.
If it's a literal, you should be able to say:
ALTER TABLE dbo.YourTable
ADD ComputedColumn AS 'PV_' + CAST(ID AS VARCHAR(10)) PERSISTED
If it's a string contained in another column, you should be able to define it like this
ALTER TABLE dbo.YourTable
ADD ComputedColumn AS PV + '_' + CAST(ID AS VARCHAR(10)) PERSISTED
assuming PV is the column (of type VARCHAR) containing the prefix string.
The main point is: since you're mixing a literal string, and an INT value, you need to CAST the INT to a string first before being able to concatenate those two.
Use formula:
('PV_'+CAST(ID as varchar))
if you want to keep the resulting value - add the PERSISTED in the end