DATE_FORMAT() equivalent in PostgreSQL - mysql

I have this query in mysql
SELECT *
FROM `calendar`
WHERE DATE_FORMAT(startTime, "%Y-%m-%d") = '2010-04-29'
How can i convert to Postgresql query?

Basically, the query in MYSQL which uses DATE_FORMAT() converts date into string. If you want to compare it with date, don't use DATE_FORMAT() but instead DATE(). Try this, in PostgreSQL, casting timestamp into date,
SELECT *
FROM "calendar"
WHERE "startTime"::date = '2010-04-29'
SQLFiddle Demo
other source

SELECT *
FROM calendar
WHERE starttime::date = '2010-04-29'

Related

mysql date_format() or str_to_date() in where clause

I've view named Sales View
select * from SalesView;
I want to fetch records from SalesView having order_date 9-OCT-2019.
Value for Order date is supplied from GUI in format DD-MM-YYYY.
I tried,
select *
from salesView
where str_to_date(order_date,'%d-%m-%Y') = str_to_date('09-10-2019','%d-%m-%Y')
order by oID;
I've also tried date_format() instead of str_to_date(), but It is returning nothing.
Check your where clause
where str_to_date(order_date,'%d-%m-%Y') = str_to_date('09-10-2019','%d-%m-%Y')
Replace order_date's format with the format in which database stored it. YYYY-MM-DD
where str_to_date(order_date,'%Y-%m-%d') = str_to_date('09-10-2019','%d-%m-%Y')

Converting date from the column to the unix_timestamp

I got date, stored in MySQL column like this: 09-11-2012 (format is DD/MM/YYYY), but when I do:
SELECT UNIX_TIMESTAMP(t.date) FROM table t;
It does returns 0 for every single row.
How can I convert this format date to the unix timestamp? I would like to convert this date just like the PHP's strtotime is doing it.
I guest t.date is a string right? try converting it to DATE first using STR_TO_DATE
SELECT UNIX_TIMESTAMP(STR_TO_DATE(t.date, '%d-%m-%Y'))
FROM table t;
SQLFiddle Demo

Retrieve records based on Date

I have a date column in my table.
Is there any way in mysql to retrieve record based on date excluding the time part?
I have tried appending * but does not seems to work.
SELECT * FROM myTable where filterDate="2010-08-01 *"
Is there any way to do it like this rather then filtering it by using between?
Thanks.
SELECT * FROM myTable where filterDate LIKE "%2010-08-01 %"
You can use DATE_FORMAT function of mysql
SELECT * FROM myTable where DATE_FORMAT(filterDate,'%Y-%m-%d') ="2010-08-01"
Try this
SELECT DATE_FORMAT(filterDate , '%d-%m-%Y') AS new_date FROM table_name WHERE
DATE_FORMAT(filterDate , '%d-%m-%Y') = '2010-08-01'

MySql, combining date and time column into a time stamp

I am guessing this is relatively simple to do, but I am unsure of the syntax. I have date and time columns that I want to combine to a timestamp column. how would I query this using a select?
Or you could use the built-in TIMESTAMP(date,time) function.
So then you would do something like this say from an Orders table...
SELECT OrderNumber, TIMESTAMP(date,time) as OrderTS, SalesPersonID
FROM Orders
Mysql does not seem to have a constructor for datetime such as datetime('2017-10-26', '09:28:00'). So you will have to treat the component part as string and use string concatenation function (Note mysql does not have the || operator for string concatenation). If you want the datetime type, you will have to cast it.
concat(datefield,' ',timefield) as date
select cast(concat('2017-10-26', ' ', '09:28:00') as datetime) as dt;
If it possible to use built-in function, just use it.
Any way here is an example to find records between given timestamps.
SELECT `id` FROM `ar_time` WHERE TIMESTAMP(`cdate`,`ctime`) BETWEEN fromTimeStamp AND nowTimeStamp;
For 24hr time
TIMESTAMP(Date, STR_TO_DATE(Time, '%h:%i %p'))
SELECT * FROM tablename WHERE TIMESTAMP(datecol, timecol) > '2015-01-01 12:00:00';
O.P. did say SELECT but in case anyone wants to add a timestamp column:
ALTER TABLE `t` ADD COLUMN `stamp` TIMESTAMP;
UPDATE `t` SET `stamp` = STR_TO_DATE(CONCAT(`Date`, ' ', `Time`), '%m/%d/%Y %H:%i:%s');
Adjust format strings to taste.
concat('2021-12-31', ' ', '07:00:00')
it worked in an INSERT procedure.

How to cast DATETIME as a DATE in mysql?

My query is this. I have a bunch of entries and i want to group them by date. But instead of having date in my database, I have a datetime field. What do I do?
select * from follow_queue group by follow_date cast follow_date as date
That's not working.
Use DATE() function:
select * from follow_queue group by DATE(follow_date)