Sqlite3 Case statement weirdness - sqlite

I have the following case statement on a TEXT file and I'm trying to get it to say '' instead of none.
CASE CustomField1
When 'None'
then ''
Else CAST(CustomField1 as INT)
End ReceiptLoc
Can anyone see anything wrong with this. If the column is NULL then put nothing in the column or empty string is fine. I've tried replacing 'None' with NULL and NaN. What am I missing?
I'm more of a SQL Server person so This is a little different for me.

You could use searched CASE expression:
CASE WHEN CustomField1 IS NULL THEN ''
WHEN CustomField1 = 'None' THEN ''
Else CAST(CustomField1 AS INT)
End ReceiptLoc
db<>fiddle demo

Related

MySql mariaDB CREATE FUNCTION DELIMITER doesnt work. has error near ''

im going insane. i dont know whats wrong with this code i see nothing wrong in it. but it kept reading thee same error each time i put the $$ after the "END". please help guys...
here is my code
DELIMITER $$
CREATE FUNCTION fungsi1 (a int)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE nilaiasal INT;
DECLARE teks VARCHAR(50);
SET nilaiasal = a * a;
IF nilaiasal > 100 THEN
SET teks = 'LEBIH DARI SERATUS';
ELSEIF nilaiasal < 100 THEN
SET teks = 'KURANG DARI SERATUS';
INSERT INTO table1 (dataone,datatwo) VALUES(a,teks);
SELECT * FROM table1;
RETURN (nilaiasal);
END $$
DELIMITER ;
And the error is this
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 15
Please help me. I've looked up create functions error and there hasnt been anyone that has a problem near ''.
You might want to consider using a stored procedue instead of a function if you want to return a result set.
You are missing END IF; as Ergest Basha suggested and you are trying to return a result set from a function which is not allowed.

SQLite "modified" trigger which only only updates modified field if values have actually changed

I'm working on a trigger for SQLite which will update the "modified" field if & only if the data in all the columns under consideration has actually changed.
I've gotten very close with:
CREATE TRIGGER "main"."Orders_modified"
BEFORE UPDATE OF "order_date", "grand_total_in_dollars", "ship_to", "store", "more_href", "order_id", "grand_total" ON "Orders"
BEGIN
UPDATE Orders
SET modified=(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW', 'localtime'))
WHERE (OLD.order_id=NEW.order_id)
AND (
(OLD.order_date != NEW.order_date) OR
(OLD.grand_total_in_dollars != NEW.grand_total_in_dollars) OR
(OLD.ship_to != NEW.ship_to) OR
(OLD.store != NEW.store) OR
(OLD.more_href != NEW.more_href) OR
(OLD.grand_total != NEW.grand_total)
);
END;
The problem is, it does not work when a value is being put into any of those fields for the first time; i.e., when the old value is 'Null'. Works great otherwise.
My temporary workaround to this is to just default all the monitored fields as empty strings, but I'd like to understand why (& if) this is necessary.
Any tips as to why this may be, & any workarounds that might be available?
Many thanks in advance.
Instead of the operator != use the operator IS NOT which works when one or both of the operands are NULL:
......................................................
WHERE (OLD.order_id=NEW.order_id)
AND (
(OLD.order_date IS NOT NEW.order_date) OR
(OLD.grand_total_in_dollars IS NOT NEW.grand_total_in_dollars) OR
(OLD.ship_to IS NOT NEW.ship_to) OR
(OLD.store IS NOT NEW.store) OR
(OLD.more_href IS NOT NEW.more_href) OR
(OLD.grand_total IS NOT NEW.grand_total)
);

SQLite - update command yields null values

I'm having trouble performing some update on a SQLite database. I'm using the SQLite 3 shell for Windows.
I'm running the following command:
update resovled_chrom_counts set genus =
case resolved_name_full
when resolved_name_full is not null and resolved_name_full != ''
then substr(resolved_name_full,0,instr(resolved_name_full,' ')-1)
else
substr(original_name,0,instr(original_name,' ')-1)
end;
It seems to work on most rows, but some simply end up with a null value in their genus field. I tried checking some of them manually, by using the 'id' field of this table. For example, I found out that the row with id='kew-1' is null in it's genus field, and ran the following query:
select substr(resolved_name_full,0,instr(resolved_name_full,' ')-1)
from resovled_chrom_counts
where id='kew-1';
and to my surprise, I got a result (not null)!
Looks like the query works under the 'select' statement, but not under the 'update' statement.
Can anyone give an explanation and/or a solution?
Any help would be appreciated. Thanks!
The problem is not with the substr(resolved_name_full... but with the CASE.
A CASE expression can have two different forms:
CASE x WHEN y THEN ...: This compares the value of x against the value of y.
CASE WHEN a THEN ...: This checks whether the value of a is true or false.
The problem in the UPDATE statement is that there is a value (resolved_name_full) directly after the CASE, so the value of resolved_name_full is compared with the value of the expression resolved_name_full is not null and resolved_name_full != '', and this comparison always fails because resolved_name_full never happens to be 0 or 1.
Just use the second form of the CASE expression:
update resovled_chrom_counts set genus =
case
when resolved_name_full is not null and resolved_name_full != ''
then substr(resolved_name_full,0,instr(resolved_name_full,' ')-1)
else
substr(original_name,0,instr(original_name,' ')-1)
end;
SQLFiddle

<SQLITE> compare old value and new value

I have a trigger.
I want to do insert changed values after every update.
But I get syntax error on line IF .. THEN, and I can't find example.
IF (IFNULL(NEW.symbol,'') <> IFNULL(OLD.symbol,'')) THEN
INSERT INTO LOG(old_value, new_value, DATA, user)
VALUES ('Symbol: '|| IFNULL(OLD.symbol,''), 'Symbol: ' (IFNULL(NEW.symbol,''), CURRENT_TIMESTAMP, id_user)
Can You help me?
SQLite has no IF statement.
You must put the condition into the WHEN clause of the trigger.

SQL SERVER CASE STATEMENT CLARIFICATION

I have to execute a statement like ( i need and keyword along with when ).
select
'Is Allowed'= case A.Column
when
A.Column='XXXX' and Isnull(A.AnotherColumn,'')<>'' then 'Yes'
else
'No' end from TableName
I am getting syntax error,how to rewrite it without affecting the condition.
Try:
select case when A.Column='XXXX' and Isnull(A.AnotherColumn,'')<>'' then 'Yes'
else 'No' end as 'Is Allowed'
from TableName
SELECT
CASE A.Column
WHEN 'Is Allowed THEN 'First'
WHEN 2 THEN 'Second'
WHEN 3 THEN 'Third'
ELSE 'Other'
END
Is the general way of making a CASE (which is your question). However, your query/logic looks a bit convoluted. More detailed answer / query is possible, but would perhaps use more statements/ nested CASE.
Have a closer look at CASE (Transact-SQL)
SELECT CASE
WHEN some boolean expression
THEN value
ELSE default value
END
or
SELECT CASE value to check
WHEN vlue to check agains
THEN value
ELSE default value
END

Resources