What did I do wrong? UPDATE with condition and LIKE postgres - postgresql

There is a table and I need to update the data in the 3rd column, according to the condition. If there are brackets, then you need to take information from there, and if not, what is in the first column. Put it where there is no information on the 3rd column. In most cases it is necessary to put where status down or error
https://dbfiddle.uk/GgFft6cY
here is my request:
UPDATE table_1
SET name_3 =
CASE
WHEN name_3 != '' THEN name_3
WHEN name_1 LIKE '%(%' THEN SUBSTRING(name_1 FROM '%(%' FOR ')')
ELSE name_1
END
WHERE status IN ('down', 'error');
ERROR: invalid regular expression: parentheses () not balanced
what's wrong? or can it be done differently?

After FROM and FOR you need a start index and the length, which is a number.
UPDATE table_1
SET name_3 =
CASE
WHEN name_3 <> '' THEN name_3
WHEN name_1 LIKE '%(%' THEN SUBSTRING(name_1 FROM POSITION ('(' in name_1)+1 FOR POSITION (')' in name_1) -POSITION ('(' in name_1)-1)
ELSE name_1
END
WHERE status IN ('down', 'error');
Demo

Related

ELSE value not executed in CASE expression PostgreSQL

I'm using CASE expression to display "NamaAyah" or "NamaIbu" or "NamaWali", and if all of them is empty, the default value will display "ORTU-'NomorPokok' ".
But the default value not displayed, it just displays symbol "-" in my table. I think the value in ELSE statement not executed.
Postgre Version : PostgreSQL 9.4.15
This is my code
SELECT
"MahasiswaID" AS "PERSONID","NomorPokok" AS "KODE",
UPPER(CASE
WHEN "NamaAyah" <> '' THEN "NamaAyah"
WHEN "NamaIbu" <> '' THEN "NamaIbu"
WHEN "NamaWali" <> '' THEN "NamaWali"
ELSE 'ORTU'||'-'||"NomorPokok"
END) AS "NAMALENGKAP"
FROM "MasterMahasiswa" ORDER BY "KODE"
and this is the result
The expression you have can simpler be:
ELSE 'ORTU-'||"NomorPokok"
Apart from that, the only reasonable explanation for what you display is that there are literal - in one or more of your columns "NamaAyah", "NamaIbu" and "NamaWali". Did you check that?

how do i make a grading system using cases?

pretty sure this is a pretty simple issue:
I need to create an exam results table for a student, but it needs to meet the conditions of If the student is awarded a grade of 70% or more then the result is to be shown as 'Distinction', a grade of at least 50% but less than 70% is to be shown as 'Pass' and grades below 50% are to be shown as 'Fail'. If the student has not taken the examination then the result is shown as 'Not taken'.
I'm having issues with my code which i can't seem to get right:
CREATE VIEW results_table
AS SELECT entry.excode, sname, student.sno, entry.egrade, result AS entry
FROM student
JOIN entry
ON student.sno = entry.sno
GROUP BY entry.excode, sname, student.sno, entry.egrade, result
SELECT result
CASE result
WHEN entry.egrade >= 70 THEN 'Distinction'
WHEN entry.egrade >= 50 THEN 'Pass'
WHEN entry.egrade < 50 THEN 'Fail'
WHEN entry.egrade = NULL THEN 'Not Taken'
END;
ORDER BY entry.excode,sname
could somebody please tell me where i'm going wrong?
There are two types of SQL CASE statement, as documented for PostgreSQL here:
CASE WHEN condition THEN result ELSE default END
CASE expression WHEN value THEN result ELSE default END
The first form tests a series of (unrelated) conditions, and returns the result for the first one which is true, or the ELSE if none are. The second form is a short-hand for testing multiple conditions against a single value; CASE x WHEN y THEN 'y' WHEN z THEN 'z' END is short-hand for CASE WHEN x = y THEN 'y' WHEN x = z THEN 'z' END.
In your code, you're muddling the two syntaxes; you can't use the short-hand because you're doing something more complex than = in the conditions, so you need to leave out the word result:
CASE
WHEN entry.egrade >= 70 THEN 'Distinction'
WHEN entry.egrade >= 50 THEN 'Pass'
WHEN entry.egrade < 50 THEN 'Fail'
WHEN entry.egrade IS NULL THEN 'Not Taken'
END
(Note: I've also changed = NULL to IS NULL, since no value is ever "equal to" NULL, because it means "we don't know what the value is equal to".)
You then need to put that into a SELECT query in the same place you'd select a normal column.
So starting from this simple query...
SELECT
entry.egrade as result
FROM
entry;
...you can put your CASE statement in like this:
SELECT
CASE
WHEN entry.egrade >= 70 THEN 'Distinction'
WHEN entry.egrade >= 50 THEN 'Pass'
WHEN entry.egrade < 50 THEN 'Fail'
WHEN entry.egrade IS NULL THEN 'Not Taken'
END as result
FROM
entry;

Checking if column contains meaningful data in SQL Server

I have a SQL Server 2005 table which contains several columns which I need to check if they have data using
case when MyColumn <> '' then 1 else 0 END
My problem is that I need something to work on all datatypes, as the above wouldn't work on 0 values
example
case when MyColumn <> '' then 1 else 0 END
will return false if the value is 0, which I require otherwise.
I tried to trim, convert, cast, using len and datalength to no avail.
Anyone have any suggestion please?
Put check for 0 and null as well. some thing like:
case when MyColumn <> '' or MyColumn <>'0' Or MyColumn IS NOT NULL or MyColumn <>0 then 1 else 0 END

How does sql evaluate ISNUMERIC(ISNULL(VALUE, 'blah'))

My assumption was that it would return a true if that value was numeric (within the isnumeric range) but FALSE if the ISNULL returns 'blah'. Seems like my assumption was off...
I'm using the it in the following way
case when ISNULL(ISNUMERIC(c.npinumber), 'blah') = 1
then c.NPiNUmber
else 'not valid: ' + c.NpiNumber
end as npi
Building on Dhruvesh's answer,
case
when ISNUMERIC(c.npinumber) = 1 then c.NPiNUmber
else 'not valid: ' + c.NpiNumber
end as npi
Will produce NULL anytime NpiNumber is NULL. The reason is that NULL + any string will still return NULL. The solution is to simply use the COALESCE function
case
when ISNUMERIC(c.npinumber) = 1 then c.NPiNUmber
else 'not valid: ' + COALESCE(c.NpiNumber, 'NULL VALUE')
end as npi
select ISNUMERIC(ISNULL(NULL, 'blah')),
ISNUMERIC(ISNULL(1234, 'blah')),
ISNUMERIC(ISNULL('ab', 'blah'))
Returns 0, 1, 0 - so your logic is correct.
When SQL's not behaving I like to simplify my query. Try running the query without your case statement first. If the results look right, then add additional logic.
What collation is your database? It's always a good idea to keep your column names properly cased (I'm looking at that all-lowercase column name over there...).
You don't require ISNULL. ISNUMERIC will return 1 if it's numberic or 0 if it's NULL or non-numeric.
case
when ISNUMERIC(c.NpiNumber) = 1 then c.NPiNUmber
else 'not valid: ' + c.NpiNumber
end as npi
Also as Euric Mentioned you may want to look at your all-lowercase column name.

need to translate specific t-sql case in pl/sql

Can anyone tell me how to translate the following T-SQL statement:
SELECT fileld1 = CASE
WHEN T.option1 THEN -1
ELSE
CASE WHEN T.option2 THEN 0
ELSE 1
END
END
FROM Table1 AS T
The point is I need to validate two different options from the table for a single field in the select statement..
I have tried to do somthing with an IF statement in pl/sql, but it just doesnt work for me:
SELECT IF T.option1 THEN -1
ELSE IF T.option2 THEN 0
ELSE 1
END
FROM Table1 AS T
I am not actually sure how to write IF statement inside the SELECT statement..
And also, I need to do it INSIDE the select statement because I am constructing a view.
Use:
SELECT CASE
WHEN T.option1 = ? THEN -1
WHEN T.option2 = ? THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
I can't get your original TSQL to work - I get:
Msg 4145, Level 15, State 1, Line 4
An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.
...because there's no value evaluation. If you're checking if the columns are null, you'll need to use:
SELECT CASE
WHEN T.option1 IS NULL THEN -1
WHEN T.option2 IS NULL THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
...or if you need when they are not null:
SELECT CASE
WHEN T.option1 IS NOT NULL THEN -1
WHEN T.option2 IS NOT NULL THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
CASE expressions shortcircuit - if the first WHEN matches, it returns the value & exits handling for that row - so the options afterwards aren't considered.
If I remember correctly, PL/SQL also supports the case. You just would have to move the column alias from "field1=" before the expression to "AS filed1" after the expression.