I want to convert a Date column to a formatted string in DB2. This SQL works fine:
select varchar_format(current timestamp, 'YYYY-MM')
from sysibm.sysdummy1;
but this SQL gives an error:
select varchar_format(current date, 'YYYY-MM')
from sysibm.sysdummy1;
The error is:
[SQL0171] Argument 1 of function VARCHAR_FORMAT not valid.
In the first SQL, the first arg for VARCHAR_FORMAT is a timestamp, and that works. In the second SQL, the first arg for VARCHAR_FORMAT is a date, and that doesn't work.
The IBM doc implies that there's only this one function, VARCHAR_FORMAT (and its synonym, TO_CHAR).
How am I supposed to convert a DATE (not a TIMESTAMP) to a string? Or, do I have to convert the DATE to a TIMESTAMP first, then use VARCHAR_FORMAT?
I am running DB2 7.1 for i Series.
Update: converting to TIMESTAMP_ISO works. But it's ugly:
select varchar_format(timestamp_iso(current date), 'YYYY-MM')
from sysibm.sysdummy1;
That one works.
The documentation for the VARCHAR_FORMAT function in DB2 for i only mentions TIMESTAMP values, not DATE. Some DB2 platforms will implicitly cast a DATE value to a TIMESTAMP when the statement is calling a TIMESTAMP-only function or when comparing the DATE to a TIMESTAMP, but not all do.
Related
I'm completely new to Hive SQL and I need to do the following.
I have a column which includes a date and what I would like to do is to create a new one which will be the Sunday before this date.
In xls I would write the following:
my_date-WEEKDAY(my_date,1)+1
and in sql:
DATEADD(DD, -(DATEPART(DW, my_date)-1), my_date)
I tried the following in Hive SQL:
DATE_SUB (my_date, date_format(my_date,'u')-1)
but date_format returns a string.
Any ideas?
Cast the result of date_format to int and do the arithmetic.
DATE_SUB(my_date,cast(date_format(my_date,'u') as int)%7)
I am querying a timestamp column with datediff from current_timestamp. But it gives error.
DATEDIFF(minute, timestamp_field ,current_timestamp::TIMESTAMP)
or
DATEDIFF(minute, timestamp_field ,current_timestamp)
DataType of timestamp_field is "TIMESTAMP DEFAULT '2016-03-29 20:33:33.404256'::timestamp without time zone"
OutPut:
ERROR: Specified types or functions (one per INFO message) not
supported on Redshift tables.
Warnings: Function ""timestamp"(timestamp with time zone)" not
supported. Function "timestamptz(timestamp with time zone,integer)"
not supported. Function "timestamptz(text)" not supported.
But following query is working if I use getdate() function
DATEDIFF(minute, timestamp_field ,getdate()::TIMESTAMP)
I just simply casted the timestamp with timezone to timestamp and it was working.
example:
select datediff(min, CURRENT_TIMESTAMP::timestamp, CURRENT_TIMESTAMP::timestamp);
This worked for me:
datediff('day', ref_date::date, actual_date::date)
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'));
It works, but can it be simplified?
timeaccess is updated with php time() - which is a unix timestamp.
select to_date(to_char(to_timestamp(timeaccess),'YYYY-MM-DD'),'YYYY-MM-DD'),
from user_login;
Simpler:
SELECT to_timestamp(timeaccess)::date
FROM user_login
You can cast the resulting timestamp to date, :: being the casting operator.
I am trying to insert data in Postgres with cakephp which includes a date.
The column in my database is a timestamp without timezone and I have a datetime string "Y-m-d H:i:s" (also tried with an int).
Must I cast that value?
You do not have to cast the inserted values as long as the text literal is unambiguous and in accepted format.
Don't mistake a date ('2011-10-21') for a timestamp ('2011-10-21 12:10:23').
This is perfectly legal for a timestamp:
INSERT INTO tbl (timestamp_col)
VALUES ('2011-01-01 0:0:0');
But a date you have to cast, resulting in '2011-01-01 0:0:0' in this case:
INSERT INTO tbl (timestamp_col)
VALUES ('2011-01-01'::timestamp);
ISO 8601 format 'yyyy-mm-dd' for a date and 'yyyy-mm-dd hh:mm:ss' for a timestmap are unambiguous for any locale. Other formats may depend on your locale.
use
'Y-m-d H:i:s'::timestamp
Don't forget single quotes
for example
select '2011-01-01'::timestamp