Array stored as String - How to extract pieces of it? - sql

I got a problem with a specific database where some tags from a user profile are stored as a String data type, and so far I have not figured out how to fetch data from this one.
Example String:
{"watch_intro": "Gifts from my family.", "favorite_brands_styles": ["Fossil","Tudor","Omega"]}
I am looking to query into this field and get something like below:
'Fossil, Tudor, Omega'
Any help would be appreciated, thanks.

Nevermind, found the answer:
array_to_string(PARSE_JSON(EXTRA_INFO):"favorite_brands_styles",', ') AS fav_brands

Related

JSON EXTRACT IN BIG QUERY

I Currently have a column in json format with multiple items.
The struct is like the following:
phones": [{"phone": "11111111", "type": "CELLPHONE"}, {"phone":
"222222222", "type":"CELLPHONE"}, {"phone": "99999999", "type":
"CELLPHONE"}]
I tried:
json_extract(Contacts,'$.phones.phone') as phone_number but it only extracts the first one.
JSON_EXTRACT_ARRAY(Contacts,'$.phones') as phone_contacts gives me an array, But Im getting error trying to unnest it.
Does anybody know any approach to solve this problems?
Thanks in Advance.
Consider below
select json_value(phone_contact, '$.phone') phone,
json_value(phone_contact, '$.type') type
from your_table,
unnest(json_extract_array(Contacts,'$.phones')) phone_contact
if applied to sample data in your question - output is

laravel 5.2 how to use the count query with where query

I have tried every where all examples I came across but still have the same problem error 'htmlentities() expects parameter 1 to be string, array given'
I need to select count from the database and would like to use the raw select as i will have more flexibility on it but any point towards the right direction will be appreciated here is what i have been doing'
$totalOpen = DB::table('dam')
->select(array('dam.*', DB::raw('COUNT(dam.mivisjobid) as followers')))
->join('miviswf','miviswf.mivisid','=','dam.mivisjobid')
->whereRaw( 'miviswf.mivisid=dam.mivisjobid')
->whereIn('miviswf.Status', $inputIds) // pass an array
->orderBy('miviswf.datetimesubmitted', 'ASC');'
and i get this error 'htmlentities() expects parameter 1 to be string, object given (View: '
Try with this, don't use array inside the select method
->select('dam.*', DB::raw('COUNT(dam.mivisjobid) as followers'))
for reference:
https://laravel.com/docs/5.2/queries#selects
This worked fine
DB::table(' dam')
->count();

Selecting all info from nodes with the same name

I'm a total newbie when it comes to xml stuff.
So far I have this piece of xml that I want to extract info from, but all the node names are the same (so it just grabs one of them, unless stated otherwise).
It looks something like this:
<DocumentElement>
<Screening>
<ScreeningID>2</ScreeningID>
<ScreeningDate>2011-09-13T00:00:00-04:00</ScreeningDate>
<ScreeningResult>1</ScreeningResult>
<ScreeningResultText>Negative</ScreeningResultText>
<TextResult>0</TextResult>
<TextResultText>Not Tested</TextResultText>
<PageNumber>0</PageNumber>
<AddedDate>2015-05-03T16:06:41.71774-04:00</AddedDate>
<UpdateDate>2015-05-03T16:06:41.71774-04:00</UpdateDate>
</Screening>
<Screening>
<ScreeningID>3</ScreeningID>
<ScreeningDate>2011-09-13T00:00:00-04:00</ScreeningDate>
<ScreeningResult>1</ScreeningResult>
<ScreeningResultText>Negative</ScreeningResultText>
<TextResult>1</TextResult>
<TextResultText>Negative</TextResultText>
<PageNumber>9</PageNumber>
<AddedDate>2015-05-03T16:25:21.2904988-04:00</AddedDate>
<UpdateDate>2015-05-03T16:25:21.2904988-04:00</UpdateDate>
</Screening>
And I'm currently using this kind of snippet to extract info from the TextResult area
Select
answer.value('(/DocumentElement/Screening/TextResult)[1]','int')
From
Answers
However, that only grabs the first bit of info, I know that if I write something like this, it'll get me the second bit of info but on another column: answer.value('(/DocumentElement/Screening[2]/textResult)[1]','int')
I have two issues with this: 1. There isn't necessarily going to be only 2 nodes with the same name - it could go on infinitely. And 2. I would like all the info to be gathered into only one column.
Any help would be appreciated!
You can try this way :
SELECT
X.value('.','int') as 'TextResult'
FROM Answers as 'a'
CROSS APPLY a.answer.nodes('/DocumentElement/Screening/TextResult') as answers(X)
SQL Fiddle
I understand your meaning is: get all TextResult in your xml document. If so, you can try this:
string xml = #"<DocumentElement>
<Screening>
<ScreeningID>2</ScreeningID>
<ScreeningDate>2011-09-13T00:00:00-04:00</ScreeningDate>
<ScreeningResult>1</ScreeningResult>
<ScreeningResultText>Negative</ScreeningResultText>
<TextResult>0</TextResult>
<TextResultText>Not Tested</TextResultText>
<PageNumber>0</PageNumber>
<AddedDate>2015-05-03T16:06:41.71774-04:00</AddedDate>
<UpdateDate>2015-05-03T16:06:41.71774-04:00</UpdateDate>
</Screening>
<Screening>
<ScreeningID>3</ScreeningID>
<ScreeningDate>2011-09-13T00:00:00-04:00</ScreeningDate>
<ScreeningResult>1</ScreeningResult>
<ScreeningResultText>Negative</ScreeningResultText>
<TextResult>1</TextResult>
<TextResultText>Negative</TextResultText>
<PageNumber>9</PageNumber>
<AddedDate>2015-05-03T16:25:21.2904988-04:00</AddedDate>
<UpdateDate>2015-05-03T16:25:21.2904988-04:00</UpdateDate>
</Screening>
</DocumentElement>";
XElement xmlTree = XElement.Parse(xml);
IEnumerable<XElement> textResultList = from c in xmlTree.Descendants("TextResult")
select c;
foreach (var item in textResultList)
{
Console.WriteLine(item.Value);
}
Console.Read();
I hope this help

Unable to query using 4 conditions with WHERE clause

I am trying to query a database to obtain rows that matches 4 conditions.
The code I'm using is the following:
$result = db_query("SELECT * FROM transportesgeneral WHERE CiudadOrigen LIKE '$origen%' AND DepartamentoOrigen LIKE '$origendep' AND DepartamentoDestino LIKE '$destinodep' AND CiudadDestino LIKE '$destino%'");
But it is not working; Nevertheless, when I try it using only 3 conditions; ie:
$result = db_query("SELECT * FROM transportesgeneral WHERE CiudadOrigen LIKE '$origen%' AND DepartamentoOrigen LIKE '$origendep' AND DepartamentoDestino LIKE '$destinodep'");
It does work. Any idea what I'm doing wrong? Or is it not possible at all?
Thank you so much for your clarification smozgur.
Apparently this was the problem:
I was trying to query the database by using the word that contained a tittle "Petén" so I changed the database info and replaced that word to the same one without the tittle "Peten" and it worked.
Now, im not sure why it does not accept the tittle but that was the problem.
If you have any ideas on how I can use the tittle, I would appreciate that very much.

MOSS 2007: What is the source of "Directories"?

I'm trying to generate a new SharePoint list item directly using SQL server. What's stopping me is damn tp_DirName column. I have no ideas how to create this value.
Just for instance, I have selected all tasks from AllUserData, and there are possible values for the column: 'MySite/Lists/Task', 'Lists/Task' and even 'MySite/Lists/List2'.
MySite is the FullUrl value from Webs table. I can obtain it. But what about 'Lists/Task' and '/Lists/List2'? Where they are stored?
If try to avoid SQL context, I can formulate it the following way: what is the object, that has such attribute as '/Lists/List2'? Where can I set it up in GUI?
Just a FYI. It is VERY not supported to try and write directly to SharePoint's SQL Tables. You should really try and write something that utilizes the SharePoint Object Model. Writing to the SharePoint database directly mean Microsoft will not support the environment.
I've discovered, that [AllDocs] table, in contrast to its title, contains information about "directories", that can be used to generate tp_DirName. At least, I've found "List2" and "Task" entries in [AllDocs].[tp_Leaf] column.
So the solution looks like this -- concatenate the following 2 components to get tp_DirName:
[Webs].[FullUrl] for the web, containing list, containing item.
[AllDocs].[tp_Leaf] for the list, containing item.
Concatenate the following 2 components to get tp_Leaf for an item:
(Item count in the list) + 1
'_.000'
Regards,
Well, my previous answer was not very useful, though it had a key to the magic. Now I have a really useful one.
Whatever they said, M$ is very liberal to the MOSS DB hackers. At least they provide the following documents:
http://msdn.microsoft.com/en-us/library/dd304112(PROT.13).aspx
http://msdn.microsoft.com/en-us/library/dd358577(v=PROT.13).aspx
Read? Then, you know that all folders are listed in the [AllDocs] table with '1' in the 'Type' column.
Now, let's look at 'tp_RootFolder' column in AllLists. It looks like a folder id, doesn't it? So, just SELECT the single row from the [AllDocs], where Id = tp_RootFolder and Type = 1. Then, concatenate DirName + LeafName, and you will know, what the 'tp_DirName' value for a newly generated item in the list should be. That looks like a solid rock solution.
Now about tp_LeafName for the new items. Before, I wrote that the answer is (Item count in the list) + 1 + '_.000', that corresponds to the following query:
DECLARE #itemscount int;
SELECT #itemscount = COUNT(*) FROM [dbo].[AllUserData] WHERE [tp_ListId] = '...my list id...';
INSERT INTO [AllUserData] (tp_LeafName, ...) VALUES(CAST(#itemscount + 1 AS NVARCHAR(255)) + '_.000', ...)
Thus, I have to say I'm not sure that it works always. For items - yes, but for docs... I'll inquire into the question. Leave a comment if you want to read a report.
Hehe, there is a stored procedure named proc_AddListItem. I was almost right. MS people do the same, but instead of (count + 1) they use just... tp_ID :)
Anyway, now I know THE SINGLE RIGHT answer: I have to call proc_AddListItem.
UPDATE: Don't forget to present the data from the [AllUserData] table as a new item in [AllDocs] (just insert id and leafname, see how SP does it itself).