Can anybody explain the impact on the performance of the query when we are using the Length() function to check the length of string (Varchar) data?
SELECT LENGTH(FIRST_NAME), FIRST_NAME FROM MIT_STUDENTS;
This is my query above for the length() function. Can anyone explain the performance of length() function in the above query?
Related
I want to select today's visited urls from the firefox database places.sqlite.
As first attempt, the query I used to accomplish this was (see the operator >):
SELECT datetime(moz_places.last_visit_date/1000000,'unixepoch'), moz_places.title
FROM moz_places
WHERE moz_places.last_visit_date/1000000>strftime('%s','now','start of day')
ORDER BY moz_places.last_visit_date DESC;
The answer to this query is nothing.
Then I changed the query to this, which is pretty much the same (see the operators - and >):
SELECT datetime(moz_places.last_visit_date/1000000,'unixepoch'), moz_places.title
FROM moz_places
WHERE moz_places.last_visit_date/1000000-strftime('%s','now','start of day')>0
ORDER BY moz_places.last_visit_date DESC;
then the answer is correct.
Does anybody out there know why > works in one query by does not in the other?
The strftime() function always returns a string, and a string always compares larger than a number.
When you try to use a string with addition/subtraction, it is automatically converted into a number first, so the result of last_visit_date-strftime(...) is a number.
You could change the function call in the original query to:
CAST(strftime('%s', ...) AS NUMBER)
or, less obvious, if you want to save typing:
strftime('%s', ...)+0
How to write a DQL script for getting the case insensitive string values using order by.
select *from country order by lower(name) giving me error. Thanks for your help in advance
My understanding is there are 2 ways of returning retult from a procedure or function,
OUT type parameter: this is used to get values from procedures and functions
returned value from a function,
My question is,
what is the different between OUT parameter and returned value in a function?
If I have a OUT parameter in a function, does it mean I can have 2 "returned" result?
Returned values from functions that has no out parameters can be used from SQL queries (inside a select or dml statement). If you have a code that returns a simple primitive type, i reccomend function.
SELECT * FROM PAYMENTS WHERE DATE = LAST_DAY_OF_MONTH(SYSDATE)
If you needs to return two or more related values, you can create a pl/sql object and return it in a function, and it can be used from plain sql too!
SELECT RESP.CODE, RESP.BODY FROM (SELECT GET_HTTP_RESP(URL) FROM DUAL) RESP;
Functions can be out parameters too, but that functions, like procedures, cannot be called from SQL queries.
If the code is really not supposed to be called from a SQL query, and you like to return not related values or values that not are the main objective of that code, a procedure could be a good choice.
Not always you have a "correct" choice, is more like a programming style question.
Yes as suggested its a very huge topic but I can suggest some differences. Hope it Helps.
Answer :
1) Yes function can have multiple OUT IN params but the return type will be a constant. So if you have a OUT Param in a function so it cant be called in a SQL. Similarly FUNCTION without OUT param can be called in SQL.
2) You can use the OUT as well as RETURN type to get the values from FUNCTION but still return type remains only ONE.
CREATE OR REPLACE FUNCTION FUNCT_TEST
(
p_in IN NUMBER,
p_out OUT NUMBER
)
RETURN VARCHAR2
AS
BEGIN
p_out:=10;
RETURN 'AVRAJIT';
END;
SELECT FUNCT_TEST(1) FROM DUAL;
ORA-06553: PLS-306: wrong number or types of arguments in call to 'FUNCT_TEST'
CREATE OR REPLACE FUNCTION FUNCT_TEST
(
p_in IN NUMBER
)
RETURN VARCHAR2
AS
BEGIN
--p_out:=10;
RETURN 'AVRAJIT';
END;
SELECT FUNCT_TEST(1) FROM DUAL;
FUNCT_TEST(1)
AVRAJIT
You can add and use as many IN OUT parameters in Functions like Procedure (Limit of IN OUT parameters in Functions/Procedures is 65536).
But if you have created the Function with OUT parameters you will get below error while accessing the function from SELECT query:
ORA-06572: Function function_name has out arguments
I am doing 1 simple db connection test in Robot framework.I am doing as following-
${queryResults1} Query <sql query>
now I want to use the value of ${queryResults1} as input to another query. I am doing
Execute Sql String select * from customer where customer_id=${queryResults1}
here I am getting error .Execute Sql String doesnot get value of queryresult
how can I do this ?
thanks in Advance!!!
The problem is that your first query is returning a list of tuples -- a list of rows, each of which is a tuple of columns. Even though you're apparently expecting a single value from a single column in a single row, the data is still in this format. You need to pull the value out of that list of tuples before passing it to your second query.
For example:
Execute Sql String select * from customer where customer_id=${queryResults1[0][0]}
As we have two options to intercept the null values coming from database...
ISNull
Coalesce
Following are the ways to write the query for the above two functions...
Select IsNull(Columnname, '') As validColumnValue From TableName
Select Coleasce(Columnname, '') As validColumnValue From TableName
Query - Which should be prefered in which situation and why?
This has been hashed and re-hashed. In addition to the tip I pointed out in the comment and the links and explanation #xQbert posted above, by request here is an explanation of COALESCE vs. ISNULL using a subquery. Let's consider these two queries, which in terms of results are identical:
SELECT COALESCE((SELECT TOP (1) name FROM sys.objects), N'foo');
SELECT ISNULL((SELECT TOP (1) name FROM sys.objects), N'foo');
(Comments about using TOP without ORDER BY to /dev/null/ thanks.)
In the COALESCE case, the logic actually gets expanded to something like this:
SELECT CASE WHEN (SELECT TOP (1) ...) IS NULL
THEN (SELECT TOP (1) ...)
ELSE N'foo'
END
With ISNULL, this does not happen. There is an internal optimization that seems to ensure that the subquery is only evaluated once. I don't know if anyone outside of Microsoft is privy to exactly how this optimization works, but you can this if you compare the plans. Here is the plan for the COALESCE version:
And here is the plan for the ISNULL version - notice how much simpler it is (and that the scan only happens once):
In the COALESCE case the scan happens twice. Meaning the subquery is evaluated twice, even if it doesn't yield any results. If you add a WHERE clause such that the subquery yields 0 rows, you'll see similar disparity - the plan shapes might change, but you'll still see a double seek+lookup or scan for the COALESCE case. Here is a slight different example:
SELECT COALESCE((SELECT TOP (1) name FROM sys.objects
WHERE name = N'no way this exists'), N'foo');
SELECT ISNULL((SELECT TOP (1) name FROM sys.objects
WHERE name = N'no way this exists'), N'foo');
The plan for the COALESCE version this time - again you can see the whole branch that represents the subquery repeated verbatim:
And again a much simpler plan, doing roughly half the work, using ISNULL:
You can also see this question over on dba.se for some more discussion:
Performance difference for COALESCE versus ISNULL?
My suggestion is this (and you can see my reasons why in the tip and the above question): trust but verify. I always use COALESCE (because it is ANSI standard, supports more than two arguments, and doesn't do quite as wonky things with data type precedence) unless I know I am using a subquery as one of the expressions (which I don't recall ever doing outside of theoretical work like this) or I am experiencing a real performance issue and just want to compare to see if COALESCE vs. ISNULL has any substantial performance difference (which outside of the subquery case, I have yet to find). Since I am almost always using COALESCE with arguments of like data types, I rarely have to do any testing other than looking back at what I've said about it in the past (I was also the author of the aspfaq article that xQbert pointed out, 7 years ago).
begin humor: The 1st, the 2nd will never work it's spelled wrong :D END humor
---Cleaned up response---
Features of isNull(value1,value2)
Only supports 1 valuation, if the first is null, the 2nd will be used, so if its null too you get null back!
is non-ANSI standard. Meaning if database portability is an issue, don't use this one
isnull(value1,value2) will return the datatype for Value1
will return the datatype for the selected value and fail when implicit conversions can't occur
Features of Coalesce(Value1, Value2, value3, value...)
Supports multiple valuations of Null; basically will pull in the first non-null value from the list. if all values are null in list, null is returned.
is ANSI-standard meaning database portability shouldn't be an issue.
will return the datatype for the selected value and fail if all fields in the select do not return the same datatype.
So to answer the question directly:
It depends on the situation if you need to develop SQL that
is DB independent; coalesce is more correct to use.
allows for multiple evaluations; coalesce is more correct (of course you could just embed isnull over and over and over...) but put that under a performance microscope, and coalesce may just win. (i've not tested it)
are you using a db engine that supports isNull? (if not use coalesce)
how do you want type casting handled? implicitly or not.
---ORIGINAL------
is null only supports 2 evaluations
coalesce supports many more... coalesce (columnName1, ColumnName2, ColumnName3, '')
coalesce returns datatype similar to that of case evaluation, whereas isnull returns datatype of first in list. (which I found interesting!)
As to when to use which. you'd have to investigate by looking at execution plan of both on SQL 2008 and 2005, different versions different engines different ways to execute.
Furthermore coalesce is ansii standard, isnull is engine specific. Thus if you want greater portability between dbengines use coalesce.
More info here aspfaq
or here msdn blog
you may take this onto consideration.
ISNULL function required two parameters: the value to check and the replacement for null values
2.COALESCE function works a bit different COALESCE will take any number of parameters and return the first non-NULL value , I prefer COALESCE over ISNULL 'cause
meets ANSI standarts, while ISNULL does not.
I hope you found the answer to your question.