To function or not to function, that is the question at hand

91 Views Asked by At

I am working on some SQL statements for an ASP.NET application. One of the things that is required is to only display information in an open period. The period is updated automatically by the vendor software as the previous period is closed. So I finding myself doing a bunch of sub selects like:

where date >= (SELECT DATE(CONCAT('20', CONCAT(YY, CONCAT('-', CONCAT( MM, (CONCAT('-', DD))))))) FROM LIB/FILE') 

Yes, each portion of the date is in separate fields.

Would making this query a function make the query more efficient? I have never created a function before how would I do that? My thought is having something like:

IsInRange(date)

So I can do WHERE IsInRange(date).

Or is there a better way?

3

There are 3 best solutions below

0
On BEST ANSWER

For now I created a view to join the fields into a single date, this at least cleaned up the query a bunch.

1
On

Yes, but the point of the function would be for code reuse. If you are doing this in other places, or anticipate that then go for it.

1
On
CREATE FUNCTION toDate(yy CHAR(2), mm CHAR(2), dd CHAR(2))
RETURNS DATE
RETURN DATE('20' || '-' || yy || '-' || mm || '-' || dd)

SELECT *
FROM table
WHERE date <= toDate(yy, mm, dd)