Need Desired date format in vertica - vertica

I am facing one simple issues in vertica.
select to_char(sysdate, 'MM/DD/YYYY');
OutPut:
01/18/2021
But the requirement is Output should be like below:
OutPut:
1/18/2021
Can anyone please help me to get like this.

You can suppress the leading zero using the FM Modifier:
dbadmin=> select to_char(sysdate, 'FMMM/DD/YYYY');
to_char
-----------
1/18/2021
(1 row)

Related

Why am I receiving an error for TRUNC function?

select TRUNC(TO_DATE('22-AUG-03'), 'YEAR')
from dual;
ORA-01843: not a valid month
1st example -> https://www.techonthenet.com/oracle/functions/trunc_date.php
I know "trunc" function takes in a date and optional fmt parameter.
Why am I getting this error?
I don't think the problem is trunc(). I think the problem is the date format. You are safer using the date keyword and an ISO-standard formatted date:
select TRUNC(DATE '2003-08-22', 'YEAR')
from dual;
The interpretation of a date string depends on the internationalization settings for your particular environment. The above does not have that dependency.
Here is a very simple check: IN THE SAME SESSION where your SELECT fails, and WITHOUT you altering the NLS_DATE_FORMAT, see what happens if you run the simpler statement,
SELECT TO_DATE('22-AUG-03') FROM DUAL
You will get the same error - which proves conclusively that it has nothing to do with TRUNC().
To make everything work exactly as in the tutorial, issue the command
ALTER SESSION SET NLS_DATE_FORMAT = 'dd-MON-yy'
first, before everything else. Note though that formats that have the year as two digits instead of four are a very bad idea in most cases.
It seems that you want to get the year of the date you receive in parameter
If you get '22-AUG-03' as parameter, add to TO_DATE function the date format you expect and then trunc it:
SELECT TRUNC(TO_DATE('22-AUG-03', 'dd-MON-yy'),'YEAR') FROM DUAL
Use SELECT EXTRACT(YEAR FROM TO_DATE('22-AUG-03', 'dd-MON-yy')) FROM DUAL;
to extract year from date.

Date importing from oracle to hive using spark hive context format in hive should be YYYYMMDD (dt_skey)

I have to import a table from Oracle to Hive using Spark and Scala, date column in Oracle looks like this Oracle column date, I have to cast it to the dt_skey format (YYYYMMDD) in Hive. Table format in Hive is Parquet. How can I do that? I googled it a lot but I didn't find any solution.
Thanks in advance
Assuming your input data is supposed to mean yy-mm-dd (so 16-09-15 means year 2016, month 09, day 15) you probably need a transformation like this:
select to_char( to_date (dt, 'yy-mm-dd'), 'yyyymmdd') from ...
Example:
with my_table ( dt ) as ( select '16-09-15' from dual)
-- this creates a test table my_table with column dt and value as shown
select dt,
to_char( to_date (dt, 'yy-mm-dd'), 'yyyymmdd') as dt_skey
from my_table
;
DT DT_SKEY
-------- --------
16-09-15 20160915
You could also manipulate the input string directly but I would strongly recommend against that. Translating to date and back will catch invalid "dates" in your data before you try to push them to an application. Also, the string manipulation would become complicated if the input strings are inconsistent (for example if something like 16-9-15 is allowed along with 16-09-15).
EDIT: In a comment to his original question, the OP stated that dt is already in DATE format in Oracle. In that case, it should NOT be wrapped within to_date() - that will lead to errors. Rather, the solution is MUCH simpler, all is needed is
select to_char(dt, 'yyyymmdd') from ...

Date_part does not works in oracle

SELECT
DATE_PART('days',DATE_TRUNC('month', pr.DateTo) '1 MONTH'::INTERVAL - DATE_TRUNC('month', pr.datefrom) as PeriodDays
FROM HT_PayReg
This works well in postgres,but in oracle it does not works,Please correct it in oracle.
TRUNC gives the date excluding the time portion.
TO_CHAR with format mask gives you different parts of a date.
For example,
to_char(sysdate, 'DD') gives today's date as 09.
to_char(sysdate, 'MON') gives current month as OCT.
to_char(sysdate, 'YYYY') gives current year as 2014.
Play around with the different formats. For a specific output, mention your desired output.
Look at datetime format models in documentation here http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements004.htm

how to format a date in delphi

How should I write the following select in delphi:
select to_char(sysdate, 'dd.mm.yyyy hh:mm:ss) from dual
When I try to execute it I receive an error, but this works:
select to_char(sysdate) from dual
Thanks in advance!
Missing single quote possibly at the end of your format string?
*
select to_char(sysdate, 'dd.mm.yyyy hh:mm:ss') from dual
*
You might try going in to SQL*Plus and running the query that works so you can see what format Delphi seems to want.
Share and enjoy.

number format in oracle

Hai,
i have a problem with number format.i'm using oracle.
I have a number field in database.But when i retreive it i need to be seen as floating point number
For example:
while retreiveing,now i got the result as 200 DR (DR for Debit,it is given manually).
Now i need to get the result as 200.00 DR as the result.
How can i solve this?Can any one help me?
You can use the TO_CHAR function to explicitely format data:
SQL> SELECT to_char(200.00, 'fm999G999G990D00') FROM dual;
TO_CHAR(200.00,'FM999G999G990D
------------------------------
200.00
Use TO_CHAR like this:
to_char(number,'999999.99')
For example:
SQL> select to_char(1234.5678,'999999.99')||' DR' as display from dual;
DISPLAY
-------------
1234.57 DR
The presence of a debit implies the need for a credit. In Oracle SQL we can use the SIGN() function to tell whether a number is positive or negative...
SQL> select to_char(abs(amt), 'fm999g999d00')||' '
2 ||case when sign(amt) = -1 then 'DR' else 'CR' end as fmt_amt
3 from transactions
4 order by txn_ts, txn_id
5 /
FMT_AMT
--------------
200.00 CR
200.00 DR
788.67 CR
788.67 DR
SQL>
The answers here that suggested TO_CHAR are correct, but if you're calling this SQL from the application code:
Get the number without formatting it with the SQL and then use your programming language to format it. For example, in Java, use the DecimalFormat class. In other words, leave formatting for the application specific code, not for the SQL.
The additional characters can be specified as part of the conversion by enclosing them in double-quotes, which might make things a little more simple:
To_Char(amount,'fm999G999G990D00" DR"')

Resources