Separate Character from date on filename sql expression - mysql

I just wondering how to separate the character from the filename using SQL Expression for example Filename is IED_2015Nov020914.AF I want to separate or split the "Date" Which is "2015Nov02" in the where clause of SQL Expression.. I try to use function called "cross apply" But no luck it not working here is my working so far
where (patindex('%[0-9]%', File_name) > "2018-Nov-12"));
Thank you

The easiest way is to locate _ char and substring 9 characters.
CREATE TABLE #tab(filename VARCHAR(100));
INSERT INTO #tab VALUES ('IED_2015Nov020914.AF');
SELECT SUBSTRING(filename, CHARINDEX('_',filename)+1, 9) AS result
FROM #tab;
LiveDemo
Warning: This will work only if your data has constant format:
...._yyyyMMMdd....
If you need you can CAST result to date:
SELECT CAST(SUBSTRING(filename, CHARINDEX('_',filename)+1, 9) AS DATE) AS result
FROM #tab;
LiveDemo2

Related

How to convert IF or nested If statement like EXCEL to dynamic SQL Query in SQL server

I have came across one pretty problem in which I need to convert the nested if statement (like we used to write in excel) to dynamic SQL query.
I want to convert this
select 'IIF(A>B,A,IIF(B>C,B,C))'
to like this
select 'CASE WHEN A>B THEN A ELSE CASE WHEN B > C THEN B ELSE C END END'.
Why I am doing this I have formula column in the table user can insert formula in that and I need to convert this to SQL statement.
Do anyone has idea how can I do that?
The above is for only example I will be having 150 rows for different formula's entered by user.
Note : Here do not interpret IIF as SQL server IIF, it can be IF.
Assuming that A,B,C are numbers we can do something like this
SELECT
(
Select MAX(cols)
from
(VALUES (ColA), (ColB), (ColC))tbl(cols)
) as greatest
from yourtable

How to use Oracle sql with json_object to generate json in NiFi

I am trying to use Oracle JSON_OBJECT to write a query to generate a specific json format.
Here is a sample query (the real query is more complex):
SELECT JSON_OBJECT('name' value name)
FROM table_a
WHERE name = 'John'
The query is working in Oracle. I used ExecuteSQL processor and put this query in it. It shows
illegal character in JSON_OBJECT('name' value name)
Any suggestions?
You most likely need to give the column an alias. Nifi probably doesn't like having spaces, single quotes, or parenthesis in the column name.
select json_object('name' value NAME) as json_with_name
from table_a
where NAME = 'John';

Check if boolean value present in nested object

I have a JSON column and the data stored looks like:
{"results":{"made":true,"cooked":true,"eaten":true}}
{"results":{"made":true,"cooked":true,"eaten":false}}
{"results":{"made":true,"eaten":true,"a":false,"b":true,"c":false}, "more": {"ignore":true}}
I need to find all rows where 1+ values in $.results is false.
I tried using JSON_CONTAINS() but didn't find a way to get it to compare to a boolean JSON value, or to look at all values in $.results.
This needs to work with MySQL 5.7 but if it's not possible I will accept a MySQL 8+ answer.
I don't know the way for to search for a JSON true/false/null value using JSON functions - in practice these values are treated as string-type values during the search with JSON_CONTAINS, JSON_SEARCH, etc.
Use regular expression for the checking. Something like
SELECT id,
JSON_PRETTY(jsondata)
FROM test
WHERE jsondata REGEXP '"results": {[^}]+: false.*}';
DEMO
You could simply search the JSON_EXTRACT using the LIKE condition this way.
SELECT * FROM table1 WHERE JSON_EXTRACT(json_dict, '$.results') LIKE '%: false%';
Check this DB FIDDLE
An alternative to the pattern matching in other answers, is to extract all values from $.results and check each entry with a helper table with running numbers
SELECT DISTINCT v.id, v.json_value
FROM (
SELECT id, json_value, JSON_EXTRACT(json_value, '$.results.*') value_array
FROM json_table
) v
JOIN seq ON seq.n < JSON_LENGTH(v.value_array)
WHERE JSON_EXTRACT(v.value_array, CONCAT('$[', seq.n, ']')) = false
Here is the demo

Use comma separated list from one table as clause in query for another table

I have an events table with a field called breaks. This is populated with data in a comma separated format, i.e. 1,2,3 or 1 or 1,4,5 - the same format that MySQL's IN command uses.
I'd then like to run a query - on the slots table - to return all rows apart from those specified in events.breaks.
The query, theoretically, should be something like this:
SELECT
`slots`.`id` AS id,
RIGHT(`slots`.`time`, 8) AS `time`
FROM
`slots`, `event`
WHERE
`slots`.`id` NOT IN (`event`.`breaks`)
But that doesn't appear to work - if event.breaks is 4,5,7, the only row from the slots table that doesn't return is 4!
SQLFiddle here: http://sqlfiddle.com/#!2/913fe/1/0
You're passing a single field to the NOT IN () clause, not a subexpression. Think of it like this
(1, 2, 3)
is roughly the same as
SELECT 1
UNION
SELECT 2
UNION
SELECT 3;
as a subexpression. What you're doing instead is
('4,5,7')
which is roughly equivalent to
SELECT '4,5,7';
which in turn MySQL probably converted to a number for the comparison and the result is
NOT IN (4)
What you're actually trying to do isn't really supposed to be done like that. It'd be better if you added an AxB relation table so you can select several rows with the IDs you don't want.
Give this a try:
SELECT slots.id AS id, RIGHT(slots.time, 8) time
FROM slots, event
WHERE FIND_IN_SET(slots.id, event.breaks) = 0
This is how the FIND_IN_SET(str,strlist) function works:
Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by “,” characters. [...] Returns 0 if str is not in strlist or if strlist is the empty string.
Also note that IN (val1, val2, val3) is NOT the same as IN (val4) where val4 is a commma-separated string. The IN clause will compare by equality.
you may need a subselect to return the split string
... NOT IN (SELECT your_split_fnc(`event`.`breaks`) FROM `events`)
See answers here for a way to split strings in MySQL Can Mysql Split a column?
instr() MySQL function could be of help also
... INSTR(event.breaks,id) = 0
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_instr

How to use a mysql variable from one table as a subquery in another, but casting it as a string (from int)

This is the query i'm trying to run:
UPDATE files set refcount=
(
SELECT count(*)
FROM comments WHERE data=files.id
)
WHERE id=?;
The problem is, comments.data is a text column (for other reasons). So I need to cast files.id as a STRING instead of what it is (an INT), because otherwise the comments.data index won't be used.
For example, this query runs fine:
SELECT count(*) FROM comments WHERE data='1234';
But this one takes forever (because it cannot use the index, comments has 10M rows):
SELECT count(*) FROM comments WHERE data=1234;
Perhaps I need to use #vars or something? I tried putting the thing in quotes, but that uses the literal "files.id" i think.
UPDATE files set refcount=
(
SELECT count(*)
FROM comments WHERE data='files.id'
)
WHERE id=?;
All you have to do is to cast files.id into string before comparing it to data
something like this :
UPDATE files set refcount=
(
SELECT count(*)
FROM comments WHERE data=CAST(files.id AS vachar)
)
WHERE id=?;
Here's a link that show how you can use cast functions and operators in mysql.
UPDATED: It seems that for some reasons CAST is not working with varchar.Though char might do the trick (whih in case it doesn't as Timh said in the comments below) CONCAT can be used to convert other types to a varchar (when you concat othery types with a string it returns a string and concating with an empty string will act as some sort of conversion :) )