To_date
Hi Guys,
I am new to SQL so could do with some help writting a query well the to date to be exact.
select orders.order_id from orders where order_date between to_date('10-NOV-12', 'DD-MON-YY') and to_date('01-JAN-12', 'DD-MON-YY') ;
The only problem is the date format is as follows: 12-NOV-12 17.51.16.000000000
Any ideas as to why my query is not working?? Any help would be hugely appreciated.
Cheers
Also consider using 4 digit years e.g.
to_date('10-NOV-2012', 'DD-MON-YYYY')otherwise you could end up with the old year 2000 issue which most programmers have the sense to avoid now, after all this time.
By just specifying 12 with a 2 digit year format you can end up with a century you weren't expecting...
SQL> select to_date('10-NOV-99','DD-MON-YY') from dual;
TO_DATE('10-NOV-99',
10-NOV-2099 00:00:00in this case you would probably have expected 1999.
Similar Messages
-
** How to use TO_DATE function in Stored Proc. for JDBC in ABAP-XSL mapping
Hi friends,
I use ABAP-XSL mapping to insert records in Oracle table. My Sender is File and receiver is JDBC. We use Oracle 10g database. All fields in table are VARCHAR2 except one field; this is having type 'DATE'.
I use Stored procedure to update the records in table. I have converted my string into date using the Oracle TO_DATE function. But, when I use this format, it throws an error in the Receiver CC. (But, the message is processed successfully in SXMB_MONI).
The input format I formed like below:
<X_EMP_START_DT hasQuot="No" isInput="1" type="DATE">
Value in Payload is like below.
<X_EMP_START_DT hasQuot="No" isInput="1" type="DATE">TO_DATE('18-11-1991','DD-MM-YYYY')</X_EMP_START_DT>
Error in CC comes as below:
Error processing request in sax parser: Error when executing statement for table/stored proc. 'SP_EMP_DETAILS' (structure 'STATEMENT'): java.lang.NumberFormatException: For input string: "TO_DATE('18"
Friends, I have tried, but unable to find the correct solution to insert.
Kindly help me to solve this issue.
Kind Regards,
Jegathees P.
(But, the same is working fine if we use direct method in ABAP-XSL ie. not thru Stored Procedure)Hi Sinha,
Thanks for your reply.
I used the syntax
<xsl:call-template name="date:format-date">
<xsl:with-param name="date-time" select="string" />
<xsl:with-param name="pattern" select="string" />
</xsl:call-template>
in my Abap XSL. But, its not working correctly. The problem is 'href' function to import "date.xsl" in my XSLT is not able to do that. The system throws an error. Moreover, it is not able to write the command 'extension-element-prefixes' in my <xsl:stylesheet namespace>
May be I am not able to understand how to use this.
Anyway, I solved this problem by handling date conversion inside Oracle Stored Procedure. Now, its working fine.
Thank you. -
The to_date function doesn't work ?
Hello
I don't know why my to_date function doesn't work on my pc.
my statement is pretty complex so i just tried simple one
select to_date('10-Jan-2006','dd-mon-yyyy') from dual;
but even this one doesn't work it says it is invalid month
howcome?
is it because my Windows XP (not english version) doesn't recognise month Jan?
i tried it another place it worked
so is there any language pack / patch i need to install? ?
so does OS matters?
or how to fix this..please help meBy default SQL Developer picks up it's language settings up from (on Windows) the Regional and Language Options. You can see what it has set by querying the NLS_SESSION_PARAMETERS view.
Assuming you don't want to change to using DD-MM-YYYY formats for your months or change your regional settings on your PC, you need to tell SQL Developer which language you want, which you can do by adding the following lines to the file sqldeveloper\jdev\bin\sqldeveloper.conf:
AddVMOption -Duser.language=en
AddVMOption -Duser.country=US -
A view, function and TO_DATE causing an error.
I have the following statement which calls a view, VW_DIST_RPT_WORK_LIST which in turn calls a function which returns either 'Null' or a date string e.g. '07 Oct 2003' as a VARCHAR2 (alias PROJECTED_DELIVERY_DATE).
Statement:
SELECT CUSTOMER_NAME, PROTOCOL_REFERENCE, SHIPPING_REFERENCE, CUSTOMER_REFERENCE, COUNTRY, PROJECTED_DELIVERY_DATE, STATUS, NOTES,
TO_DATE(PROJECTED_DELIVERY_DATE)
FROM VW_DIST_RPT_WORK_LIST
WHERE EXPECTED_DESP_DT IS NOT NULL
AND UPPER(PROJECTED_DELIVERY_DATE) NOT LIKE('NULL%')
AND EXPECTED_DESP_DT <= TO_DATE('07/10/2003', 'DD/MM/YYYY')
AND TO_DATE(PROJECTED_DELIVERY_DATE) <= TO_DATE('31/12/2003', 'DD/MM/YYYY') --< Problem here
I need to be able to specify a date filter on the PROJECTED_DELIVERY_DATE field and hence used the TO_DATE(PROJECTED_DELIVERY_DATE) <= TO_DATE('31/12/2003', 'DD/MM/YYYY') but this is generating an ORA-01858: a non-numeric character was found where a numeric character was expected.
I think the problem lies with the fact that this field can contain 'Null' which cannot be converted to a date using TO_DATE. I've tried adding a NOT LIKE ('NULL%') statement to catch any nulls which may be creeping in bu this doesn't solve the problem.
I've added TO_DATE(PROJECTED_DELIVERY_DATE) to the select above to determine if the nulls are being caught and if the TO_DATE in performing the conversion correctly which it is on both counts.
Any ideas anyone ?The answer provided above by Monika will work for this situation. However, you should seriously think whether you should be using a string for date datatype. Ideally, you should rewrite the function that returns PROJECTED_DELIVERY_DATE and change the return type to DATE. The least you should do is to return NULL (instead of the string 'NULL') from the function. Oracle handles nulls perfectly, there is no reason you should write code to handle nulls;
One more thing. Looking at the type of error you are receiving, it seems that you are using rule based optimizer. Why do I think so? Because, in rule based optimizer, the conditions are evaluated in a specific order (viz, bottoms-up for AND clauses). To show this, look at the following simple demonstration. I did this in Oracle 8.1.6 (also in 9.2.0.4.0 on Windows).
-- Check the database version
select * from v$version;
BANNER
Oracle8i Enterprise Edition Release 8.1.6.1.0 - Production
PL/SQL Release 8.1.6.1.0 - Production
CORE 8.1.6.0.0 Production
TNS for Solaris: Version 8.1.6.0.0 - Production
NLSRTL Version 3.4.0.0.0 - Production
-- Create the test table
create table test (a number(2));
insert into test(a) values (0);
insert into test(a) values (1);
insert into test(a) values (2);
insert into test(a) values (3);
insert into test(a) values (4);
insert into test(a) values (5);
insert into test(a) values (6);
insert into test(a) values (7);
commit;
-- See that I have not analyzed the table. This will make use of RULE based optimizer
select * from test
where a > 0
and 1/a < .25;
and 1/a < .25
ERROR at line 3:
ORA-01476: divisor is equal to zero
-- Look at the query clause. Even though I specifically asked for records where a is positive
-- the evaluation path of rule based optimizer started at the bottom and as it evaluated the
-- first row with a=0, and caused an error.
-- Now look at the query below. I just re-arranged the conditions so that a > 0 is evaluated
-- first. As a result, the row with a=0 is ignored and the query executes without any problem.
select * from test
where 1/a < .25
and a > 0;
A
5
6
7
-- Now I analyze the table to create statistics. This will make the query use the
-- cost based optimizer (since optimizer goal is set to CHOOSE)
analyze table test compute statistics;
Table analyzed.
-- Now I issue the erring query. See it executes without any problem. This indicates that
-- the cost based optimizer was intelligent enough to evaluate the proper path instead of
-- looking only at the syntax.
select * from test
where a > 0
and 1/a < .25;
A
5
6
7
Does the above example seem familiar to your case? Even though you had the AND UPPER(PROJECTED_DELIVERY_DATE) NOT LIKE('NULL%') in your query, a record with PROJECTED_DELIVERY_DATE = 'NULL' was evaluated first and that caused the error.
Summary
1. Use dates for dates and strings for strings
2. Use cost based optimizer
Thanks
Suman -
How to convert character value to_date?
I want to change the time (hours) in sysdate to '10' and then save this new date into a date column for that i've written the following command
(TO_CHAR(SYSDATE,'DD-MON-RRRR')||' 10:'||TO_CHAR(SYSDATE,'MI:SS AM'))this command will remove the actual hours in sysdate and change it with the value '10'. but the result of this command is a character value. and i want a date value to insert into a date column. for that i've converted this command to_date as follows
TO_DATE(TO_CHAR(SYSDATE,'DD-MON-RRRR')||' 10:'||TO_CHAR(SYSDATE,'MI:SS AM'))but it gives the following error
ORA-01830: date format picture ends before converting entire input string
how to do this?Gul wrote:
I want to change the time (hours) in sysdate to '10' and then save this new date into a date column for that i've written the following command
(TO_CHAR(SYSDATE,'DD-MON-RRRR')||' 10:'||TO_CHAR(SYSDATE,'MI:SS AM'))this command will remove the actual hours in sysdate and change it with the value '10'. but the result of this command is a character value. and i want a date value to insert into a date column. for that i've converted this command to_date as follows
TO_DATE(TO_CHAR(SYSDATE,'DD-MON-RRRR')||' 10:'||TO_CHAR(SYSDATE,'MI:SS AM'))but it gives the following error
ORA-01830: date format picture ends before converting entire input string
how to do this?TO_DATE(TO_CHAR(SYSDATE,'DD-MON-YYYY')||' 10:'||TO_CHAR(SYSDATE,'MI:SS AM'),'DD-MON-YYYY HH:MI:SS AM') -
To_Date function in the Where Clause
Hello All,
I'm having an issue using the to_date function that has me quite perplexed.
I have two varchar2 fields, one with a date value in the format Mon, DD YYYY, the other has a time value in the format HH:MI PM.
When I run my query one of the columns I retrieve looks like this TO_DATE (d4.adate || e4.atime, 'Mon DD, YYYYHH:MI PM'). The two fields are concatenated together and converted to a date. This works fine.
My problem occurs when I attempt to apply the same logic to the where clause of the aforementioned query. e.g. when I add the following criteria to my query and TO_DATE (d4.adate || e4.atime, 'Mon DD, YYYYHH:MI PM') <= sysdate I get an ORA-01843: not a valid month error.
To further illustrate my problem here are the two queries:
Select d4.adate, e4.atime, TO_DATE (d4.adate || e4.atime, 'Mon DD, YYYYHH:MI PM')
from ....
where ....
The above query works.
Select d4.adate, e4.atime, TO_DATE (d4.adate || e4.atime, 'Mon DD, YYYYHH:MI PM')
from ....
where ....
and TO_DATE (d4.adate || e4.atime, 'Mon DD, YYYYHH:MI PM') <= sysdate
The second query does not work.
The tables used and the limiting criteria are identical, except for the last one.
Does anyone have any ideas why this could be happening.
erHello,
Check this out. It does work. Do cut n paste sample
data from your tables.
SQL> desc test
Name Null? Type
ID NUMBER
DDATE VARCHAR2(20)
DTIME VARCHAR2(20)
SQL> select * from test;
ID DDATE DTIME
1 Jan, 10 2006 12:32 PM
2 Mar, 11 2005 07:10 AM
3 Apr, 13 2006 03:12 AM
4 Nov, 15 2003 11:22 PM
5 Dec, 20 2005 09:12 AM
6 Oct, 30 2006 10:00 AM
7 Jan, 10 2006 12:32 PM
8 Apr, 11 2005 07:10 AM
9 May, 13 2006 03:12 AM
10 Sep, 15 2003 11:22 PM
11 Oct, 20 2005 09:12 AM
12 Dec, 30 2006 10:00 AM
12 rows selected.
SQL> select id, ddate, dtime,
2 to_date(ddate||dtime,'Mon, DD YYYYHH:MI PM') AA,
A,
3 to_char(to_date(ddate||dtime,'Mon, DD YYYYHH:MI
MI PM'),'Mon, DD YYYYHH:MI PM') BB
4 from test;
ID DDATE DTIME
DTIME AA BB
1 Jan, 10 2006 12:32 PM
12:32 PM 10-JAN-06 Jan, 10 200612:32 PM
2 Mar, 11 2005 07:10 AM
07:10 AM 11-MAR-05 Mar, 11 200507:10 AM
3 Apr, 13 2006 03:12 AM
03:12 AM 13-APR-06 Apr, 13 200603:12 AM
4 Nov, 15 2003 11:22 PM
11:22 PM 15-NOV-03 Nov, 15 200311:22 PM
5 Dec, 20 2005 09:12 AM
09:12 AM 20-DEC-05 Dec, 20 200509:12 AM
6 Oct, 30 2006 10:00 AM
10:00 AM 30-OCT-06 Oct, 30 200610:00 AM
7 Jan, 10 2006 12:32 PM
12:32 PM 10-JAN-06 Jan, 10 200612:32 PM
8 Apr, 11 2005 07:10 AM
07:10 AM 11-APR-05 Apr, 11 200507:10 AM
9 May, 13 2006 03:12 AM
03:12 AM 13-MAY-06 May, 13 200603:12 AM
10 Sep, 15 2003 11:22 PM
11:22 PM 15-SEP-03 Sep, 15 200311:22 PM
11 Oct, 20 2005 09:12 AM
09:12 AM 20-OCT-05 Oct, 20 200509:12 AM
12 Dec, 30 2006 10:00 AM
10:00 AM 30-DEC-06 Dec, 30 200610:00 AM
12 rows selected.
SQL> select id, ddate, dtime,
to_date(ddate||dtime,'Mon, DD YYYYHH:MI PM')
2 from test
3 where id > 3
4 and to_date(ddate||dtime,'Mon, DD YYYYHH:MI PM')
') <= trunc(sysdate);
ID DDATE DTIME
DTIME TO_DATE(D
4 Nov, 15 2003 11:22 PM
11:22 PM 15-NOV-03
5 Dec, 20 2005 09:12 AM
09:12 AM 20-DEC-05
7 Jan, 10 2006 12:32 PM
12:32 PM 10-JAN-06
8 Apr, 11 2005 07:10 AM
07:10 AM 11-APR-05
10 Sep, 15 2003 11:22 PM
11:22 PM 15-SEP-03
11 Oct, 20 2005 09:12 AM
09:12 AM 20-OCT-05
6 rows selected.
SQL> select id, ddate, dtime,
to_date(ddate||dtime,'Mon, DD YYYYHH:MI PM')
2 from test
3 where id > 3
4 and to_date(ddate||dtime,'Mon, DD YYYYHH:MI PM')
') <= sysdate;
ID DDATE DTIME
DTIME TO_DATE(D
4 Nov, 15 2003 11:22 PM
11:22 PM 15-NOV-03
5 Dec, 20 2005 09:12 AM
09:12 AM 20-DEC-05
7 Jan, 10 2006 12:32 PM
12:32 PM 10-JAN-06
8 Apr, 11 2005 07:10 AM
07:10 AM 11-APR-05
10 Sep, 15 2003 11:22 PM
11:22 PM 15-SEP-03
11 Oct, 20 2005 09:12 AM
09:12 AM 20-OCT-05
6 rows selected.
-SriSorry Sri, but I fail to see what you mean. How is what you're doing any different than what I'm doing? -
What is the difference between TO_CHAR and TO_DATE()?
Hi everybody,
i am facing a problem in my system.It is quite urgent, can you explain me "What is the difference between TO_CHAR and TO_DATE()?".
According to user's requirement, they need to generate a code with format "YYMRRR".
YY = year of current year
M = month of current month (IF M >=10 'A' ,M >=11 'B' , M >=10 'C')
RRR = sequence number
Example: we have table USER(USER_ID , USER_NAME , USER_CODE)
EX: SYSDATE = "05-29-2012" MM-DD-YYYY
IF 10
ROW USER_ID , USER_NAME , USER_CODE
1- UID01 , AAAAA , 125001
2- UID02 , AAAAA , 125002
10- UID010 , AAAAA , 12A010
This is the original Script code. But This script runs very well at my Local. Right format. But it just happens wrong format on production.
12A010 (Right) => 11C010 (Wrong).
SELECT TO_CHAR(SYSDATE, 'YY') || DECODE( TO_CHAR(SYSDATE, 'MM'),'01','1', '02','2', '03','3', '04','4', '05','5', '06','6', '07','7', '08','8','09','9', '10','A', '11','B', '12','C') || NVL(SUBSTR(MAX(USER_CODE), 4, 3), '000') USER_CODE FROM TVC_VSL_SCH
WHERE TO_CHAR(SYSDATE,'YY') = SUBSTR(USER_CODE,0,2)
AND TO_CHAR(SYSDATE,'MM') = DECODE(SUBSTR(USER_CODE,3,1),'1','01',
'2','02', '3','03', '4','04', '5','05',
'6','06', '7','07', '8','08', '9','09',
'A','10', 'B','11', 'C','12')
I want to know "What is the difference between TO_CHAR and TO_DATE()?".try to use following select
with t as
(select TO_CHAR(SYSDATE, 'YY') ||
DECODE(TO_CHAR(SYSDATE, 'MM'),
'01', '1',
'02', '2',
'03', '3',
'04', '4',
'05', '5',
'06', '6',
'07', '7',
'08', '8',
'09', '9',
'10', 'A',
'11', 'B',
'12', 'C') as code
from dual)
SELECT t.code || NVL(SUBSTR(MAX(USER_CODE), 4, 3), '000') USER_CODE
FROM TVC_VSL_SCH
WHERE SUBSTR(USER_CODE, 1, 3) = t.codeand yes you need check time on your prodaction server
good luck
Edited by: Galbarad on May 29, 2012 3:56 AM -
How to use quarter format in "to_date()" function?
Hi,
I'm trying to use to_date() function with quarter format. How do I specify the format?
For example to convert '2002 quarter 1' using the to_date function.
I tried to_date('20021', 'YYYYQ'), and I got ORA-01820 cannot appear in date input format.
Please help, thank you!!!You cannot use Q when inputting data, only when retrieving data. When inputting, you must enter a valid date. Oracle has to store a date and if you just enter a quarter, it can't figure out what date to store. See the examples below:
SQL> -- test table:
SQL> CREATE TABLE test_table
2 (test_date DATE)
3 /
Table created.
SQL> -- the wrong way to input:
SQL> INSERT INTO test_table (test_date)
2 VALUES (TO_DATE ('2002-1', 'YYYY-Q'))
3 /
VALUES (TO_DATE ('2002-1', 'YYYY-Q'))
ERROR at line 2:
ORA-01820: format code cannot appear in date input format
SQL> -- the right way to input:
SQL> INSERT INTO test_table (test_date)
2 VALUES (TO_DATE ('27-OCT-2002', 'DD-MON-YYYY'))
3 /
1 row created.
SQL> -- to output:
SQL> COLUMN "Year and Quarter" FORMAT A16
SQL> SELECT test_date,
2 TO_CHAR (test_date, 'YYYY-Q') AS "Year and Quarter"
3 FROM test_table
4 /
TEST_DATE Year and Quarter
27-OCT-2002 2002-4
1 row selected. -
How to use TO_DATE function in OBI having MSSQL DB
Hello Experts,
I am facing a problem plz help me.....
I've just created an business application using Oracle BI Administration Tool of Oracle BI Server version 10.1.3.4.1,with oracle DB.
But currently we migrate from Oracle to MSSQL Server. Due to which I am facing problem regarding Date Column (TO_DATE(), timestamp).
As I have created a report with 2 fields
Incident_Number, TimeStampAdd(SQL_TSI_SECOND,Incident_D.Reported_Date,timestamp '1970-01-01 00:00:00')
Incident_D.Reported_Date contains integer values which stores total seconds from date 01/01/1970 00:00:00 . I want to convert these seconds from 1970 to datetime.
when I ran the report with Oracle source it works fine, But with MSSQL as Source it gives me following error ...........
Odbc driver returned an error (SQLExecDirectW).
Error Details
Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P
State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 16001] ODBC error state: 37000 code: 8180 message: [Microsoft][SQL Server Native Client 10.0][SQL Server]Statement(s) could not be prepared.. [nQSError: 16001] ODBC error state: 37000 code: 195 message: [Microsoft][SQL Server Native Client 10.0][SQL Server]'TO_DATE' is not a recognized built-in function name.. [nQSError: 16002] Cannot obtain number of columns for the query result. (HY000)
SQL Issued: SELECT Incidents."Incident Number" saw_0, TIMESTAMPADD(SQL_TSI_SECOND, Incidents."Reported Date", timestamp '1970-01-01 00:00:00') saw_1
FROM "ITSM Information" ORDER BY saw_0, saw_1
The Sql query log generated is as follow :
select distinct T728.INCIDENT_NUMBER as c1,
( CAST(TO_DATE('1970-01-01 00:00:00' , 'YYYY-MM-DD HH24:MI:SS') as DATE) + ( T728.REPORTED_DATE / 86400 ) ) as c2
from
INCIDENT_D T728
order by c1, c2
+++Administrator:ce0000:ce0002:----2010-11-30 14:51:38
-------------------- Query Status: Query Failed: [nQSError: 16001] ODBC error state: 37000 code: 8180 message: [Microsoft][SQL Server Native Client 10.0][SQL Server]Statement(s) could not be prepared..
[nQSError: 16001] ODBC error state: 37000 code: 195 message: [Microsoft][SQL Server Native Client 10.0][SQL Server]'TO_DATE' is not a recognized built-in function name..
[nQSError: 16002] Cannot obtain number of columns for the query result.
Please Help me........
Thanks and Regards,
MJI will give a try at it,TO_DATE is not recognised function by the SQLserver
( CAST(TO_DATE('1970-01-01 00:00:00' , 'YYYY-MM-DD HH24:MI:SS') as DATE)Instead of it use to_Char and try it like this so on cast(year(your_date) as char) || '-'||cast(month(your_date) as char) OR use this nice blog by saichand http://varanasisaichand.blogspot.com/2010/05/evaluate-function.html
it says like EVALUATE('TO_CHAR(%1,%2)' ,"Dim- Date".Start Date,'YYYY-MM-DD HH24:MI:SS')
Hope it helps you.Mark points.
By,
KK -
To_date function not fetching the desired result.....
Hi Gurus,
I get varied results from the below 2 queries, I am pessimistic about using to_char instead prefer using to_date, but query doesn't seem to fetch the complete data while using the to_date but with to_char it works. Please can someone make this to_date used query below work for me.
Main table structure:
Column Name ID Pk Null? Data Type Default Histogram Encryption Alg Salt
MINISTRY_CODE 1 Y VARCHAR2 (15 Byte) Yes
BILL_MONTH 2 Y DATE Yes
CUBIC 3 Y NUMBER Yes
GALLONS 4 Y NUMBER Yes
AMOUNTS 5 Y NUMBER Yes
MTR_TYPE 6 Y CHAR (1 Byte) Yes 1st query:
SELECT ministry_code,
DECODE(mtr_type, 'C','Billed Cubic',
'G','Billed Gallons',
'A','Billed OMR',
'R','Collected',
'U','Total Unpaid Bills') mtr_type,
SUM(CASE
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_char(bill_month,'MMYYYY') = '012011' AND mtr_type = 'C' THEN cubic
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_char(bill_month,'MMYYYY') = '012011' AND mtr_type = 'G' THEN (gallons)
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_char(bill_month,'MMYYYY') = '012011' AND mtr_type = 'A' THEN amounts
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_char(bill_month,'MMYYYY') = '022011' AND mtr_type = 'R' THEN amounts
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_char(bill_month,'MMYYYY') <= '022011' AND mtr_type = 'U' THEN amounts ELSE 0 END) January
FROM tmp_paew_month_gafu1 WHERE ministry_code IN
(SELECT DISTINCT ministry_code FROM tmp_paew_month_gafu GROUP BY ministry_code)
GROUP BY ministry_code,mtr_type
ORDER BY 1,3 DESCSAMPLE OUTPUT:
MINISTRY_CODE MTR_TYPE JANUARY
001 Billed Gallons 5326252
001 Billed Cubic 24210.2363636364
001 Total Unpaid Bills 16402.5
001 Billed OMR 13287.36
001 Collected 10410.508
002 Total Unpaid Bills 12089.99
002 Billed Cubic 0
002 Billed OMR 0
002 Billed Gallons 0
002 Collected 0
003 Total Unpaid Bills 27418.711
2nd query:
SELECT ministry_code,
DECODE(mtr_type, 'C','Billed Cubic',
'G','Billed Gallons',
'A','Billed OMR',
'R','Collected',
'U','Total Unpaid Bills') mtr_type,
SUM(CASE
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_char(bill_month) = to_date('012011','MMYYYY') AND mtr_type = 'C' THEN cubic
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_date(bill_month) = to_date('012011','MMYYYY') AND mtr_type = 'G' THEN (gallons)
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_date(bill_month) = to_date('012011','MMYYYY') AND mtr_type = 'A' THEN amounts
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_date(bill_month) = to_date('022011','MMYYYY') AND mtr_type = 'R' THEN amounts
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_date(bill_month) <= to_date('022011','MMYYYY') AND mtr_type = 'U' THEN amounts ELSE 0 END) January
FROM tmp_paew_month_gafu1 WHERE ministry_code IN
(SELECT DISTINCT ministry_code FROM tmp_paew_month_gafu GROUP BY ministry_code)
GROUP BY ministry_code,mtr_type
ORDER BY 1,3 DESCSample OUTPUT:
MINISTRY_CODE MTR_TYPE JANUARY
001 Total Unpaid Bills 106062.002
001 Billed OMR 0
001 Billed Cubic 0
001 Billed Gallons 0
001 Collected 0
002 Total Unpaid Bills 42023.243
002 Collected 0
002 Billed Gallons 0
002 Billed Cubic 0
002 Billed OMR 0
003 Total Unpaid Bills 133260.435What is the sense of to_date(bill_month) in the second query?
bill_month is already a date. to_date function will expect a character parameter and convert it in a date value.
So what will happen? First the bill_month will be implicitely converted to a character value.
Second this character value will be converted back to a date. You gave no format string. So both conversions will be made will your setting of nls_date_format.
I can only guess what you really want. Perhaps you want to trunc the bill_month with trunc(bill_month,'MM').
Please give us
- table create script
- insert statements for test data
- required output for these test data
- description of your requirements
Edited by: hm on 22.11.2012 23:16 -
Equivalent of to_date function in Ms SQL and using it in Evaluate function
Hi,
I am trying to find out a function in MS SQL which is equivalent to to_date function in oracle. Please find below the advanced filter i am trying to use in OBIEE.
Evaluate('to_date(%1,%2)' as date ,SUBSTRING(TIMES.CALENDAR_MONTH_NAME FROM 1 FOR 3)||'-'||cast(TIMES.CALENDAR_YEAR as char(4)),'MON-YYYY')>=Evaluate('to_date(%1,%2)' as date,'@{pv_mth}'||'@{pv_yr}','MON-YYYY') and Evaluate('to_date(%1,%2)' as date ,SUBSTRING(TIMES.CALENDAR_MONTH_NAME FROM 1 FOR 3)||'-'||cast(TIMES.CALENDAR_YEAR as char(4)),'MON-YYYY') <=timestampadd(sql_tsi_month,4,Evaluate('to_date(%1,%2)' as date,'@{pv_mth}'||'@{pv_yr}','MON-YYYY'))
The statement above works fines with oracle DB with to_date function. The same statement throws an error with MS SQL since to_date is not a built in function.
With MS SQL I tried with CAST, not sure how to pass parameters %1 and %2.
Please help me how to use Evaluate function and passing parameters along with to_date funtion in MS SQL.
Regards!
RRHi,
please refer to this thread for useful information on using to_char and to_date functions of oracle in MS SQL server:
http://database.ittoolbox.com/groups/technical-functional/sql-server-l/how-to-write-to-to_char-and-to_date-sql-server-351831
Hope this helps.
Thanks,
-Amith. -
SQL Developer: to_date function..
Hi..
Can anyone tell me what is wrong with the following:
select to_date('02-Jun-2008 12:10:00', 'DD-MON-RR:HH24:MI:SS') - 2 "Subtract 2 days" from dual;
I get the following result:
31-MAY-08
I thought the format mask supplied would generate the result in the requested format with HH:MI:SS but, as you can see...it doesn't. The nls_date_format is DD-MON-RR, but that shoudn't matter as I have supplied an alternate mask to use (should it..?).
It's just bugging me..using SQL Developer Version 1.5.5
Thanks,
RoryHi Roy,
I'm sorry, but You're wrong.
The format mask you provided is only to transform a text into date format. Not for displaying a date. So the default format mask used to display the date is the one defined in NLD_DATE_FORMAT.
If you want display date in 'DD-MON-RR:HH24:MI:SS' format you should either modify the NLS_DATE_FORMAT or use a TO_CHAR in your select ... from dual;
Pierre. -
Integer parameters not acceptible in WHERE clause with TO_DATE function.
Please tell me why the following query is not acceptible in a BI Publisher (11.1.1.5) data set.
SELECT * FROM all_tables
WHERE
LAST_ANALYZED >= TO_DATE(:yyyy || '-' || :startmo || '-' || :startday, 'YYYY-MM-DD')
AND LAST_ANALYZED < TO_DATE(:yyyy || '-' || :endmo || '-' || :endday, 'YYYY-MM-DD')
Attempting to save the query results in:
"ORA-01858: a non-numeric character was found where a numeric was expected"
It doesn't work even if you surround the parameters with TO_NUMBER().
The query clearly works in SQL Developer.Also, neither of the following work, even though they both work in SQL Developer:
SELECT table_name, last_analyzed
FROM all_tables
WHERE
LAST_ANALYZED >= (TO_DATE(:start_date, 'MM-DD-YYYY') + 1)
SELECT table_name, last_analyzed
FROM all_tables
WHERE
LAST_ANALYZED >= (TO_DATE(:start_date, 'MM-DD-YYYY') + INTERVAL '1' DAY)
These don't work in BIP even when the start_date parameter has a format set to the default MM-dd-yyyy. So to sum up, I can't use date parameters, nor can I use integers passed as date parts. What the fuck am I supposed to do?
After four years, your fucking date parameters still don't work! Fuck off Oracle. I'm going home, frustrated with your garbage as usual. -
TO_DATE Function Is not working
Hi Friends,
select * from view_date where to_date(my_date) between '01-dec-06' and '31-dec-06';
This query is not responding...
I have the the date field in character in the view so i need to convert it to date ,so that iam using to_date function and its not responding.
Without using to_date iam getting the results but not the exact results,it includes all the record sets.
Help me in thisTO_DATE converts a string to a date. TO_CHAR converts a date to a string. What is my_date?
Edit: Just realised you said "my_date" is a string.
Try
select * from view_date
where TO_DATE(my_date,'???')
between TO_DATE('01-dec-2006','dd-mon-yyyy') and TO_DATE('31-dec-2006','dd-mon-yyyy');or if you are dealing with literals you can use the more compact ANSI format:
select * from view_date
where TO_DATE(my_date,'???') between DATE '2006-12-01' and DATE '2006-12-31';Avoid 2-digit years. You have no idea the problems that caused a few years back.
Message was edited by:
William Robertson
Oops, typed stuff the wrong way around, need coffee... -
Bad performance due to the use of AGO and TO_DATE time series functions
Hi all,
I'm building an OBI EE Project on top of a 1TB DW, and i'm facing major performance problems due to the use of the AGO and TO_DATE time series functions in some of the Metrics included on the reports. I discovered that when a report with one of those metrics is submited to the DB, the resulting query/explain plan is just awful!... Apparently OBI EE is asking the DB to send everything it needs to do the calculations itself. The CPU cost goes to the roof!
I've tried new indexes, updated statistics, MV's, but the result remains the same, i.e., if you happen to use AGO or TO_DATE in the report you'll get lousy query time...
Please advise, if you have come across the same problem.
Thanks in advance.Nico,
Combining the solution to view the data in dense form (http://gerardnico.com/wiki/dat/obiee/bi_server/design/obiee_densification_design_preservation_dimension), and the use of the lag function (http://gerardnico.com/wiki/dat/obiee/presentation_service/obiee_period_to_period_lag_lead_function) appears to be the best solution for us.
Thanks very much. -
TO_DATE and bind variables in SQLJ
Hi all
I need to convert a date, in string format using TO_DATE in an INSERT INTO contained in a sqlj class. The reason for this is I am reading in data from a pipe delimited file created by a MUMPS application. (Don't ask:)).
This works fine if I use the date in the function during testing i.e
:SOMEVAL,
TO_DATE('21-04-2003',DD-MON-YYYY'),
:SOMEOTHERVAL,
I though want to use a bind variable containing the date read in from the file.
:SOMEVAL,
TO_DATE(':DOB','DD-MON-YYYY'),
:SOMEOTHERVAL,
When I do the second example, I get a missing comma error at run time. Am I asking SQLJ to do somthing it can't or do I have the syntax wrong... I hope its the latter. I can find examples of both of the above but not combined in one statement.
NeilI believe you merely need to get rid of the quotes around :DOB in your second statement.
Justin
Maybe you are looking for
-
Airport Extreme- PC users cannot connect to security enabled Wifi
I have an Airport Express and have setup a security enabled Wifi network. It works perfectly for me, but my PC user roomates cannot access it. I don't know much about the difference between WPA and other settings, so maybe I need to change the secu
-
DNG will not open after Maverick osx
After installing Maverick osx, many dng do not open and I get the message: Could not complete your request because the file format module can not parse the file. The error occurs randomly on files that have always worked well. I already tried to rein
-
Problem with opening a Work Book
Dear Guru's I am not able to open a workbook in BEx Analyser(3.x v), There are 4 Works under same Query Eg:- MTa, MTb, MTc, MTd. MTc is not opening and remaining 3 are fine. I Would be great if some one help me out of this problém. Thanks in advance.
-
Using BT HomeHub as a print server
I have an epson printer which I have been using for a couple of weeks with my new mac, and I also have to share it with my brother who uses XP. I connected the printer to the Hub (BT HomeHub v1.5) USB port and managed to make it print through windows
-
My adobe is not working with Windows 7 it says "program not working"
My adobe does not worked with I have tried 9 & 10 both versions. It does not seem to work with Windows 7.