How to convert string Date to DateTime when you are selecting record through LINQ? - list

I have a table that contains the entire history of the actions which are performed on the system by the users. I need to retrieve all those records in date descending order. The problem is I have saved the date in string format in my table, since my application support multi-culture. I don't know it was a good idea or bad, now I need to retrieve them in date descending order. So, when I am retrieving the data I am trying to convert the date to DateTime format, but I am getting an exception to invalid date format. In my database, the date is in dd-MM-yyyy format. Following is my query:
db.Histories.ToList.OrderByDescending(x=> Convert.ToDateTime(x.Date));

It seems your data is not all of valid datetime format's.
You must cleen up the data first, I think.
Or you could sort like this:
var tmp = array.OrderByDescending(x => ParseDate(x.DateTest));
private static DateTime ParseDate(string dateString)
{
var dateFormats = new[]
{"dd-MM-yyyy", "dd/MM/yyyy", "M/d/yyyy", "d.M.yyyy", "dd.MM.yyyy", "MM/dd/yyyy", "M/d/yyyy"};
try
{
return DateTime.ParseExact(dateString, dateFormats, CultureInfo.CurrentCulture,
DateTimeStyles.None);
}
catch (Exception ex)
{
return DateTime.Now.AddYears(-500);
}
}
Where the not valid datetime strings will end up last in the sorted list.

Related

google visualization date format in getValue

Where do I set the data format when displaying data from a dataTable? The code below is not working.
a.getDataTable().getValue(0, 2, {pattern: "dd-MM-yyy"})
first, you must format the data table values.
you can use one of Google's formatters, in this case the DateFormat class.
once the data table has been loaded with data,
use the formatter to format the data.
// create date formatter
var formatDate = new google.visualization.DateFormat({
pattern: 'dd-MM-yyyy'
});
// format data table column index 2
formatDate.format(dataTable, 2);
then when displaying data from the data table,
use the getFormattedValue method, to display the formatted value...
dataTable.getFormattedValue(0, 2);

Flutter : List<Document>, how to remove duplicate documents according to its title

Let's say I have a class Document like this :
class Document {
String text;
String title;
String date;
}
I created a List of Document (List Document listDocs), and I would like to delete documents that have the same title, and keep ONLY the one with the earliest date.
For example if I have 3 documents with same title (but different texts and dates), I want to keep only the document with the earliest date.
Is there an easy way ?
thanks !!
To sort the list of objects by its date, you can do MyDocumentList.sort((a, b) => a.date.compareTo(b.date));
To make sure there is no second object with the same title you can use a Set. which is
A collection of objects in which each object can occur only once.
In this set you would store all the seen titles. Then you would go over MyDocumentList, try to store its title and only add a new Document to uniqueDocuments, if seenDocumentTitles does not contain the title of the current Document (if seenDocumentTitles.add returns true, meaning the set didn't contain the documents title)
Set<String> seenDocumentTitles = Set<String>();
List<Document> uniqueDocuments = MyDocumentList.where((document) => seenDocumentTitles.add(document.title)).toList();
For an example check this out:
https://dartpad.dartlang.org/20a889b174d2e45f7255d1b110be0627
You can add a function that returns an earlier date time object from two given dates like so:
DateTime longer(String dateString, String dateString2) {
DateTime dateTime = DateFormat("yyyy-MMMM-dd").parse(dateString);
DateTime dateTime2 = DateFormat("yyyy-MMMM-dd").parse(dateString2);
return dateTime.isBefore(dateTime2) ? dateTime : dateTime2;
}
Then, to check which date amongst the three is earlier, you add a method to your class like:
DateTime longest(String dateTime, String dateTime2) {
DateTime thisDate = DateFormat("yyyy-MMMM-dd").parse(this.date);
return thisDate.isBefore(longer(dateTime, dateTime2)) ? thisDate : longer(dateTime, dateTime2);
}
If you want to decouple that method from your Document class you can do so:
DateTime longest(String dateTime, String dateTime2, String dateTime3) {
DateTime firstDate = DateFormat("yyyy-MMMM-dd").parse(dateTime);
return firstDate.isBefore(longer(dateTime2, dateTime3)) ? firstDate : longer(dateTime2, dateTime3);
}

AWS Athena query error when trying to filter by date

I am trying to use Athena to query some data I have stored in an s3 bucket in parquet format. I have field called datetime which is defined as a date data type in my AWS Glue Data Catalog.
When I try running the following query in Athena, I get the error below:
SELECT DISTINCT datetime
FROM "craigslist"."pq_craigslist_rental_data_parquet"
WHERE datetime > '2018-09-14'
ORDER BY datetime DESC;
And the error:
Your query has the following error(s):
SYNTAX_ERROR: line 3:16: '>' cannot be applied to date, varchar(10)
What am I doing wrong here? How can I properly filter this data by date?
the string literal that you provide has to be casted to a date, in order to compare to a date.
where datetime = date('2019-11-27')
its having issue with the string literal used for date filter. Use WHERE datetime > date '2018-09-14'
from_iso8601_date or date should work.
SELECT DISTINCT datetime
FROM "craigslist"."pq_craigslist_rental_data_parquet"
WHERE datetime > from_iso8601_date('2018-09-14')
ORDER BY datetime DESC;
both return a proper date object.
SELECT typeof(from_iso8601_date('2018-09-14'))
Bit late here, but I had the same issue and the only workaround I have found is:
WHERE datetime > (select date '2018-09-14')

informatica datetime datatype format

I want to convert the string 20160101000000 into datetime format using expression. I have used below date function
TO_DATE(PERIOD_END_DATE),'MM/DD/YYYY HH24:MI:SS')
But my table file is not loading. My session and workflow gets succeed. My target and source is also flatfile.
I want to change the string 20160101000000 into MM/DD/YYYY HH24:MI:SS for loading data into my target table.
You need to give exact format that looks so that to_date function can understand that format and converts it into date.
TO_DATE(PERIOD_END_DATE,'YYYYMMDDHH24MISS')
So here your date looks like YYYYMMDDHH24MISS (20160101000000).
There is often confusion with the TO_DATE function... it is in fact for converting a string into a date and the function itself is to describe the pattern of the incoming date. Now if you want to convert a date field to a specified date format you must use TO_CHAR

Inserting date/timestamp values into SQL Server table using Python

I have three variables stored as number, string and string, as shown below.
load_id = 100
t_date = '2014-06-18'
p_date = '19-JUN-14 10.51.45.378196'
I would like to insert them into a SQL Server table using Python 2.7. The SQL Server table structure is as follows
load_id = float
t_date = date
p_date = timestamp
In Oracle, we tend to use TO_DATE or TO_TIMESTAMP to convert the string to DATE or TIMESTAMP field.
I would like to know how I can do similar conversion while inserting into an SQL Server table.
Thanks in advance.
convert with :
import datetime
import calendar
thedate=datetime.datetime.strptime(p_date,'%d-%b-%y %H.%M.%S.%f')
thetimestamp=calendar.timegm(thedate.utctimetuple())
https://community.toadworld.com/platforms/sql-server/b/weblog/archive/2012/04/18/convert-datetime-to-timestamp
DECLARE #DateTimeVariable DATETIME
SELECT #DateTimeVariable = GETDATE()
SELECT #DateTimeVariable AS DateTimeValue,
CAST(#DateTimeVariable AS TIMESTAMP) AS DateTimeConvertedToTimestampCAST
SELECT CAST(CAST(#DateTimeVariable AS TIMESTAMP) AS DATETIME) AS
TimestampToDatetime
Do the conversion with SQL instead of trying to get Python to match the SQL format.
Neither format matches yours, however the DATETIME type should be adequate.