I'm having a little trouble with this sql view.
CREATE OR REPLACE VIEW view_themed_booking AS
SELECT tb.*,
CASE
WHEN (tb.themed_party_size % 2) = 0 THEN
(tb.themed_party_size-2)/2
ELSE ((tb.themed_party_size-2)/2) + 0.5
END themed_tables
FROM themed_booking tb;
Can anyone help me here? I'm trying to add a column to the end of the view that the natural number result of (S-2)/2 where S is the themed_party_size.
When i say natural number result i mean like round up the answers that end in .5 so if S=7 the answer would be 3 and not 2.5.
The error I get when I try and run the above code is
Error starting at line 1 in command:
CREATE OR REPLACE VIEW view_themed_booking AS
SELECT tb.*,
CASE WHEN (tb.themed_party_size % 2) = 0
THEN (tb.themed_party_size-2)/2
ELSE ((tb.themed_party_size-2)/2) + 0.5
END themed_tables
FROM themed_booking tb
Error at Command Line:3 Column:34
Error report:
SQL Error: ORA-00911: invalid character
00911. 00000 - "invalid character"
*Cause: identifiers may not start with any ASCII character other than
letters and numbers. $#_ are also allowed after the first
character. Identifiers enclosed by doublequotes may contain
any character other than a doublequote. Alternative quotes
(q'#...#') cannot use spaces, tabs, or carriage returns as
delimiters. For all other contexts, consult the SQL Language
Reference Manual.
*Action:
If it makes a difference I am using sqldeveloper connected to an oracle server so I can use PL/SQL.
The error message is telling you what the problem is.
Look at Line:3 Column:34
It is an invalid character
CREATE OR REPLACE VIEW view_themed_booking AS
SELECT tb.*,
CASE WHEN (tb.themed_party_size % 2) = 0
^
My suspicion is that you are trying to use the modulo operator.
Since you are using oracle PL/SQL, you should use mod
Here is a reference Oracle/PLSQL: Mod Function
I think you can simplify with the CEIL() or ROUND() function:
CREATE OR REPLACE VIEW view_themed_booking AS
SELECT tb.*,
ROUND((tb.themed_party_size-2)/2) AS themed_tables
FROM themed_booking tb;
Not sure why you get that error. Perhaps it's the % operator that is not available in Oracle. This links suggests so: Fundamentals of PL/SQL. There seems to be a MOD() function though.
Related
I'm trying to do overlap in SQL (using postgres) but receive an syntax error. And i do not know why I'm getting error and whats wrong with my SQL Statement
Error text
[42601] ERROR: syntax error at or near "[" Position: 296
SQL Statement
SELECT *,
ARRAY_AGG("reserve"."state_id" ) AS "states" FROM "orders_order"
JOIN "reserve" ON ("order"."id" = "reserve"."order_id")
GROUP BY "order"."id"
HAVING (NOT (ARRAY_AGG("reserve"."state_id" )&& ['test']::varchar(255)[]))
As documented in the manual there are two ways to specify an array:
First with the array keyword:
array['test']
or as a string enclosed with curly braces:
'{test}'::text[]
In the second case the casting might or might not be needed depending on the context on where it's used.
There is no need to cast it to a varchar(255)[] array. text[] will do just fine - especially when comparing it to the result of an array_agg() which returns text[] as well.
I am new to SQL plus . Could anyone help me figure out the syntax error for my code?
CREATE OR REPLACE VIEW BR_STATUS AS
SELECT CARTS_PER_CUSTOMER.loginName,CARTS_PER_CUSTOMER.number_of_carts,
CASE WHEN (number_of_carts < 1 ) THEN 'BR-1 Satisfied.'
ELSE 'BR-2 violated.'
END AS 'BR-status'
FROM CARTS_PER_CUSTOMER;
Whenever I try to run this part of the code, I get this error message
ORA-00923: FROM keyword not found where expected.
I followed several oracle documentation for CASE, but can't figure out what I am writing wrong. Any suggestions would be appreciated.
There is a unnecessary comma before FROM clause nothing wrong with CASE statement. Also use double quotes for Alias name instead of single quotes (thanks to Jarlh)
SELECT CARTS_PER_CUSTOMER.loginName,
CARTS_PER_CUSTOMER.number_of_carts,
CASE
WHEN ( number_of_carts < 1 ) THEN 'BR-1 Satisfied.'
ELSE 'BR-2 violated.'
END AS "BR-status" --Remove the comma here
FROM CARTS_PER_CUSTOMER;
I Have an error on a oracle server but I don't understand why is not work.
I use the software oracle sql developer.
The query is:
SELECT * FROM TestView WHERE REPLACE(TestView.Row2, '.', ',') > 0 ;
The value who is in TestVue.Row2 : '46.08','-46.47','1084.05','66500',...
"TestView" is a view who check to return a row without empty value
When I Execute the query I have always an error who says:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause:
*Action:
Thanks for your help
Zoners
You have a row in your table, which cannot be parsed into number format. Most likely it is a blank column.
SELECT * FROM TestVue WHERE TestVue.Row2 IS NULL ;
Use a NVL Function to avoid errors caused by null field and a TO_NUMBER function to cast into a number before comparision with zero
SELECT * FROM TestView WHERE TO_NUMBER(REPLACE(NVL(TestView.Row2,'0'), '.', ',')) > 0 ;
You should be able to find the row with something like:
select Row2
from TestVuw
where length(trim(translate(Row2, '+-.,0123456789', ' '))) = 0
Of course, this allows multiple +, -, ., and ,. It can be further refined using a regular expression, but this usually works to find the bad ones.
I have the following code in which I'm using a variable to pass a list of values to multiple SQL statements (I can't save in a table as I don't have authority and don't want to have to maintain the list in all of the various SQL sections).
It works fine as long as all of the values are on a single line... but as I have so many values; I'd like to split it into multiple lines and use the Continuation Character '-'.
I'm running Oracle SQL Developer 2.1.1.64 against Oracle 10g (I also tried this in PL/SQL Developer and it failed there as well)
--=========================================
define subclasses = ('10-1010-10','10-1010-15','10-1010-20', -
'10-1010-25','10-1010-30') --- there are another 60 values...
select item from item_master where ((subclass) in &&subclasses);
Select Price from Item_prices where ((subclass) in &&subclasses);
--=========================================
I get the following error
ORA-01722: invalid number
01722. 00000 - "invalid number"
as it is parsing the code as
select item from item_master where ((subclass) in ('10-1010-10','10-1010-15',
'10-1010-20', -'10-1010-25','10-1010-30'))
...keeping the continuation code '-' in the SQL....tho it DOES go to the 2nd line of values.
If I remove the '-' ... it only processes the values on the first line and parses as
select item from item_master where ((subclass) in ('10-1010-10','10-1010-15','10-1010-20', )
... losing the second to nth line of values (and throwing errors as it ends w/ ',' and doesn't have the final ')'). How do I fix this?
You could do this:
column subc new_value subclasses
select q'[('10-1010-10','10-1010-15','10-1010-20',
'10-1010-25','10-1010-30')]' as subc
from dual;
Now &subclasses. will contain all the codes.
NB I used the q'[...]' quote syntax to avoid have to double up all the quotes in the data.
I noticed that you are trying to substitute a list of string variables into the select statement. You should rewrite your define statement to make it a single list of strings like this:
define subclasses = '''10-1010-10'',''10-1010-15'',''10-1010-20'', -
''10-1010-25'',''10-1010-30'''; --- there are another 60 values...
The - should be fine as a continuation character (see Oracle documentation here).
Now, when you execute your select statements you need to edit the WHERE clause so they are formatted so it will plug those values directly in there as written:
Select item from item_master where subclass in (&subclasses);
Select Price from Item_prices where subclass in (&subclasses);
This will end up being interpreted as if you had written:
Select item from item_master
where subclass in ('10-1010-10','10-1010-15','10-1010-20', '10-1010-25','10-1010-30');
If you have a lot of values though, you might run into limitations for substitution variables if you are using SQL*Plus (i.e. limited to 240 bytes per variable). In that case, you can either split the variables into multiple variables and concatenate them in the SELECT, or if you are in a PL/SQL environment, you can create variables that will hold the larger data size.
Are there any nvl() equivalent functions in SQL?
Or something close enough to be used in the same way in certain scenarios?
UPDATE:
no if statementsno case statementsno isnullno coalesce
select nvl (purge_date,"SODIUFOSDIUFSDOIFUDSF") from id_rec where id=36581;
(expression)
SODIUFOSDIUFSDOIFUDSF
1 row(s) retrieved.
select isnull (purge_date,"SODIUFOSDIUFSDOIFUDSF") from id_rec where id=36581;
674: Routine (isnull) can not be resolved.
Error in line 1
Near character position 8
select coalesce (purge_date,"SODIUFOSDIUFSDOIFUDSF") from id_rec where id=36581;
674: Routine (coalesce) can not be resolved.
Error in line 1
Near character position 8
select decode(purge_date, NULL, "01/01/2009", purge_date) from id_rec where id=74115;
800: Corresponding types must be compatible in CASE expression.
Error in line 1
Near character position 57
ISNULL (for a single replace)
or
COALESCE (Returns the first nonnull expression among its arguments.)
SQL Server:
IsNull or COALESCE
http://msdn.microsoft.com/en-us/library/ms184325.aspx
Sybase:
isnull function
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.blocks/html/blocks/blocks162.htm
Postgres:
I couldn't find one though haven't fully checked. Suggests to select where IS NULL and build from here
http://archives.postgresql.org/pgsql-sql/1998-06/msg00142.php
DB2 - COALESCE
http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/admin/r0000780.htm
You seem to be using Informix.
AFAIK, there is DECODE there:
DECODE(field, NULL, 'it is null, man', field) should give you same result as NVL(field, 'it is null, man')
Please post exact name and version of the RDBMS you are using.
The problem with your DECODE statement that is generating the 800 error is simple. '01/01/2009' is being treated as a string, and it's actually the 4th argument that generates the error.
Appreciate that the input and output of a DECODE statement can be different data-types, so the engine requires you to be more explicit in this case. (Do you want purge_date cast as a string or the string '01/01/2009', or the string argument parsed as a date or the original date? There's no way for the engine to know.
Try this:
SELECT DECODE(purge_date, NULL, '01/01/2009'::DATE, purge_date)
You could also write that 3rd argument as:
DATE('01/01/2009')
MDY(1,1,2009)
depending on version and personal preference.