im encountering the following issue i have a couple of strings in SQL and try to get just a little part off of it and i cant get it to work.
Examples of my strings:
{"#odata.type":"#make.this.short","Id":0,"Value":"Stackoverflow"}
[{"#odata.type":"#Mmake.this.short","Id":1,"Value":"Stackoverflow1"},{"#odata.type":"#make.this.short","Id":2,"Value":"Stackoverflow2"}]
{"#odata.type":"#make.this.short","Claims":"i:0#.f|membership|name#email.nl","DisplayName":"Lastname F. (Firstname)","Email":"Name#email.nl","Picture":"https://picture.com/","Department":null,"JobTitle":null}
Now im trying to write a stored procedure and get the following values back:
Stackoverflow
Stackoverflow1,Stackoverflow2
and name#email.nl
I tried working with SUBSTRING, LEN, RIGHT, CHARINDEX and REVERSE but i cant seem to get what i need. The value of Value: can ofcourse be of different lenghts for each record.
My idea was to count the total string (len string)
then get the first " on the right and subtract that from the total string.
then i would try and get the second " and subtract that from the first. whats left is the value inside and i could use a substring.
But that would only work for the first one, the others are quite different and somehow i cant even get the first to do what i like. (i cant seem to get the 2nd " on the right)
i hope this is enough info so you could point me in the right direction to fix this problem.
Thankyou
As you are on SQL Server 2017 you should be using the JSON functions for this
SELECT JSON_VALUE('{"#odata.type":"#make.this.short","Id":0,"Value":"Stackoverflow"}','$.Value')
SELECT JSON_VALUE('{"#odata.type":"#make.this.short","Claims":"i:0#.f|membership|name#email.nl","DisplayName":"Lastname F. (Firstname)","Email":"Name#email.nl","Picture":"https://picture.com/","Department":null,"JobTitle":null}','$.Email')
SELECT STRING_AGG(JSON_VALUE(value,'$.Value') ,',')
FROM OPENJSON('[{"#odata.type":"#Mmake.this.short","Id":1,"Value":"Stackoverflow1"},{"#odata.type":"#make.this.short","Id":2,"Value":"Stackoverflow2"}]','$')
Related
Looking to capture a customer code that appears after a hyphen and before the .file_extension ...
Example: DWL-202_EJJFT_Transactions-EOTTFFS001.csv
In this case, I want to capture EOTTFFS001 as my account code.
Thus far I have tried working with RIGHT but since our customers have different length codes, sometimes I end up with -DJTSM001.csv because, in this case, the customer had a five-letter code. This approach also does not remove CSV. I have also tried to nest a RIGHT statement inside of another RIGHT statement but that does not seem to work.
My goal is to use REGEXP_SUBSTR.
I think you just want the non-hyphenated string just before the last period:
select regexp_substr(col, '-([^-]+)[.][^.-]+$', 1, 1, 'e')
Throwing a split_part based alternative in there
select split_part(replace(col,'-','.'),'.',-2) -- -2 gets you the second last item
This seems like a very simple question, however, I am still not getting rid of the Data Type Mismatch. Scenario:
-> Excel File link in as table [tbl_Mast_CC_List], I convert the possible Cost Center Numbers into Values for safety via query, there are NO text variables in the Cost Center or preceding 000's, next arrow
-> qry_CC_Clean is CostCenter:Val([tbl_Mast_CC_List.CostCenter])
-> then I create the Unmatched Query, here is the SQL:
SELECT
qry_CC_S1_Clean_F2F_Alloc.DataName
, qry_CC_S1_Clean_F2F_Alloc.Year
, qry_CC_S1_Clean_F2F_Alloc.CostCenter
FROM qry_CC_S1_Clean_F2F_Alloc
LEFT JOIN qry_CC_S1_Clean_Mast_CC_List
ON qry_CC_S1_Clean_F2F_Alloc.CostCenter = qry_CC_S1_Clean_Mast_CC_List.CostCenter
WHERE (((qry_CC_S1_Clean_Mast_CC_List.CostCenter) Is Null))
ORDER BY qry_CC_S1_Clean_F2F_Alloc.CostCenter;
The only time I can get it to work is if I make table of the query and I don't really want to do that. Any suggestions would be greatly appreciated because I have to run this unmatched query against numerous tables to make sure the company is not missing any cost centers rolling through. Thank you!
Your problem is likely due to using the val() function and trying to test it against nulls. My understanding is that val() doesn't return nulls, it returns 0 when it can't find anything. You might be better off running the conversion in the opposite direction, i.e. using CStr() on the numeric CostCenter field and comparing that to the text data from Excel.
Alternately you could change the Excel field itself to a number format instead of text.
In SQL Server, I am trying to select some of the records using a string which has space so I trim and use but something is wrong please correct me where something is missed by me.
SELECT * FROM projects where str_id=ltrim(rtrim(' artf130 ')) --- No rows selected
SELECT * FROM projects where str_id='artf130' -- one row selected
Update: I copied the first line from google spread sheet.
Maybe that was my bad. People keep helping.
I think my comment was enough, cause I linked to a very similar problem, which got a answer. So here for everyone:
You can see the answer to that question here.
I am trying to get the second value in field to populate a column in a query.
Example:
I was able to accomplish this using MS Access and the SQL statement looks like this:
SELECT stone_schedules.mach_equip_id,
stone_schedules.timeline,
IIf(InStr(1,Mid([timeline],InStr(1,[timeline],",")+1),",")=0,0,Mid(Mid([timeline],InStr(1,[timeline],",")+1),1,InStr(1,Mid([timeline],InStr(1,[timeline],",")+1),",")-1)) AS NextSchedEntry_id
FROM stone_schedules
WHERE (((stone_schedules.active)<>"0"));
The problem with running this in Access and not on the server is that it runs too slow and server side is able to run this a lot quicker. This is what I have so far server side in pgAdmin:
SELECT
schedules.mach_equip_id,
schedules.timeline,
--select function where I need help
FROM
stone.schedules
WHERE
schedules.active = true;
Thanks
Well I was able to find an answer that is easier than the solution from ms access and my guess is probably less expensive.
When values are separated like that and inside the curly braces, it is an array.
I was able to select the 2nd value of the array by using the following sql statement.
SELECT
schedules.mach_equip_id,
schedules.timeline,
schedules.timeline[2] -- using brackets made it possible to select the 2nd value
FROM
stone.schedules
WHERE
schedules.active = true;
That was easier than I thought it would be.
I currently have a table with a lot of ASCII code scattered through it. I'm using the REPLACE operator to remove it but before I remove it I need to find the total of each different piece of ASCII code.
My current code is almost working correctly but I run into an issue when ASCII code is right next to each other.... eg. "48% of the registered voters, NOT 48% of citizens ==>> BIG ! HUGE DIFFERENCE."....My code below is only counting the ASCII code once not twice. Any help to get around this issue would be appreciated. If you need any more info please ask.
SELECT comments, COUNT(*) AS total FROM AE
WHERE comments like '%>%'
GROUP BY comments
If you want to count the number of times that a string appears:
SELECT comments,
(length(replace(comments, '>' , 'X>')) - length(comments)) as NumOccurrences
FROM AE
WHERE comments like '%>%' ;
I'm not sure that '>' is the string you really want to search for. But the idea is pretty simple. Replace the string with a string one character longer and then use length() to count the frequency within the comment.