remove time from date field in db2 - db2

This is the date column I am using to get date
(CAST(substr(CAST(q2.hedte AS VARCHAR(8)),1,4) || '-' || substr(CAST(q2.hedte AS VARCHAR(8)),5,2) || '-' ||
substr(CAST(q2.hedte AS VARCHAR(8)),7,2) AS date)) As FLD5
However, this is also getting me time. How do I get rid of time?

When using Db2 in Oracle compatibility mode, DATEs are actually TIMESTAMPs. That is how Oracle works, and so is how Db2 has to work to. It does mean that you can't then create proper DATE only columns.
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.apdv.porting.doc/doc/r0053667.html
So short of converting your DATE that is really a TIMESTAMP into e.g. CHAR(10), you can't "get rid of the time".
E.g. try this
SUBSTR(TO_DATE(q2.hedte,'YYYYMMDD'),1,10)

Related

How to transfer specific number to date in PostgreSQL?

Need to transfer numeric columns with numbers like '201 711' and '201 806' to dates like '2017-11-01' and '2018-06-01' ('YYYY-MM-01'). When I try this
select to_date(debt_max_period,'YYYY-MM') as date1 from debt;
I get
SQL Error [42883]: ERROR: function to_date(numeric, unknown) does not
exist.
Will be thankful for any ideas!
You need to cast the number to a text/varchar value. As the number doesn't contain the - character, you also need to remove that from your format mask.
to_date(debt_max_period::text, 'yyyymm')
Oracle -
select TO_CHAR(TO_DATE(START_DATE,'YYYYMMDD'),'YYYY-MM-DD') STARTDATE from account
You can use like below as well.
Postgresql -
select TO_CHAR(TO_DATE(START_DATE::text,'YYYYMMDD'),'YYYY-MM-DD') STARTDATE from account

how to coalesce timestamp with not null constraint postgres

insert into employee(eid,dojo) SELECT
14,coalesce(to_char(dojo,'dd-mm-yyyy'),'')
from employee;
I have to insert into table by selecting it from table,my column dojo has not null constraint and timestamp doesn't allow '' to insert please provide an alternate for this if timestamp is null from select query
Your current query has severals problems, two of which I think my answer can resolve. First, you are trying to insert an empty string '' to handle NULL values in the dojo column. This won't work, because empty string is not a valid timestamp. As others have pointed out, one solution would be to use current_timestamp as a placeholder.
Another problem you have is that you are incorrectly using to_char to format your timestamp data. The output of to_char is a string, and the way you are using it would cause Postgres to reject it. Instead, you should be using to_timestamp(), which can parse a string and return a timestamp. Something like the following is what I believe you intend to do:
insert into employee (eid, dojo)
select 14, coalesce(to_timestamp(dojo, 'DD/MM/YYYY HH:MI:SS PM'), current_timestamp)
from employee;
This assumes that your timestamp data is formatted as follows:
DD/MM/YYYY HH:MI:SS PM (e.g. 19/2/1995 12:00:00 PM)
It also is not clear to me why you are inserting back into the employee table which has non usable data, rather than inserting into a new table. If you choose to reuse employee you might want to scrub away the bad data later.
you can use some default date value like 1st jan 1900 or now()
your query should be like
insert into employee(eid,dojo) SELECT
14,coalesce(to_char(dojo,'dd-mm-yyyy'),now())
from employee;
There is no such thing as a non-null yet blank timestamp. NULL = blank.
There is literally nothing you can do but store a valid timestamp or a null. Since you have a non-null constraint your only option is to pick a default timestamp that you consider "blank".
Using a hard coded date to indicate a blank value is a terrible terrible terrible idea btw. If it is blank, remove the not null constraint, make it null and move on.
I am not trying to be condescending but I do not think you understand nulls. See here
https://en.wikipedia.org/wiki/Null_(SQL)

ORA-01843: not a valid month - but what month format? Oracle 11g

I want to know what other MONTH formats exist except MM , MONTH or MON.
When the query below runs it gives me an error ORA-01843: not a valid month and I can understand why, because the server removes the "0" from the month "07" and leaves only the number "7", so the MM format is not the right one.
But which one is it?
select to_char(to_date(START_DATE,'MM/DD/YYYY '), 'DD-MM-YYYY')
from PER_ALL_PEOPLE_F
WHERE person_id=12345
The START_DATE column is DATE TYPE and it provides results like: 7/17/2012 .
Your assumption that the single-digit 7 for the month is a problem is not correct; Oracle is generally quite flexible and will happily parse a single digit month with the MM model:
select to_date('7/17/2012', 'MM/DD/YYYY') from dual;
TO_DATE('7/17/2012'
-------------------
2012-07-17 00:00:00
If start_date is already a DATE type then you should not be calling to_date() for it. You're doing an implicit conversion to a string using your NLS_DATE_FORMAT moodel, and then back to a date with your specified format. So really you're doing:
select to_char(to_date(to_char(START_DATE, <NLS_DATE_FORMAT>),
'MM/DD/YYYY '), 'DD-MM-YYYY')
If your NLS_DATE_FORMAT is something other than MM/DD/YYYY, e.g. DD-MON-YYYY, then you'll get an ORA-1843 error for at least some values.
You can see this with:
select to_date(date '2014-01-16', 'MM/DD/YYYY') from dual;
or the expanded:
select to_date(to_char(date '2014-01-16', 'DD-MON-YYYY'),
'MM/DD/YYYY') from dual;
Dates do not have any specific format, they're stored in an internal representation and then converted to a formatted string for display. You said your dates display like 7/12/2012, but given the error you're seeing your client seems to be doign that formatting, and it isn't related to the session NLS_DATE_FORMAT.
You only need to do:
select to_char(START_DATE, 'DD-MM-YYYY')

how to insert a time in oracle 10g database

I want to insert date and time in oracle database, I have created the table with columns
create table myadmin
( employe_id number(5),
supervisor Varchar2(20),
department Varchar2(20),
action Varchar2(20),
sdate date,
stime date)
While inserting the values below it gives an error. Please tell me how to insert the time ?
insert into myadmin
( employe_id,supervisor,department,action,sdate,stime) values
(83,'gaurav','helpdesk','pick','23-jan-2013','09:43:00');
You have to use keyword to_date for date insert in oracle like this.
to_date('23-01-2013','dd-mm-yyyy')
Basically you have to use keyword to_date('your date','your date format').
You can also add date and time together if you want and it would be something like this
to_date('23-01-2013 09:43:00','dd-mm-yyyy hh24:mi:ss')
A date in Oracle always has a date part and a time part. Having date and time in two separate columns only makes sense, if it can occur that date is null and time is not. (And still, you could set date to an improbable value like 1.1.0001 then.)
However, if you want to stick to those two separate fields, then make your string a datetime with the to_date function specifying the format used:
insert into myadmin
( employe_id,supervisor,department,action,sdate,stime) values
(83,'gaurav','helpdesk','pick',to_date('23-01-2013','dd-mm-yyyy'), to_date('09:43:00', 'hh24:mi:ss'));

PostgreSQL - Convert year to date

The year is given as int: 2009, 2010 etc.
I want to convert this information to DATE (first January).
My solutions (I prefer the first one):
(year::char || '-01-01')::DATE
'0001-01-01' + ((year-1)::char || ' year')::interval
Is there a better (build in) or more elegant and faster solution?
(I'm currently working with PostgreSQL 8.4 but are also interested in more recent versions.)
I think this is the simplest way:
to_date(year::varchar, 'yyyy')
SELECT to_date(2011::text, 'YYYY');
Attention: any code based on default casting from text to date is bad. Somebody can change a default format datestyle to some unexpected value, and this code fails. Using to_date or to_timestamp is very preferable. to_date or to_timestamp is relative expensive, but it is rock robust.
to_date('01 Jan ' || year, 'DD Mon YYYY')
OR
SELECT (DATE (year || '-01-01'))
ref: http://www.postgresql.org/docs/current/interactive/functions-formatting.html
Note: I haven't worked with PostgreSQL
One possibility:
select year * '1 year'::interval + '0000-01-01'::date;
I like this way because it avoids conversion between text and integer (once all the constants are parsed).