How to properly call a function with date parameter?

Marcin Szynkowski

I had to create a function that computes how many people are born from the given date as a parameter.

I have a table named Person with date of birth written as "YY/MM/DD".

The function is as follows :

create or replace function persons_count(p_date date) RETURN NUMBER IS cnt NUMBER; 
BEGIN`
  SELECT COUNT(*) INTO cnt FROM person
  WHERE date_of_birth > To_Date(p_date,'dd.mm.yyyy'); 
  RETURN cnt;
END;

And i tried to run this function :

SET SERVEROUTPUT ON

DECLARE
  num NUMBER;
BEGIN
  num := persons_count('82/11/01');
  dbms_output.put_line(num);
END;

but it doesn't work. I get a message that number of parameters is not valid or that tha day/month is invalid.

Alex Poole

Your function takes a date parameter, but you are passing a string; and within the function you're converting an argument that is already a date into a date, which doesn't make much sense. Both are relying on your session NLS_DATE_FORMAT to do do implicit conversions. Inside the function you're effectively doing:

WHERE date_of_birth > To_Date(to_char(p_date, NLS_DATE_FORMAT),'dd.mm.yyyy'); 

while in the call you're doing:

persons_count(to_date('82/11/01', NLS_DATE_FORMAT));

If you're lucky your session will be set up right, but here you're relying on conflicting formats, so one or the other will fail or get an unexpected answer. You shouldn't rely on NLS settings, and you shouldn't convert a date to a string and back.

You want something more like:

create or replace function persons_count(p_date date)
RETURN NUMBER IS
  cnt NUMBER; 
BEGIN
  SELECT COUNT(*) INTO cnt
  FROM person
  WHERE date_of_birth > p_date;
  RETURN cnt;
END;

Although you may actually want >= p_date. And then call it with an explicit date, such as:

num := persons_count(to_date('1982/11/01', 'YYYY/MM/DD));

(note the four-digit year; don't use two-digit years, even with the RR format model element), or an ANSI date literal:

num := persons_count(DATE '1982-11-01');

I have a table named Person with date of birth written as "YY/MM/DD"

If your column has data type DATE then it has no intrinsic format. If you query the table and see date values formatted like that then your NLS_DATE_FORMAT is YY/MM/DD or maybe RR/MM/DD; or a client setting is formatting the date implicitly. It isn't stored like that and changing the client/session settings will change how it's displayed. It's possible you actually have it stored as a formatted string, but that would be a really bad idea, and your question implies it is actually a date.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to properly call a PHP function?

From Dev

How to call a function with a function as parameter?

From Dev

How to properly call a function using argparse?

From Dev

How to properly call a recursive function inside a for loop?

From Dev

How Can I Call a Function Properly?

From Dev

how to properly call passed function in child component

From Dev

How to properly call an ActionResult with parameter which return a view

From Dev

call oracle function with oracle date parameter in classic asp

From Dev

How to call a JavaScript function with parameter in jQuery?

From Dev

How to call one function with different parameter types

From Dev

How to call a JavaScript function with parameter in jQuery?

From Dev

How to call class template function with parameter

From Dev

How to call a function with a parameter as string in php?

From Dev

How to call DLL function using "&" parameter?

From Dev

How can I call function with parameter in bash?

From Dev

how to call function where parameter based on a promise

From Dev

How to call on init function from textarea with parameter?

From Dev

How to call a parameter list from a function in Julia

From Dev

how to properly call a function when it is a pointer to a class function but is outside that class

From Dev

how to properly call a function when it is a pointer to a class function but is outside that class

From Dev

How to call a Scala function with a single parameter and write the parameter first

From Dev

How do I call a nested JavaScript function properly

From Dev

How to use protocol properly to call a GameViewController's function?

From Dev

How to properly call a thiscall function with this being an mfc control

From Dev

how to properly call jquery select2 api function?

From Dev

how to call function inside reactjs render method properly?

From Java

How do I disambiguate this call to a generic function with a function parameter in Swift?

From Dev

How to call a function another function as a parameter passed as a string

From Dev

AngularJS ng-click: how to call a function with a function as parameter?

Related Related

  1. 1

    How to properly call a PHP function?

  2. 2

    How to call a function with a function as parameter?

  3. 3

    How to properly call a function using argparse?

  4. 4

    How to properly call a recursive function inside a for loop?

  5. 5

    How Can I Call a Function Properly?

  6. 6

    how to properly call passed function in child component

  7. 7

    How to properly call an ActionResult with parameter which return a view

  8. 8

    call oracle function with oracle date parameter in classic asp

  9. 9

    How to call a JavaScript function with parameter in jQuery?

  10. 10

    How to call one function with different parameter types

  11. 11

    How to call a JavaScript function with parameter in jQuery?

  12. 12

    How to call class template function with parameter

  13. 13

    How to call a function with a parameter as string in php?

  14. 14

    How to call DLL function using "&" parameter?

  15. 15

    How can I call function with parameter in bash?

  16. 16

    how to call function where parameter based on a promise

  17. 17

    How to call on init function from textarea with parameter?

  18. 18

    How to call a parameter list from a function in Julia

  19. 19

    how to properly call a function when it is a pointer to a class function but is outside that class

  20. 20

    how to properly call a function when it is a pointer to a class function but is outside that class

  21. 21

    How to call a Scala function with a single parameter and write the parameter first

  22. 22

    How do I call a nested JavaScript function properly

  23. 23

    How to use protocol properly to call a GameViewController's function?

  24. 24

    How to properly call a thiscall function with this being an mfc control

  25. 25

    how to properly call jquery select2 api function?

  26. 26

    how to call function inside reactjs render method properly?

  27. 27

    How do I disambiguate this call to a generic function with a function parameter in Swift?

  28. 28

    How to call a function another function as a parameter passed as a string

  29. 29

    AngularJS ng-click: how to call a function with a function as parameter?

HotTag

Archive