Postgres OVERLAPS date function in TypeORM - postgresql

I want to transfer the query to TypeORM.
It looks like this in Postgres
SELECT *
from equipment_charging
where (equipment_charging."start_date"::date, equipment_charging."end_date"::date) OVERLAPS (DATE '2020-09-10', DATE '2020-09-14')
I'm writing this query in TypeORM like this:
.where(
'(equipment_charging.startDate::date, equipment_charging.endDate::date) OVERLAPS (DATE :startDate, DATE :endDate)',
{ startDate, endDate },
)
it gives me error like this:
{"context":"ExceptionsHandler","stack":["QueryFailedError: syntax error at or near \"$1\"\n at new QueryFailedError
What am I doing wrong?

If equipment_charging.startDate and equipment_charging.endDate are already date type in database I think that you could remove ::date
In same idea try to give directly date type for the input.
I also use lot of double quote for query builder in typeorm
try this:
.where(
'("equipment_charging"."startDate", "equipment_charging"."endDate") OVERLAPS (:startDate, :endDate)',
{ startDate, endDate },
)
And convert startDate and endDate to date if needed (with parseIso of date fns for example)

Related

GORM PostgreSQL getting date column without timezone

I'm learning golang atm, and currently I'm using gorm trying to select query getting date column, but it keep returning '2020-01-10T00:00:00Z', how do I get rid of the timezone?
I've tried changing date to time.Time or string, nothing works, here is the code
type Price struct {
DateStay time.Time `json:"date"`
Price int `json:"price"`
}
Update:
This is the code that I am using
var price []models.Price
err = models.DB.Raw(`
SELECT P.date_stay, P.price
FROM prices p
WHERE P.room_type_id = ?
`, roomTypeId).Scan(&price).Error
I tried to P.date_stay::date, date(P.date_stay) on the query but nothing works
I expect it to return '2020-01-10'
Using time.Time as a type for the date is probably best.
You can format the result of the date by setting the format to the desired date format you want.
(dd-mm-yyyy or whatever order you please).
Then you can format the value using time.Parse(format, date)
format := "2000-01-13"
formattedDate, err := time.Parse(format, price.DateStay)
assuming price is a result from your select query.
If you time is a time.Time you can try using price.DateStay.Format(format)

#Query for selecting date from datetime

I have a JPA mapped "event" object that includes a timestamp column. I am trying to select all events that occurred on a specific date. To do this I need to ignore the time component of the timestamp. I am not sure that this is possible using non database specific functions? Something like this fails to select:
#Query("SELECT r FROM BioChemRequest r WHERE r.pasId = :pasid AND r.observationDate BETWEEN :startDate AND :startDate")
public List <BioChemRequest> find(#Param("pasid") String pasid , #Param("startDate") Date startDate);
try the following
#Query("SELECT r FROM BioChemRequest r WHERE r.pasId = :pasid AND r.observationDate BETWEEN :startDate AND :endDate")
public List <BioChemRequest> find(#Param("pasid") String pasid , #Param("startDate") Calendar startDate, #Param("endDate") Calendar endDate);
where startDate is a calendar with hour 00:00:00 000
and endDate is a calendar with hour 23:59:59 999

Create an inserted_at datetime where filter using simple date string

I'm trying to get records inserted after a certain date given to me by the client.
2018-06-06
Here's how I'm writing the query:
{:ok, date} = NaiveDateTime.from_iso8601(date_string)
from(
m in query,
where: m.inserted_at > ^date
)
(MatchError) no match of right hand side value: {:error, :invalid_format}
And when I try to use a simple Date object:
** (Ecto.Query.CastError) lib/messages/search.ex:77: value ~D[2018-06-06] in where cannot be cast to type :naive_datetime in query
How can I find all messages inserted after that dummy string date the client is passing me?
You have an ISO 8601 date there, not a datetime. You can convert it into a NaiveDateTime (with hour, minute, second all set to 0) like this:
iex(1)> date_string = "2018-06-06"
"2018-06-06"
iex(2)> ndt = NaiveDateTime.from_iso8601!(date_string <> " 00:00:00")
~N[2018-06-06 00:00:00]
Now you can use ndt in your query and it will work.

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

TSQL DateTime Comparison

What I am trying to do is get a result from sql where the dates are in a certain range but its not working correctly, here is my query.
DECLARE #CurrDate DATETIME
SET #CurrDate = GETDATE()
SELECT dbo.ProductDetails.PartnerID
,dbo.ProductDetails.ProductID
,dbo.Products.ProductName
,StartDate
,EndDate
FROM dbo.ProductDetails
INNER JOIN dbo.Products
ON dbo.ProductDetails.ProductID = dbo.Products.ProductID
WHERE CONVERT(VARCHAR(10),StartDate,111) <= #CurrDate
AND CONVERT(VARCHAR(10),EndDate, 111) >= #CurrDate
but when the Enddate = #CurrDate the row does not show, but if i make that date just one day higher it gets displayed. Am i doing anything wrong? Any advice will do, thanks.
GetDate() returns date and time, while your conversion to varchar strips away the time part (I'm suspecting that's all it's actually supposed to do). So you would need to do the same conversion for #CurrDate.
If what you want is to simply consider the date only (ignoring the time part), you could use DATEDIFF instead of converting to varchar (see here); example:
DECLARE #CurrDate DATETIME
SET #CurrDate = GETDATE()
SELECT dbo.ProductDetails.PartnerID, dbo.ProductDetails.ProductID,
dbo.Products.ProductName , StartDate, EndDate
FROM dbo.ProductDetails INNER JOIN
dbo.Products ON dbo.ProductDetails.ProductID = dbo.Products.ProductID
-- where StartDate is on the same day or before CurrDate:
WHERE DATEDIFF(day, StartDate, #CurrDate) >= 0 AND
-- and where EndDate is on the same day or after CurrDate:
DATEDIFF(day, EndDate, #CurrDate) <= 0
If you want only DATE comparison, without time use the
cast(CONVERT(varchar, StartDate, 112) as datetime)
I am quite sure that the comparison takes into account the time as well as the date, in which case if the dates are the same but the current time is greater than the time being compared to you won't get that row as a result.
So, what you need to do is just extract the date part and compare those.
GETDATE() gives you date and time
if yours column have only date
then
CONVERT(VARCHAR(10),StartDate,111) <= #CurrDate
can give you unexpected result
remember
19.12.2011 14:41 > 19.12.2011 00:00
If you are using SQL 2008 or later, and wanting to compare only the date, not the time, you can also do:
Cast(StartDate as Date)
(This avoids having to convert to a string.)