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"}
Related
I want to get concatenated list of values from serialized string using MySQL query.The sample string is
a:4:{s:11:"customersId";i:381032;s:12:"commerceName";s:9:"amazonmws";s:9:"serviceId";i:32042;s:9:"trackings";a:2:{i:0;a:10:{s:12:"customers_id";i:381032;s:13:"commerce_name";s:9:"amazonmws";s:10:"service_id";i:32042;s:15:"commerce_ref_id";s:19:"112-1414980-3467405";s:12:"order_number";i:151434392;s:11:"tracking_no";s:15:"995365613134160";s:7:"carrier";s:3:"FDX";s:8:"order_id";i:151434392;s:7:"details";a:1:{i:0;a:2:{s:4:"wsku";s:12:"KZW4OC-F0015";s:3:"qty";s:9:"000000001";}}s:20:"commerce_account_key";N;}i:1;a:10:{s:12:"customers_id";i:381032;s:13:"commerce_name";s:9:"amazonmws";s:10:"service_id";i:32042;s:15:"commerce_ref_id";s:19:"002-5809777-9709040";s:12:"order_number";i:151536332;s:11:"tracking_no";s:15:"995365613134153";s:7:"carrier";s:3:"FDX";s:8:"order_id";i:151536332;s:7:"details";a:1:{i:0;a:2:{s:4:"wsku";s:12:"KZW4OC-F0011";s:3:"qty";s:9:"000000001";}}s:20:"commerce_account_key";N;}}}
I want the list of values next to key order_id in above string.
I have tried with substring_index.
Please find my sample query
select SUBSTRING_INDEX(substring_index('serialized_sting','"order_id";i:',-1),';s:7:',1)
I get only one value from string,but not all
I need to select a value from a column in the table as a name of the another column in mysql
for ex ::
select column AS (select column from table where id = 1) from table;
it give me a syntax error .. How can I use select statement inside AS Function
Actually I need to set a value from a column as a name to another column using AS Function in mysql
The answer is simple: It is not possible in SQL. Column aliases are constants.
You can't do this in a single query. All identifiers must be fixed in the query at the time it is parsed, which is before the query begins reading data. This includes column names and column aliases. The names or aliases of columns cannot be set at runtime based on data read during the query.
But you can do what you want in two queries.
Query to get the column alias name you want to use. This returns a string.
Use that string as you format the second query. Then the column alias will be fixed in that second query by the time you prepare it.
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
I'm having an Active Record in my rails application in which each row of the table MyTable contains:
stringArr --> an array of string
id
Now I want to query my database to pull all the records in which a particular queryString is present in strinArr of that record.
I know we can do MyTable.where(xyz:someXYZ) to pull all the records with xyz value as someXYZ?
But how can I do this query?
To fetch all record, which contains queryString in an array of records, you can do -
MyTable.where("column_name LIKE ?","'%queryString%'"})
This will do exact query string search means return all data, which column_name contains queryString.
You can use the sql like clause
MyTable.where("stringArr LIKE :keywords",{keywords: '%querystring%'})
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