change date format in sqlite - ios

Change this date format which is in sqlite db 12/10/11 to 12-10-11 (mm-dd-yy) I am unable to do so .I am a noob in sqlite and have to parse this value SELECT strftime('%d-%m-%Y',Date) from report but I am getting null as sqlite db excepts value in mm-dd-yy so How do I convert format 12/10/11 to 12-10-11 (mm-dd-yy) .Thanks in advance .Really appreciate the help.

The short answer:
If you have a text string stored as "12/10/11" that you want reported as "12-10-11", you should use the replace(X,Y,Z) function, to replace occurrences of Y in X with Z. Thus:
SELECT replace('12/24/11','/','-');
will return:
12-10-11
The long answer:
First, dates do not actually exist as a proper datatype in SQLite. They're stored as either TEXT, REAL, or INTEGER values. See date and time datatype in SQLite. So it depends upon how your date was stored in the database.
Second, you seem to be implying that you stored the date in a "mm/dd/yy" format. That's not a valid/useful TEXT format to be storing date/time values (as the date cannot be sorted, cannot used in "greater than" and "less than" operations, cannot be used in SQLite date functions, etc.). You really want to store datetime values in one of the formats listed in the "Time strings" section of the date and time functions document.
So, generally you should store your date/time values in one of those formats, use NSDateFormatter to convert that to a NSDate when you retrieve it from the database. And when you want to display the date value in your app, use whatever format you want for output.
But, if you don't care that the dates are stored as text strings and are not effectively usable as dates in SQLite, then just treat it as a plain old TEXT string and use TEXT functions, such as replace(X,Y,Z) to replace occurrences of "/" with "-", as outlined above.

Related

How to i change the date time format on Google Sheets?

I am working on a datasheet on Google sheets and I am trying to change values in column (D) to date time format, so I went to Format<number<date time. But only a few values are getting converted and the rest remain the same. Please see below-
Screenshot of column D
The values aligned on right are converted and the left ones aren't. I even tried paint format, trim white spaces but it didnt work. Please suggest a way on how to resolve this error. Also, there are 49623 values in column D.
thank you
Your spreadsheet is set to a locale that uses the mm/dd/yyyy date format, while your data is using the dd/mm/yyyy date format. That means that the dates where the day of the month is less than or equal to 12 will get converted to dates incorrectly, and the rest of the data will remain as text strings.
To make it work, choose File > Settings > Locale and choose a locale that matches your data, then repaste the data to convert it correctly.
The "49623 values" are dateserials that can be converted to dates by formatting them in a date format. See this answer for an explanation of how date and time values work in spreadsheets.

Neo4j, How can I compare dates that stored as strings?

I have a date in my Neo4j database that is 9-03-2021 and this date is stored as a string in Neo4j, how would I compare that this date for example is <= 9-05-2021 ?
I am basically trying to return all nodes in my database where the date field is <= a certain date, or >= a certain date, where these dates are stored as strings,
thank you
The better way, of course, is to store the date as a temporal property, index it, and then in your predicates on the property compare it to other temporal values.
If you're stuck with a String property, then it would be best to use strings that afford comparison in YYYY-MM-DD format, as the string comparisons in that format will match temporal comparisons.
Your current format, DD-MM-YYYY, does not afford comparisons to other strings in that format. If you are stuck with this current format for your properties, then as Tomaž said in the comments, you would need to parse this into a type that is comparable (such as by using apoc.date.parse() ) which should give you a unix epoch timestamp (long value), and you would similarly need to parse the dates that you are comparing it to so they are all of the same type.
The problem with that approach is that you cannot use indexes here, so you won't be able to speed up the lookup. So you really should consider either using temporal properties, or at least using strings in a comparable YYYY-MM-DD format.
I would compare them both as strings:
WHERE
9-03-2021 <= '9-05-2021'
I found it helpful to use Neo4j's date() function to convert a date stored as a string into a temporal value if you want to compare a string value to a temporal value. First I trim the string so it's formatted as YYYY-MM-DD and then I place it inside the date() function.
For example start_date in the below example stores the start date and time as a string but I only want to return values where the start date is less than today's date:
WHERE
date(left(start_date, 10)) < date()

Warning about "xsd:date"

I am using Jena to parse a "TTL" formatted file. I see the warning in the console
Lexical form '1896-13-04' not valid for datatype http://www.w3.org/2001/XMLSchema#date
I want to know why this warning happens.
Per the XML schema specification for xsd:date:
The ·lexical space· of date consists of finite-length sequences of characters of the form: '-'? yyyy '-' mm '-' dd zzzzzz? where the date and optional timezone are represented exactly the same way as they are for dateTime
i.e. dates must follow the International Convention of having year then month then day.
From the example given your data appears to have dates in the American convention which has year then day then month. Since 13 is not a valid month you receive a warning.
Your input data is not valid according to the specifications and therefore may not be processed correctly when you try to ask queries based upon that data e.g. Find items with dates before or after a specific date of interest. Dates for which you are not receiving a warning maybe interpreted incorrectly a day and month being interchanged.
You need to correct the data as otherwise this will cause you issues later. If the data is from a public data source you should let them know that they have a data quality issue, if the data is being created by yourself you need to correct your data generation so the dates following the specification.

Rails column type for moment.js formatted date and time

I am building a rails app, where the user picks up a date from a date picker and a time from the time picker. Both the date and time have been formatted using moment js to show the date and time in the following way:
moment().format('LL'); //January 23,2017
moment().format('LTS'); //1:17:54 PM
I read this answer with guidelines about selection of a proper column type.
Is there documentation for the Rails column types?
Ideally, I should be using :date, :time or :timestamp for this. But since the dates are formatted, should I be using :string instead?
Which would be the correct and appropriate column type to use in this situation?
If you want to store a time reference in your database you should use one of the types the database offers you. I'll explain this using MySQL (which is the one I have used the most) but the explanation should be similar in other database servers.
If you use a timestamp column you will be using just 4 bytes of storage, which is always a good new since it makes smaller indexes, uses less memory in temporal tables during the internal database operations and so on. However, timestamp has a smaller range than datetime so you will only be able to store values from year 1970 up to year 2038 more or less
If you use datetime you will be able to store a wider range (from year 1001 to year 9999) with the same precision (second). The bad consequence is that a higher range needs more memory, making it a bit slower.
There are some other differences between these two column types that don't fit in this answer, but you should keep an eye on before deciding.
If you use varchar, which is the default column type for text attributes in Ruby on Rails, you will be forced to convert from text to datetime and vice-versa every time you need to use that field. In addition, ordering or filtering on that column will be very inefficient because the database will need to convert all strings into dates before filtering or sorting, making it impossible to use indexes on that column.
If you need sub-second precision, you can use bigint to meet your requirements, as MySQL does not provide a date specific type for this purpose
In general, I recommend using timestamp if your application requirements fit the timestamp limitation. Otherwise, use datetime, but I strongly discourage you to use varchar for this purpose.
EDIT: Formatting
The way you store dates in database is completely different from the way you display it to the user. You can create a DateTime object using DateTime.new(year, month, day, hour, minute, second) and assign that object to your model. By the time you save it into database, ActiveRecord will be in charge of converting the DateTime object into the appropiate database format.
In order to display a value that is already stored in database in a specific format (in a view, API response, etc.) you can hava a look at other posts like this one.
You can have a timestamp column in your database, and then parse the request to a ruby datetime object like this:
d = Time.parse(params[:date])
t = Time.new(params[:time])
dt = DateTime.new(d.year, d.month, d.day, t.hour, t.min, t.sec, t.zone)
#now simply use dt to your datetime column
On Postgres you can save a ruby DateTime object straight into a postgres timestamp field, e.g
User.first.update_attribute('updated_at', dt )
Another option is to concatenate your date and time strings into one and then u can do a one-liner:
User.last.update_attribute('created_at', Time.parse('January 23,2017 1:17:54 PM'))
I'm pretty sure this will work on MySQL datetime or timestamp as well.
Credit to david grayson Ruby: combine Date and Time objects into a DateTime

What format do I write my date into Core Data for an attribute with date type?

I'm trying to populate the sqlite database used by my core date model with the proper type of date for each entry (one for each day in the month). What format should I store the date in the sqlite database so that my app can display it correctly or at least receive a format it understands which I can tweak using date formatter?
Thanks :)
If you're saving dates, you read and write NSDate objects. They do not have a date format-- they just store an absolute time offset since the reference date.

Resources