I created a Firestore Database all fields are in String Format.
Now I need to by date on this field (reqDate) which is in string format.
Sample Data on the field (reqDate) = 10/11/2021, 9:22:11 PM
I tried the code below on Cloud Functions using moment to format the string field:
const thisCollection = firestore.collection("ItemList")
.where(moment("reqDate").format('MM/DD/YYYY'), ">", "10/05/2021");
But it return blank or no record.
Thanks in advance
First off, the condition you pass to you query is wrong because you need to pass:
The field name to filter on in the first parameter, which is now moment("reqDate").format('MM/DD/YYYY'), but should be "reqDate".
The operator, which is correct in your code.
The value to filter on.
This date format you use is unsuitable for range filters, since strings are ordered lexicographically and in that order: "10/05/2021" is before "11/05/2020".
To allow a range query, you need a date format that allows for that. When the date is a string, the most common formats are based on ISO-8601, such as "2021-10-17", because in this format dates that are after October 17 are queryable with: .where("reqDate", ">", "2021-10-17")
Related
In Sqlite I want to extract the date and time portions of a DateTime field separately in a view and return them also as a datetime, not strings. I've tried Cast, Date(), datetime(), but they all return strings.
I've read the SQLite documentation and understand how there is not an actual Date data type. Yet a Table field defined as DateTime is able to be parsed as a Date by an Excel query, but calculations on that field are not. I'm trying to do all data prep in the database view.
My data has the following field taken directly from the table definition:
LastModifiedDate datetime
I want the date (without time) to have the same DateTime data type as LastModifiedDate, not Text, because I use this view in many spreadsheets. I can apply Excel Date functions and formatting to LastModifiedDate field directly as returned from the ODBC query to Excel, and want to do the same to the Date-only part. I don't want to have to put a string-to-date conversion in every spreadsheet when I know it can get the date natively from Sqlite in LastModifiedDate.
SELECT LastModifiedDate,
date(LastModifiedDate) as Datepart,
cast(LastModifiedDate as numeric) as Date2
FROM Transactions
LastModifiedDate Datepart Date2
2019-07-28 18:22:38.9165394 2019-07-28 2019
LastModifiedDate in the above query is interpreted in Excel as a date to which date formats and date functions can be applied with no further processing required. Datepart above is returned as Text to Excel, and I can't apply date functions and formats without further pre-processing in Excel. I would like Datepart to be interpreted a date in Excel just as LastModifiedDate is.
I'm looking at the ch-werner.de sqliteodbc-0.9998. It will return an ODBC TIMESTAMP type only if the column decltype starts with timestamp or datetime. It returns ODBC TIME only for decltypes starting with time and ODBC DATE only for decltypes starting with date.
sqlite3 provides this decltype only for result table columns that are direct database column references. So if your SELECT statement has some expression that is more than a plain column reference, the decltype is lost. sqlite3 works like this at least up to version 3.39.0. It is documented.
The CAST expression converts the value of given expression to a storage classes by the determined affinity of the given declared type, but does not assign decltype to the result.
If you want to see the decltypes for query columns, you can use the sqlite3 cli and give it command .stats 2. Then it'll output the column declared types for each statement it executes.
If the decltype is found, the sqliteodbc-0.9998 will always parse string values into ODBC types. If DSN Option JDConv is enabled, it'll also parse floating point julianday values (whether provided as float or a string of a float) into ODBC types and when writing it'll write floating point into database.
If you can afford to change the schema, you can add a generated virtual column. This is cheap in storage, because data is not affected, but it costs when you query the column. This column can calculate other column into the values and decltypes you need for ODBC.
ALTER TABLE data ADD COLUMN
Datepart date AS (date(LastModifiedDate))
Then to get the Datepart, you simply query the column.
SELECT Datepart FROM data
When reading date fields from Sqlite into Firedac, I get conversion errors. The fields are called dates but with string entries (yyyy-mm-dd). I set the option for Datetime Format = string, but I've discovered that while null values are handled OK, empty values (= '') produce an error which I can't figure out how to handle.
You can enable StrsEmpty2Null option, which will automatically convert all empty strings to NULL state. But it's for all values and parameters handled by the data component. So it's not the cure.
I'm not sure what you're doing, but in general, NULL is a state and you cannot convert NULL state to a value because it's a state indicating no value. So as you cannot convert empty string to date.
So try to describe more about your value to string conversion, so we can suggest a proper way to deal with it. For SQLite, I'd suggest using DATE pseudo data type and convert values through the built-in formatting expressions.
I simply want to show a date and its day of the week from a table.
The following works:
select "invDate", (select extract (dow from timestamp '2014-09-22'))
from "tblInvMaster"
But the moment I try to use the actual field like the example below, it doesn't work:
select "invDate", (select extract (dow from timestamp "invDate"))
from "tblInvMaster"
The above gives a syntax error where the field name starts in timestamp.
What is the correct method of getting this to work?
The syntax
TYPENAME 'VALUE'
e.g.
TIMESTAMP '2014-01-01'
is only valid in SQL for type literals.
If you want to cast a non-literal value you must use an explicit cast. Most likely you don't require a cast at all, and can just write:
extract(dow from "invDate")
as "invDate" should already be a timestamp or date. If it isn't, you'll need to CAST("invDate" AS timestamp).
I am having graph database which is collection of events and attendees.
I store start_time property of an event as unix timestamp so that its easier to search upcoming events just by comparing unix timestamp.
Now the problem is by mistake I stored date string as start_time value in few events and now I can not compare date string with unix timestamp, and thats why query returns no events.
How can I compare data type of start_time property before comparing its value?
Please guide me the correct way to achieve my objective..
You can implicitly check for the property type using the toInt function and comparing with the value. To convert all string style start_time to their numeric variant:
MATCH (n)
WHERE has(n.start_time) and (toInt(n.start_time)<>n.start_time)
SET n.start_time = toInt(n.start_time)
In case of a lot of nodes use SKIP and LIMIT to work on reasonable batches.
This is how I solved this problem.
"START attendee=node:attendee('user_id:100001195447969') MATCH (attendee)-[:friends_with]-(friend)-[:attending]-(event) WITH event,attendee as user, count(distinct friend.user_id) as count WHERE REPLACE(str(event.start_time),"-","") = str(event.start_time) AND count >= 1 RETURN event.start_time;"
So now it does not give me events having start_time like "2014-06-05 10:00:00". And I can compare start_time of rest of the events.
I don't know if this would work, but could you compare the value to the value wrapped in a str function? It may not work if the string properties are formatted like unix timestamp, but it's worth a try.
MATCH (e:Event) WHERE e.start_date = str({e.start_date}) RETURN e;
currently, i have a datetime object
DateTime theDateTime = DateTime.ParseExact(dateAndTime, "d MMMM yyyy hh:mm tt", provider);
which successfully converts it into a datetime (from a string) to become for example :
7/6/2012 9:30:00 AM
How do i convert this to become 2012/07/06 09:30:00 (24hr format)? So that i can insert it into the database using C#??
PS: I'm using Sybase SQL Anywhere 12, and from what I've read, they neeed the format to be in year/months/day and the time to be in 24hr format right? Please correct me if I'm wrong.
The DateTime itself does not have a format. The date and time are stored internally as a number. Usually the classes of the database provider take care of converting a DateTime to the correct format.
If Sybase will only accept the date formatted as a string you will need to use the DateTime.ToString method and format it with the correct format string.
How are you building your insert command? Are you using database parameters or just building a string containing the insert statement?
SQL Anywhere 12 has a default date format of YYYY-MM-DD HH:NN:SS.SSS
This can be configured/changed with the timestamp_format database option however:
timestamp_format option
The setting can be permanently changed through SQL like:
SET OPTION PUBLIC.timestamp_format = '<format here>';
Or temporarily changed (per connection basis) like:
SET TEMPORARY OPTION timestamp_format = '<format here>';
Of course, if you already have a datetime object in your code, you should be able to pass the value into a parameterized query. It doesn't have to be passed as a string.