I just want to ask what's the proper way to use IF statement in SQL Server 2014 Express? I have this code but no luck it is not working.
select
field1,
field2,
field3,
field4,
if(field5 = 'value1', field5,
if(field5 = 'value2', field5,
if(field5 = 'value3', field5,
if(field5 = 'value4', field5, field6))))
from
table1
Any help will be greatly appreciated. Thanks in advance.
the standard SQL syntax for IF is CASE WHEN THEN END, for example:
SELECT field1,
CASE field5
WHEN 'value1' THEN field5
WHEN 'value2' THEN field4
ELSE field3
END
FROM table1
Or
SELECT field1,
CASE WHEN field5='value1' THEN field5 ELSE field4 END
FROM table1
The ELSE part is optional..
Related
In MySQL we can write this query. Assuming there's a column field1 & field2 and a table table1.
select IF(field1 > 10, true, field2) AS `mycolumn` from table1
How can I achieve similar logic with Teradata query?
Use a CASE expression:
SELECT CASE WHEN field1 > 10 THEN 'true' ELSE field2 END AS mycolumn
FROM table1;
The above assumes that field2 be a text column. If not, then the 'true' in the CASE expression would have to be replaced with a literal matching the type of field2.
I'm creating a procedure on db2 that will insert values into a table only if the table is empty. I've created the following statements, but something is wrong since I'm getting error:
[42601][-104] An unexpected token "END-OF-STATEMENT" was found
following "END FOR".
Expected tokens may include: " END IF".. SQLCODE=-104, SQLSTATE=42601,
DRIVER=4.7.85
create or REPLACE PROCEDURE proc1
BEGIN
IF (exists (select 1 from table1)) then
TRUNCATE TABLE table1;
ELSE
FOR l1 as
select id, max(bla) as bla from table2 group by id
do
insert into table1 (column1, column2)
values (id, bla);
END FOR;
END IF;
END;
thanks!
Apparently, this little change helped to solve the problem:
create or REPLACE PROCEDURE proc1
BEGIN
IF (exists (select 1 from table1)) then
DELETE FROM TABLE table1;
END IF;
FOR l1 as
select id, max(bla) as bla from table2 group by id
do
insert into table1 (column1, column2)
values (id, bla);
END FOR;
END;
why you dont do just this
create or REPLACE PROCEDURE proc1
BEGIN
DELETE FROM table1;
insert into table1 (column1, column2)
select id, max(bla) from table2 group by id;
END;
I have to make a regular expression where I need to replace all the words of a dynamic query in ORACLE with NULL except for those words that begin with the # character. For example:
SQL:
SQL: SELECT #param1, column2, column3, #param2 FROM dual WHERE #code = code_table AND amount > #param4 + 50
Using REGEXP_REPLACE
DECLARE
vl_result VARCHAR2(1000);
BEGIN
vl_result := REGEXP_REPLACE('SELECT #param1, column2, column3, #param2 FROM dual WHERE #code = code_table AND amount > #param4 + 50', 'EXP_REG', '');
dbms_output.put_line(vl_result);
END;
should have the following result:
#param1#param2#code#param4
And try several in various ways and still can not.
They know if you can do this?? and how serious the regular expression.
I'm handling PL / SQL
The following works for the given example:
select REGEXP_REPLACE('SELECT #param1, column2, column3, #param2 FROM dual WHERE #code = code_table AND amount > #param4 + 50', '.*?((#[^ ,]+)|$)', '\1') new_str
from dual;
NEW_STR
--------------------------
#param1#param2#code#param4
This also uses a back reference, and works for your example:
select REGEXP_REPLACE('SELECT #param1, column2, column3, #param2 FROM dual WHERE #code = code_table AND amount > #param4 + 50',
'[^#]?(#[[:alnum:]]+)?', '\1')
from dual;
REGEXP_REPLACE('SELECT#PAR
--------------------------
#param1#param2#code#param4
The same thing works from PL/SQL (as does #boneists of course):
set serveroutput on
DECLARE
vl_result VARCHAR2(1000);
BEGIN
vl_result := REGEXP_REPLACE('SELECT #param1, column2, column3, #param2 FROM dual WHERE #code = code_table AND amount > #param4 + 50',
'[^#]?(#[[:alnum:]]+)?', '\1');
dbms_output.put_line(vl_result);
END;
/
PL/SQL procedure successfully completed.
#param1#param2#code#param4
UPDATE Table1
SET [Marks] =
(
SELECT
CASE STATEMENTS
FROM Table2 T2
WHERE Table1.ID = T2.ID)
)
The above UPDATE statements works fine, but if the ID doesn't match then it insert NULL value for 'Marks'.
But i wanted to keep the original value for Marks in Table1 if the Table1 and Table2 ID doesn't match.
How do i implement that in my code please.
i also tried using WHERE EXISTS BUT STILL no luck. I wonder whats the exact use of it.
Any help much appreciated.
UPDATE Table1
SET [Marks] =
(
SELECT
CASE STATEMENTS
FROM Table2 T2
WHERE Table1.ID = T2.ID)
)
WHERE id IN (SELECT id FROM table2)
I have the following Oracle SQL that works. All that I am trying to do is to add one field to the output and I cant get this SQL working:
Here is my information:
table name: access_log
col name: activity
value: Download file:/webdocs/data/groupXXX/case/03_28_54_9_0000011856.pdf
Now here is my SQL that IS WORKING right now:
select regexp_replace(activity, '^.*/(.*)/.*$', '\1') AS FILENAME,
COUNT (regexp_replace(activity, '^.*/(.*)/.*$', '\1')) AS DOWNLOADS,
FROM access_log where id = 5339 and time_stamp BETWEEN TO_DATE ('2014/02/01', 'yyyy/mm/dd') AND TO_DATE ('2014/02/02', 'yyyy/mm/dd')
GROUP BY regexp_replace(activity, '^.*/(.*)/.*$', '\1')
ORDER BY DOWNLOADS DESC;
But I would like to change the SQL to also display the full values of "activity" all the way to the right but I cant add this field and keep the SQL working.. cna someone please help me..
select regexp_replace(activity, '^.*/(.*)/.*$', '\1') AS FILENAME,
COUNT(1) OVER ( PARTITION BY regexp_replace(activity, '^.*/(.*)/.*$', '\1')) AS DOWNLOADS,
activity
FROM access_log
where id = 5339
and time_stamp BETWEEN TO_DATE ('2014/02/01', 'yyyy/mm/dd') AND TO_DATE ('2014/02/02', 'yyyy/mm/dd')
ORDER BY DOWNLOADS DESC;
We use COUNT() here as a window function approach.