Search date period in DB (month and year columns) - nestjs

Here's my Dto:
export class GetQuotaQuery {
#Matches(
/^(0[1-9]|1[0-2])-(20[0-9]{2})$/,
{
message:
"Date format of 'from' field should be MM-YYYY (Month between 01 and 12, Year 2000 to 2099)"
}
)
from: string;
#Matches(
/^(0[1-9]|1[0-2])-(20[0-9]{2})$/,
{
message:
"Date format of 'to' field should be MM-YYYY (Month between 01 and 12, Year 2000 to 2099)"
}
)
to: string;
}
I will receive a string representing a date in the format MM-YYYY on parameters from and to, and I need to use those to query objects in the database matching that period.
The database doesn't use a datetime column, but two separated integer columns: month and year. As below:
What's the best way to achieve that?
REMARK: Changing the database columns type is not an option as I have no control over that. Thanks in advance.

Related

How to extract the year and quarter from the String date in databricks SQL

Can someone show me how to extract the year from the String date in databricks SQL.
I am based in the UK and our date format is normally as follows:
dd/mm/yyyy
The field containing the dates is set as StringType()
I am trying to extract the year from the string as follows:
select year(cast(financials_0_accountsDate as Date)) from `financiallimited_csv`
I'm using the following the code to extract the quarter
select quarter(cast(financials_0_accountsDate as Date)) from `financiallimited_csv`
However, both result in NULL values.
Any thoughts on how to extract the year and quarter from dates with StringType() dd/mm/yyyy?
The table looks like the following:
Could you try the to_date function?
select year(to_date(financials_0_accountsDate, 'dd/MM/yyyy')) from `financiallimited_csv`

How do I group by date in Cassandra?

I'm trying to find a query in Cassandra cql to group by date. I have "date" datatype where the date is like: "mm-dd-yyyy". I'm just trying to extract the year and then group by. How to achieve that?
SELECT sum(amount) FROM data WHERE date = 'yyyy'
You cannot do a partial filter with just the year on a column of type date. It is an invalid query in Cassandra.
The CQL date type is encoded as a 32-bit integer that represents the days since epoch (Jan 1, 1970).
If you need to filter based on year the you will need to add a column to your table like in this example:
CREATE TABLE movies (
movie_title text,
release_year int,
...
PRIMARY KEY ((movie_title, release_year))
)
Here's an example for retrieving information about a movie:
SELECT ... FROM movies WHERE movie_title = ? AND release_year = ?

Convert a varchar column to date for the month of December

I have a varchar2 column in a table which saves dates in it with the format mask 'DD-MON-RR'.
Now when I try to convert it with
{to_date(my_field,'DD-MON-RR')}
it returns accurate result except when I convert the December month, it throws an error
literal does not match the format string
I have solved my problem by converting whole string to {to_char} again, but my question is: why does Oracle not let me convert the month of December saved in form of 'DEC'?

How to get values of date & time fields as millisecs from netsuite suitescript 2.0

In Suitescript 2.0, I can able to get record by id.
RESTlet:
var response = record.load({
type: resourceType,
id: context.recordId
});
Response:
{
...
"starttime": "6:00 pm",
"startdate": "13 FEBRUARY, 2019"
...
}
How to get all date & time values as millisecs in Netsuite using suitescript 2.0?
NetSuite has 2 types of date fields, date and dateTime.
Date field does not contain time details, so you cannot get time from these fields like trandate etc. Whereas if you use getValue on dateTime field, NetSuite returns a date object and you can toISOString or any other method from Date class.

cassandra extract month from timestamp

In Postgres, we have function to extract MONTH, YEAR etc from timestamp using EXTRACT function. See below.
SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
Is it possible to do the same in cassandra? Is there a function for this?
If it is possible, I can then run queries such as find all entries in year "2015" and month "may". This is possible in postgres using EXTRACT function.
I hope you have the answer, but you can create a function in your keyspace like this:
cqlsh> use keyspace;
cqlsh:keyspace> CREATE OR REPLACE FUNCTION YEAR (input TIMESTAMP)
RETURNS NULL ON NULL INPUT RETURNS TEXT
LANGUAGE java AS 'return input.toString().substring(0,4);';
cqlsh:kespace> SELECT YEAR('2001-02-16 20:38:40') as year FROM ...;
In Cassandra you would handle that a little differently. You can have fields in your table of type timestamp or timeuuid, and then use that field in a time range query.
For example if you wanted all entries for 2015 and May and have a timestamp field called 'date', you could do a query like this:
SELECT * from mytable where date > '2015-05-01' and date < '2015-06-01' allow filtering;
You can use a number of different formats for specifying the date to specify the time more precisely (such as down to fractions of seconds).
Cassandra converts date strings using the org.apache.commons.lang3.time.DateUtils class, and allows the following date formats:
private static final String[] dateStringPatterns = new String[] {
"yyyy-MM-dd HH:mm",
"yyyy-MM-dd HH:mm:ss",
"yyyy-MM-dd HH:mmX",
"yyyy-MM-dd HH:mmXX",
"yyyy-MM-dd HH:mmXXX",
"yyyy-MM-dd HH:mm:ssX",
"yyyy-MM-dd HH:mm:ssXX",
"yyyy-MM-dd HH:mm:ssXXX",
"yyyy-MM-dd HH:mm:ss.SSS",
"yyyy-MM-dd HH:mm:ss.SSSX",
"yyyy-MM-dd HH:mm:ss.SSSXX",
"yyyy-MM-dd HH:mm:ss.SSSXXX",
"yyyy-MM-dd'T'HH:mm",
"yyyy-MM-dd'T'HH:mmX",
"yyyy-MM-dd'T'HH:mmXX",
"yyyy-MM-dd'T'HH:mmXXX",
"yyyy-MM-dd'T'HH:mm:ss",
"yyyy-MM-dd'T'HH:mm:ssX",
"yyyy-MM-dd'T'HH:mm:ssXX",
"yyyy-MM-dd'T'HH:mm:ssXXX",
"yyyy-MM-dd'T'HH:mm:ss.SSS",
"yyyy-MM-dd'T'HH:mm:ss.SSSX",
"yyyy-MM-dd'T'HH:mm:ss.SSSXX",
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX",
"yyyy-MM-dd",
"yyyy-MM-ddX",
"yyyy-MM-ddXX",
"yyyy-MM-ddXXX"
};
But note that Cassandra is not as good at ad hoc queries as a relational database like Postgres. So typically you would set up your table schema to group the time ranges you wanted to query into separate partitions within a table.

Resources