How to pass date parameter inside the oracle procedure? - sql

Below is the sample procedure code,
create or replace procedure pro_test(start_date date, end_date date)
is
begin
insert into test1 select col1, col2, col3 from main where range_date between start_date and end_date;
exception
< what are the exception I need to capture >
end;
/
Q1 : Is this right way to pass date directly inside the procedure?
Q2 : If not, can I pass varchar inside the procedure , to convert the date in declaration part?
Q3 : In begin part am using between operator, can i pass procedure parameter directly ?
While executing this procedure, exec pro_test('10102015','30102015'); What i need to mention in between sentence? between start_date and end_date is this enough or i need to mask date format?
can someone help me to clear on this?

Q1 : Is this right way to pass date directly inside the procedure?
Yes.
Q3 : In begin part am using between operator, can i pass procedure parameter directly ?
Not sure what you mean by this, but your insert statement is fine. You are passing a DATE as parameter and inserting into the table.
In my opinion, all this could be done in a single INSERT..SELECT statement in pure SQL.
insert into test1
select col1, col2, col3
from main
where range_date
between TO_DATE(<date_literal>,<format mask>)
and TO_DATE(<date_literal>,<format mask>);
UPDATE Per OP's comment:
While executing this procedure, exec pro_test('10102015','30102015');
What i need to mention in between sentence? between start_date and
end_date is this enough or i need to mask date format?
'10102015' is not a DATE, it is a string literal. You must pass it as DATE, so you must either use TO_DATE with proper format mask or ANSI Date literal as you do not have any time portion. ANSI Date literal uses a fixed format 'YYYY-MM-DD'.
For example,
Using TO_DATE:
EXEC pro_test(TO_DATE('10102015','DDMMYYYY'),TO_DATE('30102015','DDMMYYYY'));
Using ANSI Date literal:
EXEC pro_test(DATE '2015-10-10', DATE '2015-10-30');

Q1. You need say procedure what type of parameters input or output(adding in or out), for your procedure it is input:
create or replace procedure pro_test(start_date in date, end_date in date)
Everything else in your procedure fine.

If you want to be extra cautious, you should namespace your pl/sql variables when they are used in a SQL statement:
create or replace procedure pro_test(start_date date, end_date date)
is
begin
insert into test1
select col1, col2, col3
from main where range_date
between pro_test.start_date and pro_test.end_date;
...
This prevents the SQL engine "capturing" the variables if their name is the same as a column in a referenced table.
Commonly you see people try to avoid this with:
create procedure my_procedure (p_customer_id number)
...
where customer_id = p_customer_id
Namespacing the variables allows you to keep a cleaner interface without the prefixing.

Related

Sql in Snowflake changing date from 106(13-APR-2020) to 105(13-04-2020)

So I have a couple date fields in a table that are formatted as (DD-MON-YYYY) and I need to convert them to (DD-MM-YYYY) The field itself is already a VARCHAR. In Snowflake, how would I make this change. Here is what I have done so far.
select to_date(end_date, 'dd-mm-yyyy') from
error: Can't parse '31-JAN-2020' as date with format 'dd-mm-yyyy'
An easy way for you to approach this is to convert your string to a date, and then back to a string in the new format, an example follows:
--create an example table to test
CREATE TEMPORARY TABLE xyz (my_dt_string VARCHAR(100));
--insert a couple records
INSERT INTO xyz VALUES ('31-JAN-2020'), ('13-APR-2020');
--test a conversion, from string to date back to string
SELECT my_dt_string,
TO_CHAR(TO_DATE(my_dt_string, 'dd-mon-yyyy'), 'dd-mm-yyyy') converted
FROM xyz;
--looks good? run the following
UPDATE xyz
SET my_dt_string = TO_CHAR(TO_DATE(my_dt_string, 'dd-mon-yyyy'), 'dd-mm-yyyy');
--take a look
select * from xyz;
I hope this helps...Rich
The to_date() function provides you the ability to convert a VARCHAR field to a DATE field, not specify the format of the date. So, you'd want to run this:
SELECT TO_DATE(end_date, 'DD-MON-YYYY');
This will output the date as a date. If you want to specify how you see a date, you have 2 choices:
First, convert it back to a VARCHAR in your preferred format:
SELECT TO_VARCHAR(TO_DATE(end_date, 'DD-MON-YYYY'),'DD-MM-YYYY');
Second, change your session parameter DATE_OUTPUT_FORMAT to be the format you'd like to see dates displayed:
ALTER SESSION SET DATE_OUTPUT_FORMAT = 'DD-MM-YYYY';
This could also be done for a user, rather than a session using:
ALTER USER SET DATE_OUTPUT_FORMAT = 'DD-MM-YYYY';

Compatible syntax for getting date of today in both SQL Server and Oracle

How can I write the following so that it works for both SQL Server and Oracle (that is, without having to comment or uncomment, depending on the environment):
SELECT col1, col2
FROM table
WHERE -- anytime today
-- date_requested = TRUNC(SYSDATE) -- Oracle
date_requested = CONVERT(date, GETDATE()) -- SQL Server
I need to compare date_requested with today's date (without time).
create a function getDate in oracle:
create or replace function getDate
return date is
l_sysdate date;
begin
select sysdate
into l_sysdate
from dual;
return l_sysdate;
end;
/
you can use it like this : select getdate() from dual
Getting current Date has different implementations in these two different databases and hence you need to use those different ways in those databases. If you are looking for workarounds to look the same, then go for a similar wrapper function and use the same in both databases.

How do I properly SELECT WHERE Effective_Date >= 'Given_Date' in a stored procedure?

I have this select statement that returns the results I'm looking for:
SELECT *
FROM Database.dbo.Table
WHERE Effective_Date >= '04/01/2014'
AND Chain = 'MCD'
I'm looking to turn this into a stored procedure with the following variables, #EffectiveDate and #Chain so that I can simply replace the date and chain to get different results. Here is the stored procedure I've made that doesn't work correctly:
CREATE PROCEDURE Database.dbo.StoredProc
#Chain VARCHAR(255),
#EffectiveDate VARCHAR(255)
AS
SELECT *
FROM Database.dbo.Table
WHERE Effective_Date >= '+#EffectiveDate+'
AND Chain = '+#Chain+'
GO
I'd like to execute this stored procedure like this:
EXEC Database.dbo.StoredProc
#PharmacyChain = N'MCD',
#EffectiveDate = N'04/01/2014'
;
GO
In this example, Table.Effective_Date is in datetime format. When I run the SELECT statement w/o the stored proc, the date comparison works fine to only select records with effective date after '04/01/2014'. However, when it's run using the variables int he stored proc, it doesn't convert the date correctly to compare. I've tried changing the EffectiveDate variable to datetime format, but still had no luck. Any help would be greatly appreciated. Thank you
Parameters should match the column datatype
#Chain VARCHAR(255) -- what is Chain?
#EffectiveDate datetime -- or date etc
And simply do this
SELECT *
FROM dbo.Table
WHERE Effective_Date >= #EffectiveDate
AND Chain = #Chain;
You don't need 3 part object names either

DB2: Construct Variable inside stored procedure with current date?

I have a stored procedure that basically updates the LASTUPDATE times of some rows and I want to have a variable inside the stored procedure that is basically: Date + " " + "20:30:00" So when the stored procedure is run it looks something like this:
update
person
set
LASTUPDATETIME='2012-06-18 20:30:00',
NAME='Mike',
where (
SOME_PK='123'
);
but if I was to run the stored procedure tomorrow it would look like so:
update
person
set
LASTUPDATETIME='2012-06-19 20:30:00',
NAME='Mike',
where (
SOME_PK='123'
);
P.S The time never changes, it should always be 20:30:00 no matter when the procedure is called, I’m just interested in the date with the format Year-Month-day.
Thanks for any help!
You can use the VARCHAR_FORMAT() function to get the current date as a string, and then append your hard coded time to it:
SELECT VARCHAR_FORMAT(CURRENT_TIMESTAMP, 'YYYY-MM-DD') || ' 20:30:00'
FROM SYSIBM.SYSDUMMY1
If you want to store the result as a TIMESTAMP data type, you can use the TIMESTAMP function:
SELECT TIMESTAMP(CURRENT_DATE, '20:30:00')
FROM SYSIBM.SYSDUMMY1
Tested on DB2 for Linux/Unix/Windows, and z/OS.

SQL syntax as parameters for a MySQL Routine?

I have the following MySQL routine:
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `getGroupOrders`(grp INT,
ord CHAR(20),
srt CHAR(4),
page INT,
count INT)
BEGIN
SELECT *
FROM `dbre`.`order_info`
WHERE username IN (SELECT `dbre`.`users`.`username`
FROM `dbre`.`users`
WHERE `dbre`.`users`.`id_group` = grp)
ORDER BY ord srt LIMIT page,count;
END
As you can see, I want to pass the ordering column and sorting as a parameters, however I get a syntax error is there a way to do this or do I have to make similar routines for each type of ordering?
I don't think this is possible in the way you try it.
You cannot use a variable to define the ORDER BY column an direction.
The only workaround I can think of is to create a prepared statement from a dynamically created string (where you can use the variables to specify the order by details) and then execute that prepared statement.
Here is an example of such a dynamic statement:
http://forums.mysql.com/read.php?98,393613,393642#msg-393642