How to use date_trunc with variable as timestamp? - postgresql

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 ").

Related

Convert a postgres timestamp with no timezone column to a date

Given a postgres table with a column that is a timestamp without time zone
select "columnWithoutTimeZone" from my_table
Which results in:
columnWithoutTimeZone
Jun 11, 2022, 1:15:06 AM
Jun 11, 2022, 1:15:06 AM
How can the date component of timestamp be extracted?
I tried
select to_date("columnWithoutTimeZone") from my_table
But this produced an error:
ERROR: function to_date(timestamp without time zone) does not exist
There is a similar stack question, but that deals with timezone conversions. I am simply trying to extract the Jun 11, 2022 that is presently represented in the column.
Thank you in advance for your consideration and response.
Does this work for you?
select "columnWithoutTimeZone"::date from my_table

postgresql text type to date

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;

DB2 VARCHAR_FORMAT works with a Timestamp but not a Date

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.

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')

Oracle SQL : Expand year to a full date

This must be simple to answer, but how do you expand in Oracle a year to a full date, e.g.
1996 to 1996-01-01 00:00:00 ?
EDIT
The data type of the year is char, and I want to end up by comparing this year to a string-date, e.g.
1996 <= '1998-31-12 12:04:35'
It is important that the expanded data is expanded in the same data Format (since I get the dates preformatted)
At the end I need something like this
WHERE ( to_date(table.year_char ,'YYYY') <= '1996-12-31 00:00:00')
or sth like this
WHERE ( to_char(to_date(table.year_char ,'YYYY')) <= '1996-12-31 00:00:00')
or anything which works
If you're starting with the year as a string and you want to end up with a DATE object, you use the TO_DATE() function; but you need to supply a dummy month or it'll default to the first day of the current month in the specified year:
select to_date('1996', 'YYYY') from dual;
May, 01 1996 00:00:00+0000
SQL Fiddle
With the month, and to make it clearer the day too, appended and a suitable format model:
select to_date('1996' ||'-01-01', 'YYYY-MM-DD') from dual
January, 01 1996 00:00:00+0000
SQL Fiddle. I've left the year and the '-01-01' literal separate and concatenated on the assumption that you'll be using a variable really...
In a WHERE clause, using the sample date you initially showed:
select * from dual
where to_date('1996' ||'-01-01', 'YYYY-MM-DD')
<= to_date('1998-31-12 12:04:35', 'YYYY-DD-MM HH24:MI:SS')
Or if you're actually comparing to a string as your second example suggests, just leave it as a string, as you want both sides of the comparison to be the same data type without any implicit conversion that might cause you problems later. The string you have fortunately has the data in a format that is comparable:
WHERE (table.year_char || '-01-01 00:00:00' <= '1996-12-31 00:00:00')
You could convert it to and back from a DATE but there isn't any benefit in doing so.