kdb - how generate HEX colour code as string or symbol - kdb

I would like to create a column on an in-memory table that generates a colour HEX code based on a person's name (another column). A quick google didn't give much so wondered if any pointers can be given here.
e.g
update colour: <some code and use username col as input> from table

In kdb+ you can run a function on a column via an update statement but there are slight differences depending on whether the function is vectorised or not. If vectorised:
update colour:{<some code>}[username] from table
update colour:someFunction[username] from table
If not vectorised then an iterator like each ' is required
update colour:{<some code>}'[username] from table
update colour:someFunction'[username] from table
This function will generate hex codes from the first 3 characters of a string.
q)hex:{a:i-16*j:(i:`int$3#x)div 16;"0123456789ABCDEF"raze(j-16*j div 16),'a}
q)hex"Hello"
"48656C"
q)update colour:hex'[username] from table

Related

Combining columns in Qlik

I have an Excel sheet that has two seperate columns of data that I need combined for my table in Qlik. I know it would be easy to combine the two in the Excel document, but because this is a data feed I would prefer not to do any extra work. One column has the first name and the other the last. Thank you.
I tried to concat but that did not work.
It sounds like what you're trying to achieve is string concatenation, where you'd combine two or more strings together. It'd be the same concept for fields, as long as their values can be coerced to a string type. In Qlik, this is done very simply by using the ampersand & operator. You can use this in three places:
Data Manager
If done in the Data Manager, you are creating a new calculated field. This can be done by editing the table that you're loading in from Excel, selecting Add field >> Calculated field, and then using an expression like this:
first_name & ' ' & last_name
What that expression is doing is taking the field first_name and concatenating it's values with a space ' ' and then concatenating the values of the last_name field.
So your new field, which I'm naming full_name here, would look like this:
first_name
last_name
full_name
Chris
Schaeuble
Chris Schaeuble
Stefan
Stoichev
Stefan Stoichev
Austin
Spivey
Austin Spivey
Here's what the data manager would look like:
Then after you load the data, you will have a new field with the combined names:
Data Load Editor
Doing this in the Data Load Editor will also result in a new field and is the exact same expression (see line 6):
Chart expression
The other option you have is to use this expression "on-the-fly" in a chart without creating a new column in the app data model like the first two options. Just use that same expression from above in a chart field expression and you'll get the same result:

How to create a custom function for Dynamic Data Masking

I'm testing Dynamic Data Masking and I discovered that SQL Server propose only 4 functions out of the box:
Function
Examples
Default
Example of alter syntax: ALTER COLUMN Gender ADD MASKED WITH (FUNCTION = 'default()')
Email
Example of alter syntax: ALTER COLUMN Email ADD MASKED WITH (FUNCTION = 'email()')
Random
Example of alter syntax: ALTER COLUMN [Month] ADD MASKED WITH (FUNCTION = 'random(1, 12)')
Custom String
Example of alter syntax: ALTER COLUMN [Phone Number] ADD MASKED WITH (FUNCTION = 'partial(1,"XXXXXXX",0)')
I would like to use the last option, Custom String, because I need to:
Keep 3 prefix number
Shuffle 3 numbers in the middle
Keep 3 last numbers
So this phone number 123-456-789 will become 123-039-789
My first approach is to nest two functions together this way:
ALTER TABLE [Person].[PersonPhone]
ALTER COLUMN [PhoneNumber] NVARCHAR(25) MASKED
WITH (FUNCTION = 'partial(4,"' + (FUNCTION = 'random(100,999)') + '",4)');
How Can I execute a function inside a function?
Or is there any other option to create a custom MASK?
There is no option to combine two data masking functions in SQL Server (2016 or 2019).
I think you want to shuffle (instead of mask) the middle portion because you want the phone numbers to look like they have not been masked, but I don't think this is a good idea. It's better to mask them with X-s, so it is obvious what has been masked. If you have a validation rule somewhere else that says there can be no letters in the phone number, you can mask the middle-portion with 0-s, by using MASKED WITH (FUNCTION = 'partial(4,"000",4)').
The alternative would be not to use Dynamic Data Masking, but a custom view (or a computed column), where you can call any T-SQL function, for example:
LEFT(PhoneNumber,4)+REPLACE(STR(ABS(CHECKSUM(NEWID()))%1000,3),' ','0')+RIGHT(PhoneNumber,4)

replacing characters in a CLOB column (db2)

I have a CLOB(2000000) field in a db2 (v10) database, and I would like to run a simple UPDATE query on it to replace each occurances of "foo" to "baaz".
Since the contents of the field is more then 32k, I get the following error:
"{some char data from field}" is too long.. SQLCODE=-433, SQLSTATE=22001
How can I replace the values?
UPDATE:
The query was the following (changed UPDATE into SELECT for easier testing):
SELECT REPLACE(my_clob_column, 'foo', 'baaz') FROM my_table WHERE id = 10726
UPDATE 2
As mustaccio pointed out, REPLACE does not work on CLOB fields (or at least not without doing a cast to VARCHAR on the data entered - which in my case is not possible since the size of the data is more than 32k) - the question is about finding an alternative way to acchive the REPLACE functionallity for CLOB fields.
Thanks,
krisy
Finally, since I have found no way to this by an SQL query, I ended up exporting the table, editing its lob content in Notepad++, and importing the table back again.
Not sure if this applies to your case: There are 2 different REPLACE functions offered by DB2, SYSIBM.REPLACE and SYSFUN.REPLACE. The version of REPLACE in SYSFUN accepts CLOBs and supports values up to 1 MByte. In case your values are longer than you would need to write your own (SQL-based?) function.
BTW: You can check function resolution by executing "values(current path)"

Reading through PostgreSQL ranked results

I'm new to PostgreSQL and I am working on a function to return the word locations for a searched word.
I want to first narrow down the text fields the search has to go though to make sure it is a relevant result from the database.
My table name is 'testing' then the text field column is called 'context' and the line number where it is located is called 'line_number'. Where the context text is associated with a specific line_number.
Right now my ranking code looks like this:
select line_number into lineLocation
from (
SELECT
testing.line_number,
ts_rank_cd(to_tsvector('english', testing.context),
to_tsquery('Cats & Dogs & Kids')) AS score
FROM Testing
) ranking
WHERE score >0
ORDER BY score DESC;
Return QUERY select * from lineLocation;
When I try to print out lineLocation as a return query, it works in reporting the new ranked line numbers 22,19,21,20,17,13 each returned in their own column.
My problem now is that I want to search each of those lines (22 ... 13) for a key word like "dog" and return its position
Obtaining the text for that by using:
select context into sample from testing
where testing.line_number = lineLocation;
If I try to just decrement the lineLocation in a loop like lineLocation -i
It goes out of order, and will eventually search context that is not relevant.
Is there any type of 'read next line' function I could use?
I am looking for a way to loop through the ranked result line numbers
EDIT I then go on to use a for loop where I want it to read through all of the rows of text in the column context from the ranked results
The problem I am having with this is that it only reads the first row of text in the column 'context' and I need it to look at all of the rows that are returned by the ranked search
Ended up creating a ranking function of its own, and inserting the results of that text search into another table with a serial increment column.
filled the values of the new table (ranked_results) with this code:
INSERT INTO ranked_results(sentence) VALUES (columnRanking());
I also had to create a function to delete/reset the columns in the new table upon insertion of more lines.
TRUNCATE table ranked_results RESTART IDENTITY;

Pick another column value depending on column value (postgresql)

In my frontend application I have a function that is called pick(VALUE,'col1','col2','col3'). If the VALUE is 2 the value in col2 should be picked.
This is very handsome for replacing long code using "case when", "switch case" or "if else" calculations.
I have tried to find a similar function in Postgres, but no luck so far. Seen some function array() and values() mentioned, but cannot find the correct syntax.
The goal is to set an return on of three column values depending on first column value.
Pseudo code (not working):
Select status values(column1,column2,column3)from code
I know I can do this by using "case-when-then-else-end", but I am looking for a shorter way to achieve the same thing.
Jsfiddle showing the principe. But I only want to pick ONE value depending on type:
http://sqlfiddle.com/#!15/e0b41/10
You can create an array of values from pr_* columns, then pick one of them in this way:
(array[prl_1,prl_2,prl_3])[code_type]
Here is a simple demo: http://sqlfiddle.com/#!15/e0b41/23
select *,
(array[prl_1,prl_2,prl_3])[code_type]
from code
left join prl on prl_id =1