so I am pretty new to this, but I would like to convert a text data_type into a date type (preferably into something like this yyyy-mm-ddThh:mi:ss.mmmZ date/time with timezone).
Now, I have found the W3 page's convert
and postresql. I have also found some solutions on stack overflow, but they all didn't work.
I have queried my database like this
SELECT TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,DATA_TYPE
FROM myappname.INFORMATION_SCHEMA.columns
WHERE TABLE_SCHEMA='myapp'
AND COLUMN_name LIKE '%date%'
ORDER BY table_name,column_name
where table_schema is where the tables of importance are in and table_name are tables of all my events. I selected only the properties (column_name) of these tables where they have a date, and all of them have a 'text' data_type.
Hence to my question, how can I convert them all to a date/time with timezone as above, so I can query my events (tables) by
where [column_name] between [date] and [date/time]
Thanks, in advance.
After looking into this for some time I found a solution. I am using
to_date() as a_horse_with_no_name suggests.
I post this as this may help others. My date format in string looked like this:
Mon Feb 20 11:34:21 GMT+07:00 2017
by running the select code below I get a column named to_date with a date type that looks like
2017-02-20.
select to_date(column_name,'Dy Mon DD HH24:MI:SS GMT+07:00 YYYY') from table_name;
Related
SELECT * FROM items WHERE created_time >= 20210505143012999
on mySQL, we can give condition like WHERE created_time >= 20210505143012999.
but, I want to find it with similar format(20210505143012999) on PostgreSQL.. How can I do this?
Seems mySQL is a little lax with data types (or perhaps just more forgiving), in Postgres your value is just an number (bigint). You need to convert it with the to_timestamp function. But as an epoch it seems Postgres does not like and it also does not appear to be an epoch either. You can either pre-convert to a string then use the to_timestamp of cast it as test within the function parameters. Either way specify the format: (see demo)
select to_timestamp ('20210505143012999', 'yyyymmddhh24missms');
select to_timestamp (20210505143012999::text, 'yyyymmddhh24missms');
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)
I have following value in my database:
Wed Jun 01 2016 00:00:00 GMT+0200 (CEST)
In my Code I got something like this:
2016-06-23
I need to query for a date like the one in my code, and in postgreSQL i found date_trunc to "cut off" unnecessary information from the date. The documentation shows following usage example:
SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
Result: 2001-02-16 20:00:00
So I thougt this should work:
redshift.query("SELECT stuff, ts , date_trunc('day', TIMESTAMP 'ts') as 'date'
FROM ::table_name
WHERE date = :day",{table_name: name, day: day}, function(err, data){
>>>error: Redshift query Error: error: syntax error at or near "'date'"
But obviously this didn't work. It told me that I got a syntax error near the "day", I gues its the 'ts'. In all the examples I found, the timestamp was hardcoded, but that is not what I need. Any Ideas how to use it with a colum as timestamp instead of hardcode ?
You accidentally used a literal 'ts' instead of referencing column ts, it seems. Moreover, you cannot have a string literal like 'date' as column alias. It has to be an identifier.
So it should look like
date_trunc('day', TIMESTAMP ts) as "date"
Maybe you mixed up string literals (quoted with ') and identifiers (quoted with ").
I tried below two ways they not working
Select * from Table
where SERV_DATE BETWEEN '03/01/2013'AND
'03/31/2013'
ALSO This is not working
Select * from Table
where SERV_DATE BETWEEN DATE('03/01/2013') AND
DATE('03/31/2013')
What should be the correct format ?
Did you tried what NealB suggested? The reason for not accepting 03/01/2013 as an entry date format is, that it is region dependent in the US it is March 1, 2013 an in the UK it is January 3, 2013. So without considering the local, it is not certain, what the actual date is.
"why would db2 give error on the same format and will go well when given different format" - Don't forget, that db2 is an old lady and as all old ladies she has peculiarities. You just get used to it and there will be an happy ending.
SELECT * FROM tableName WHERE date(modifiedBy_date) between '2017-07-28' AND '2017-08-01';
Works cool for DB2.
Select * from Table
where SERV_DATE BETWEEN DATE('2013-03-01') AND DATE('2013-03-31');
Worked for me.
Select * from Table
where (SERV_DATE BETWEEN '03/01/2013'AND
'03/31/2013')
Select * from Table
where (SERV_DATE BETWEEN '2013-03-01'AND
'2013-03-31')
select count(*) from TABLE where time_stamp BETWEEN DATE('2018-01-01') AND DATE('2018-01-31');
Here time_stamp is field name and copy your timestamp filed name instead of time_stamp.
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'));