I have a stored procedure written in Oracle. I'm using a where condition in which i'm comparing two dates. I have a strange problem where the procedure works from oracle perfectly and when run from asp.net it just returns 0. Please look below for the procedure
PROCEDURE SAMPLEPROC
(
P_DATE IN VARCHAR2,
P_COUNT OUT NUMBER
)
AS
BEGIN
SELECT count(*) INTO P_COUNT from dual where
to_date(sysdate,'dd-mm-yyyy')=to_date(P_DATE,'dd-mm-yyyy');
END;
Now, when i run the above code with below input.
INPUT : 11-02-2014
OUTPUT FROM ORACLE : COUNT=1
OUTPUT FROM ASP.NET : COUNT=0
Then, After wasting long time, My friend just changed the format of the date to dd-mm-rrrr as shown below
SELECT count(*) INTO P_COUNT from dual where
to_date(sysdate,'dd-mm-rrrr')=to_date(P_DATE,'dd-mm-rrrr')
After making the changes when we run with the same Input as above
INPUT : 11-02-2014
OUTPUT FROM ORACLE : COUNT=1
OUTPUT FROM ASP.NET : COUNT=1
Why is it like this? Why is it behaving in this way when run from ASP.NET
Please someone give me a explanation for this?
Added: From ASP.NET my date will be always coming as 11-02-2014 ie DD-MM-YYYY format and its not a Date datatype. Its just a string. The same procedure works perfectly when running from ORACLE and when sent data from Asp.net it gives me 0. I debugged it I saw that the data coming from asp.net is perfectly the same which I pass from oracle when testing.
So, I just need an explanation why it could be like this when YYYY was replaced with RRRR
when,
SELECT TO_DATE(SYSDATE,'DD-MM-YYYY') FROM DUAL;
AND
SELECT TO_DATE(SYSDATE,'DD-MM-RRRR') FROM DUAL;
returns the same data '11-02-2014'
SELECT count(*) INTO P_COUNT from dual where
trunc( sysdate) =to_date(P_DATE,'dd-mm-yyyy')
It has to be like this.
sysdate already returns a date.
I believe ur nls_date_format is different in your sqlplus and asp connection.
So, to_date(sydate) is attempt to work on some string.. which I guess is mm-dd-yyyy or dd-mm-rr(most probably) and so there could be mismatch.
SELECT value
FROM nls_session_parameters
WHERE parameter = 'NLS_DATE_FORMAT'
please run the above query in both places to catch the mismatch!
Related
I'm creating a temporary datetime table with 15 minutes increment with the following code in SSRS:
--declare #Id varchar(20), #startdate datetime,#enddate datetime
--set #ID = 'J00000041'
--set #startdate = '20210601'
--set #enddate = '20210630';
create table #tempcalendar ([dispdate] [datetime], intervaldate [datetime],intervaltime [datetime])
while #StartDate <= #EndDate+1
begin
insert into #tempcalendar values(#StartDate, convert(varchar,#StartDate,110), convert(varchar(5),#StartDate,108) )
set (#StartDate) = dateadd(minute,15,#StartDate)
end
One of the parameters doesn't appear in the Define Query Parameters dialogue box when I try to run it in SSRS and I get the message 'Must declare the scalar variable' error. I've pinpointed the issue where in the code:
set #StartDate = dateadd(minute,15,#StartDate)
I was able to perform this code in SSMS (which is where I created it initially) however I can't seem to find any information where I might have gone awry in this in SSRS. I've also tried putting parenthesis in #StartDate to show set (#StartDate) = dateadd(minute,15,#StartDate), but that didn't work.
Thank you for your help
I have seen this before when setting a variable in t-sql that is passed in from SSRS.
I think the problem is the SSRS mis-interprets the query.
I worked around the problem by setting another variable to the passed in value.
e.g.
DECLARE #StartDate2 datetime
SET #StartDate2 = #StartDate
.. then use #StartDate2 in the remainder of the query.
Also check that every instance of your variable/parameter names are exactly the same, they are case sensitive. If you pass in #X but reference #x SSRS will think this is a new parameter.
SSRS expects to be able to return metadata on the SQL in the dataset. This is how it determines the column names and data types for you. This is also why you can only have one select statement in the dataset.
You can wrap your query in a stored procedure. SSRS can call the procedure and pass in the parameters. The procedure should have one select statement that returns the results for the report to handle. The procedure can be as complex as you need it to be and abstracts that code from being embedded in the report.
Actually I was trying to get all columns within a date range in Oracle db and I am successful in doing so in SQL Developer but when I execute the same code in SQL command line interface I get an error
ORA-01843 : not a valid month
I know its an error which comes in to_date format but it gets executed in SQL developer as exactly I expected but not in SQL command line. Why does this happen??
select to_date('01/04/2020') from dual;
Even this command is not working.
I had tried all format methods for the to_date method conversion and I checked nls_date_format which shows 'DD-MM-RR'
I'd suggest you to always provide appropriate format mask. Don't rely on Oracle implicitly converting & guessing what you wanted to do.
String that looks like '01/04/20' could be anything, depending on format you used (dd/mm/yy, yy/dd/mm, mm/yy/dd ... all of that is valid, but only one is correct).
SQL> alter session set nls_Date_format = 'dd-mm-rr';
Session altered.
SQL> select to_date('01/04/2020', 'dd/mm/yyyy') result from dual;
RESULT
------------------------------
01-04-20
SQL>
i want to create in mysql an event schedule every day that check
if the current date is greater than a date stored in the database table and then call some store procedures.
Reading my WRONG mysql code you will understand that i want to do:
delimiter $
set global event_scheduler = on$
create event if not exists `end_qualifications`
on schedule
every day
do
begin
if curdate() >= (select `end_date` from `round` where `nome` =
"qualifications")
then
/* call myprocedure(params); */
end if;
end $
delimiter ;
I found here If-statement in the MySQL stored procedure for selecting data something similar, but there is not way my code work as i want.
I'm a beginner with mysql so it's possible that what i want to do can't be done.
Anyone knows how to make my code work?
I'm using MySQL client version: 5.7.25
EDITED: this is the error i get when i try to run the query
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near 'do
begin
if curdate() >= (select `end_date` from `round` where `nome` =
"q' at line 5
I don't know if it matters, but the database is empty for now.
I am trying to create a report that has three optional parameters using a command in Crystal Reports 2008. The only one I can't get to work is the date parameter (seems Oracle does not treat dates the same as SQL Server).
I want the user to be able to pick and choose which parameter they would like to use; one, two, or all three. It works beautifully when I run it in TOAD and hard code the dates, but it will not run in Crystal reports using the syntax to create the parameters.
I have done this a thousand times in a SQL server environment but can't get it to work in Oracle.
The field is type is DATE in the Oracle table.
Here is the syntax from my Report command for the 3 parameters:
AND ( ( CLIL. ITEM_TAG IN ('{?tag}') OR CLS.DESCRIPTION IN( '{?desc}')
OR trunc (CLIL.ISSUE_DATE) BETWEEN to_date ('{?StartDate}', 'mm/dd/yyyy') and to_date ('{?EndDate}', 'mm/dd/yyyy' )))
Crystal reports doesn't accept the Oracle syntax.. you need to use the functions provided by the CR to do manuplations of dates... and then use that in CR.
Create a Start Date and End Date as date parameters in CR and then use those.
(CLIL.ISSUE_DATE) >= {?StartDate} and (CLIL.ISSUE_DATE) < {?EndDate}
Here start date and end date are Date datatype parameters.
if (CLIL.ISSUE_DATE) is a datetime parameter then use the function Cdate provided by the crystal.
I need to insert several rows into a SQL Server database based on Start Date and End Date textboxes.
E.G. tbStartDate.Text = "25/12/2012" and tbEndDate.Text = "29/12/2012" therefore I need to insert individual rows for the following dates:
25/12/2012
26/12/2012
27/12/2012
28/12/2012
29/12/2012
Please can you help me with the necessary T-SQL to achieve this?
As always there are a few ways. Here are some of them:
You can write code in your app that loops through the days and inserts a single record per day. (generally the worst design)
You can call some SQL script to do it all in the database.
You can wrap up your SQL script in a stored procedure and pass in the start and end date and get the stored procedure to do it for you.
You can cross join to a pre existing tally table and use it to generate your records.
If you can provide
-the version of SQL Server that you're using
-what the table looks like
-whether you're using C# or VB
then we can help further as it can be difficult to pass dates into databases. It can be particularly difficult if you do not validate them.
Anyway here is option 3 for you.
CREATE PROC dbo.t_test
#StartDate DATETIME,
#EndDate DATETIME
AS
WHILE #StartDate <= #EndDate
BEGIN
INSERT INTO YourTable(YourDateField) VALUES (#StartDate)
SET #StartDate = DATEADD(d,1,#StartDate)
END
Then you need to call this stored procedure (called dbo.t_test) from ASP.Net and pass in your two date parametes as dates.
Declare #Startdate datetime
Select #Startdate='20121025'
While #Startdate<='20121029'
begin
if not exists(select * from dummy where DATE=#Startdate)
insert into dummy (date) values (#Startdate)
set #Startdate=#Startdate + 1
end;