The library contains other useful functions which make it easier to use for our purpose. Check this out:
SELECT DISTINCT
[CustomTags]
,dbo.json_value([CustomTags],'0.TN') as [Tag], dbo.json_value([CustomTags],'0.TV') as [Val]
,dbo.json_value([CustomTags],'1.TN') as [Tag], dbo.json_value([CustomTags],'1.TV') as [Val]
FROM [dbo].[MenuItems]
where 1=1
and [CustomTags] != '[]' and [CustomTags] is not null
A very simple query using one of the functions gives us nice output:
Thanks for the help @QMcKay but think I will stick with the previous suggestion for now.
One issue though, your sample script only works properly is the Department tag is the only or lest record in the json.
If there are others after it it leaves a trailing "},{"
It seems to be the 10 in the sub string section.
Tried myself but not quite there, is there a way to dynamically add length of the tag value as well based on the " after it in the string?
Yes it could in it’s current simplicity. Need to do more processing. I was just showing proof of concept and how easy it was to use. It processes all records instead of only 1, and there isn’t a bunch extraneous code to get the same result as the first library that I tried.
There are more functions in the library as well, but I cannot find any docs on their use:
Thanks but isn’t that just then virtually the same as setting a 5 length? If shorter than 5 (giving the training "},{") you would then get the T or TN etc of a following tag.
Not that I dont appreciate your help.
Where would a charindex go it give a value base on the results from your last script?
Is that is defined AS Department can you then run expressions on that column?
I am not going down this path any further. I am going to concentrate on creating a method using the library for pulling json name-value pairs, then we can filter out pairs we don’t care about.
OK, I concede :-). I think we will look to try and use this with a fixed department name for now.
Thinking about it the main split from till would be Wet and Dry anyway which would be 3, can put anything else as ‘Sun’ (sundries)
Intend to have Room Pose (wet/dry) to go to seperate accounts from EPOS (non room post wet/dry) so that we can see easily on the PMS reports the sales coming from ‘over the counter’ and 'room ‘charges’ but this can easily be achieved using PMS Department tag of Wet or Dry and the scripts prefixing the department with ‘EPOS’ and ‘Room Charge’ as the room post and ‘non room charge’ posts are seperate scripts resulting in 4 accounts used by integration of;
EPOS Wet
EPOS Dry
Room Charge Wet
Room Charge Dry
I will then look at refining this at a later date once there is integrated JSON handling functions in SQL 2016.
Unless your new method gets to the point where it will easily give just a plane list of a single tags values
It already does, sort of. I was running it in such a way as to get distinct list of values for PMSdep. Need to do a little bit more to allow for data placement (determine where the tag is located in multi-tag strings).
First, surround all the query parts with double-quotes, instead of single-quotes. You need the single quotes within the SQL statement. This cuts down on the escaping you need to do.
Then you just need to escape the few double-quotes.
Hmm… I don’t think that will work with the declare statements in there like that either. You will need to terminate those with ;
@emre, the SQL works - it runs fine. It is just the JScript that continues to throw errors.
That error doesn’t say much, but the assumption is that the JScript is not being parsed properly.
EDIT: The followingworks, after save Logout/Login, etc:
@@dep
declare @tagName varchar(30) = 'HH'
declare @tagNameLen int = len(@tagName)
declare @validx int = @tagNameLen+len('","TV":"')
SELECT DISTINCT
replace(
substring([CustomTags],
charindex(@tagName,[CustomTags])+@validx
,10)
,'"}]','')
AS Department
FROM [MenuItems]
WHERE 1=1
AND [CustomTags] like '%{"TN":"'+@tagName+'","TV":"%"}%'
dep
function blah() {
var r = sql.Exec("@@dep");
return r[0];
}