Inserting CURRENTDATE for DATE column fails in PostgreSQL - postgresql

i am trying to insert CURRENTDATE as the value for a field that has the type defined as "Timestamp without Timezone".
INSERT INTO monthly_forecasts VALUES
('1','DIV1','Mon','Tue','Wed','Thu','Fri','','','','','-3',CURRENTDATE, CURRENTDATE)
But I get this error when I do that:
ERROR: column "currentdate" does not exist
LINE 2: ...'Mon','Tue','Wed','Thu','Fri','','','','','-3',CURRENTDAT...
^
How do I insert current date as the value for this field? Please help!

As documented in the manual the function is named current_date, not currentdate.
But as your column is defined as timestamp, you should use current_timestamp to include the time of the day (a DATE doesn't have a time)

Related

TIMESTAMP- creation_date :: date between '2022-05-15' and '2022-06-15'

I just wanted to know the difference between these two codes:
select count (user_id) from tb_users where
creation_date :: date between '2022-05-15' and '2022-06-15'
Result: 41,232
select count (user_id) from tb_users where
creation_date between '2022-05-15' and '2022-06-15'
Result: 40,130
As far as I see, it is related with the timestamp, but I do not understand the difference.
Thank you!
Your column creation_date in the table is most probably in timestamp format, which is '2022-05-15 00:00:00'. By adding ::date <- you are casting your timestamp format to date format: '2022-05-15'.
You can read more about casting data types here:
https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-cast/
When you ask Postgres to implicitly coerce a DATE value to a TIMESTAMP value - the hours, minutes and seconds are set to zero.
In the first query, you explicitly cast the creation date to DATE which is successfully compared to the provided DATE values.
In the second query, the creation date is of type TIMESTAMP and so PostgreSQL converts your DATE values to TIMESTAMP values and the comparison becomes
creation_date >= '2022-05-15 00:00:00' AND creation_date <= '2022-06-15 00:00:00'
Obviously, this produces different resultset than the first query.

text to timestamp in postgresql

In the view I have a text column which contains a timestamp in this format '20/03/2018 00:00' and I'm trying to make a selection with a between clause but it's not working
SELECT id,entry_date
FROM v_view
WHERE entrada BETWEEN to_timestamp('20/03/2018 00:00','DD/MM/YYYY')::timestamp and to_timestamp('22/03/2018 00:00')::timestamp
order entry_date
with this error message
ERROR: el operador no existe: text >= timestamp without time zone
LINE 3: WHERE entry_date BETWEEN to_timestamp('20/03/2018 00:00','DD/MM.
you need to convert the entrada column value to a timestamp.
Also: casting the result of to_timestamp() to a timestamp is useless because to_timestamp() already returns a timestamp
SELECT id,entry_date
FROM v_view
WHERE to_timestamp(entrada, 'dd/mm/yyyy hh24:mi')
BETWEEN to_timestamp('20/03/2018', 'DD/MM/YYYY')
and to_timestamp('22/03/2018', 'dd/mm/yyyy')
order entry_date;
I prefer to use ANSI SQL timestamp literals over the to_timestamp function:
SELECT id,entry_date
FROM v_view
WHERE to_timestamp(entrada, 'dd/mm/yyyy hh24:mi')
BETWEEN timestamp '2018-03-20 00:00:00'
and timestamp '2018-03-22 00:00:00'
order entry_date
Do not store date, time or timestamp values in a text or varchar column. You should define that column as timestamp then you don't need to convert anything and you don't need to deal with invalid timestamp values in that column.

Insert date into date9

Within my PROC SQL snipped I am trying to compare a column of datatype date9. to the date 31.12.2015'. I tried:
test_date = '31DEC2015'
This returns me the following error:
ERROR: Expression using equals (=) has components that are of different data types.
What would be the correct syntax?
Add "d" to the end of the quoted date value:
test_date = '31DEC2015'd;

How to save time in the database in Go when using GORM and Postgresql?

I'm currently parsing a time string and saving it to the db (Postgresql):
event.Time, _ := time.Parse("3:04 PM", "9:00 PM")
// value of event.Time now is: 0000-01-01 21:00:00 +0000 UTC
db.Create(&event)
It's giving me this error: pq: R:"DateTimeParseError" S:"ERROR" C:"22008" M:"date/time field value out of range: \"0000-01-01T21:00:00Z\"" F:"datetime.c" L:"3540"
event.Time⁠⁠⁠⁠'s type is time.Time.
I also tried setting event.Time's type to string and using time data type in postgresql:
type Event struct {
Time string `gorm:"type:time
}
But now I'm getting an error when fetching records in the db:
sql: Scan error on column index 4: unsupported driver -> Scan pair: time.Time -> *string
Investigated this issue further. Currently, there's no support in GORM for any Date/Time types except timestamp with time zone
See this part of code from dialect_postgres.go:
case reflect.Struct:
if _, ok := dataValue.Interface().(time.Time); ok {
sqlType = "timestamp with time zone"
}
So basically I see two options for you:
Either use varchar(10) in DB, and string in Go, an simply save it as "9:00 PM" (where 10 is some number that suits you)
Or use timestamp with time zone in DB, time.Time in Go, and format your date part as a constant date, 01/01/1970, for example:
time.Parse("2006-01-02 3:04PM", "1970-01-01 9:00PM")
In that case you'll have to omit the date part in your presentation, but if you plan to select by date range, that could work better for you.
You can set an arbitrary database-specific type with Gorm using sql tag
type Event struct {
Time time.Time `sql:"type:timestamp without time zone"`
}
When updating the DATETIME field in SQL, the Go string must be in this format: time.Now().Format(time.RFC3339).
From Postgres perspective the error stems from there being no year 0000. If you don't the date you may just be able to add 1 year to the converted timestamp giving '0001-01-01T21:00:00+00' which is a valid Postgres timestamp.
select '0000-01-01T21:00:00+00'::timestamptz at time zone 'UTC'
ERROR: date/time field value out of range: "0000-01-01T21:00:00+00"
Gives he same error. And just as a demonstration 1 day before 0001-01-01 gives:
select '0001-01-01T21:00:00+00'::timestamptz at time zone 'UTC' - interval '1 day' "day_before_1/1/1";
--day_before_1/1/1
--0001-12-31 21:00:00 BC

sqlite date comparison

I have column of type date time and values are getting stored in format 10-29-2011 08:25.
I would like to find out the rows only which are less then current date-time. What will be the condition for date comparison for current date and this date-time column field?
Thanks.
you could use the datetime function
SELECT * FROM mytable
WHERE mydate > datetime('now')
you can even make date operations
SELECT * FROM mytable
WHERE mydate > datetime('now','-15 days')