I've attempted a case when statement - oracle

I have attempted to use
case
when cc.create_date_time < 04/17/2019 '
then 'counted'
when cc.create_date_time > 04/17/2019
then 'need counted'
else null
end as TIME_GAP
with no luck - whatever is on the first line as in the 'counted' returns for all data even though there are results done before that date that should say need counted... how do I fix this...?
If something was counted 04/17/2019 and later its good if it has been counted before that date then I need it to tell me that... thanks

If CREATE_DATE_TIME column's datatype is DATE (it should be), why are you comparing it to strings? '04/17/2019' is a string. Either use DATE literal, or convert that string to date with the TO_DATE function and appropriate format mask, e.g.
case when cc.create_date_time < date '2019-04-17' then ...
or
case when cc.create_date_time < to_date('04/17/2019', 'mm/dd/yyyy') then ...

Related

ORA-01722 invalid number on using the expression in select and while joining

CASE WHEN (r.code_value4 = 0 AND LENGTH(ltrim(rtrim(xx.AFFILIATE_CODE))) > 0) AND
r_intercompany.code_value1 is not null AND
ltrim(rtrim(xx.AFFILIATE_CODE)) <> (CASE WHEN xx.COMPANY_CODE_JE_EXCEPTION_FLAG = 1
THEN r.code_value3 ELSE r.code_value1 END)
THEN r_intercompany.code_value1
ELSE NVL(r_mga_acct.code_value2, xx.ACCOUNT_NUMBER) END
I have view in which the above part of the sql is being used in select statement and as well as to compare with a field while joining. When I run the view after making this change, it is throwing the error ORA-01722 invalid number.
Please let me know on how to correct this.
Thanks
" it is throwing the error ORA-01722 invalid number."
It is likely somewhere you are comparing a numeric column with a string column . Oracle is implicitly casting the string to a number but the column contains values which cannot be converted, so it hurls.
"Please let me know on how to correct this."
Don't rely on implicit data conversion. Go through your code, check the data types of all the columns. Where you find a varchar2 column being compared to a number you need to cast the number to a string. For literals that means quoting them - '1' instead of 1 - and for columns that means wrapping them in to_char() calls.
My Suspicion is either r.code_value4 field or xx.COMPANY_CODE_JE_EXCEPTION_FLAG field is String type hence try the below
CASE WHEN (r.code_value4 = '0' AND LENGTH(ltrim(rtrim(xx.AFFILIATE_CODE))) > 0) AND
r_intercompany.code_value1 is not null AND
ltrim(rtrim(xx.AFFILIATE_CODE)) <> (CASE WHEN xx.COMPANY_CODE_JE_EXCEPTION_FLAG = '1'
THEN r.code_value3 ELSE r.code_value1 END)
THEN r_intercompany.code_value1
ELSE NVL(r_mga_acct.code_value2, xx.ACCOUNT_NUMBER) END

How to convert a string to timestamp with milliseconds in Hive

I have a string '20141014123456789' which represents a timestamp with milliseconds that I need to convert to a timestamp in Hive (0.13.0) without losing the milliseconds.
I tried this but unix_timestamp returns an integer, so I lose the milliseconds:
from_unixtime(unix_timestamp('20141014123456789', 'yyyyMMddHHmmssSSS')) >> 2014-10-14 12:34:56
Casting a string works:
cast('2014-10-14 12:34:56.789' as timestamp) >> 2014-10-14 12:34:56.789
but my string isn't in that form.
I think I need to reformat my string from '20141014123456789' to '2014-10-14 12:34:56.789'. My challenge is how to do that without a messy concatenation of substrings.
I found a way to avoid the messy concatenation of substrings using the following code:
select cast(regexp_replace('20141014123456789',
'(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{3})',
'$1-$2-$3 $4:$5:$6.$7') as timestamp)
A simple strategy would be to use date_format(arg1, arg2), where arg1 is the timestamp either as formatted string, date, or timestamp and the arg2 is the format of the string (in arg1). Refer to the SimpleDateFormat java documentation for what is acceptable in the format argument.
So, in this case:
date_format('20141014123456789', 'yyyyMMddHHmmssSSS')
would yield the following string: '2014-10-14 12:34:56.789' which can then be cast as timestamp:
cast(date_format('20141014123456789', 'yyyyMMddHHmmssSSS') as timestamp)
The above statement would return timestamp (as desired).
i had the date field in this form 2015-07-22T09:00:32.956443Z(stored as string). i needed to do some date manipulations.
the following command even though little messy worked fine for me:)
select cast(concat(concat(substr(date_created,1,10),' '),substr(date_created,12,15)) as timestamp) from tablename;
this looks confusing but it is quite easy if you break it down.
extracting the date and time with milliseconds and concat a space in between and then concat the whole thing and casting it into timestamp. now this can be used for date or timestamp manipulations.
Let say you have a column 'birth_date' in your table which is in string format,
you should use the following query to filter using birth_date
date_Format(birth_date, 'yyyy-MM-dd HH:mm:ssSSS')
You can use it in a query in the following way
select * from yourtable
where
date_Format(birth_date, 'yyyy-MM-dd HH:mm:ssSSS') = '2019-04-16 07:12:59999';
I don't think this can be done without being messy. Because according to the unix_timestamp() function documentation it returns the time is seconds and hence will omit the milliseconds part.
"Convert time string with given pattern to Unix time stamp (in seconds), return 0 if fail: unix_timestamp('2009-03-20', 'yyyy-MM-dd') = 1237532400."
Best option here would be to write a UDF to handle this is you want to avoid messy concatenations. However the concatenation (though messy) would be better to the job.

Select in ADO (vb6) with a numeric variable

Excuse me, occasionally I refer with some problem that maybe it's already been fixed. In any case, I would appreciate a clarification on vs.
I have a TariffeEstere table with the fields country, Min, Max, tariff
from which to extract the rate for the country concerned, depending on whether the value is between a minimum and a maximum and I should return a single record from which to extract its tariff:
The query is:
stsql = "Select * from QPagEstContanti Where country = ' Spain '
and min <= ImpAss and max >= ImpAss"
Where ImpAss is a variable of type double.
When I do
rstariffa.open ststql,.....
the recodset contains a record if e.g. ImpAss = 160 (i.e. an integer without decimals), and then the query works, but if it contains 21,77 ImpAss (Italian format) does not work anymore and gives me a syntax error.
To verify the contents of the query string (stsql) in fact I find:
Select * from QPagEstContanti Where country = 'Spain' and min < = 21,77 and max > = 21,77
in practice the bothering and would like a comma decimal, but do not know how do.
I tried to pass even a
format (ImpAss, "####0.00"),
but the value you found in a stsql is 21,77 always.
How can I fix the problem??
It sounds like the underlying language setting in SQL is expecting '.' decimals instead of ',' decimal notation.
To check this out - run the DBCC useroptions command and see what the 'language' value is set to. If the language is set to English or another '.' decimal notation - it explains why your SQL string is failing with values of double.
If that's the problem, the simplest way to fix it is to insert the following line after your stsql = statement:
  stsql = REPLACE(stsql, ",", ".")
Another way to fix it would be to change the DEFAULT_LANGUAGE for the login using the ALTER LOGIN command (but this changes the setting permanently)
Another way to fix it would be to add this command to the beginning of your stsql, which should change the language for the duration of the rs.Open:
  "SET LANGUAGE Italian;"

Oracle Pattern matching

In Oracle I want to check whether the string has "=' sign at the end. could you please let me know how to check it. If it has '=' sign at the end of string, I need to trailing that '=' sign.
for eg,
varStr VARCHAR2(20);
varStr = 'abcdef='; --needs to trailing '=' sign
I don't think you need "pattern matching" here. Just check if the last character is the =
where substr(varstr, -1, 1) = '='
substr when called with a negative position will work from the end of the string, so substr(varstr,-1,1) extracts the last character of the given string.
Use the REGEX_EXP function. I'm putting a sql command since you didn't specify on your question.:
select *
from someTable
where regexp_like( someField, '=$' );
The pattern $ means that the precedent character should be at the end of the string.
see it here on sql fiddle: http://sqlfiddle.com/#!4/d8afd/3
It seems that substr is the way to go, at lease with my sample data of about 400K address lines this returns 1043 entries that end in 'r' in an average of 0.2 seconds.
select count(*) from addrline where substr(text, -1, 1) = 'r';
On the other hand, the following returns the same results but takes 1.1 seconds.
select count(*) from addrline where regexp_like(text, 'r$' );

comp_cd > to_char('0000000000','9999999999')

I have come across an SQL statement where one of the conditions is comp_cd > to_char('0000000000','9999999999').
Running select to_char('0000000000','9999999999') from dual i am getting the result "0".
Does anyone has come across this?
The function TO_CHAR expects a NUMBER or DATE as its first parameter, but you're providing a string ('0000000000') instead.
Therefore, Oracle uses an implicit conversion to convert it to a NUMBER first; '0000000000' is converted to the number 0.
Then, TO_CHAR converts 0 back to a string using the '9999999999' format model. This should result in the string:
' 0'
Finally:
comp_cd > ' 0'
would do a lexical (alphabetical) comparison between two strings.

Resources