SQL Solr query Convert date to - sql

I'm interested in the question: how to convert date to number in millis with Solr SQL? Is it possible?

You have to use Function Queries (https://lucene.apache.org/solr/guide/6_6/function-queries.html)
For example: in the field returned by your query, just insert ms(2000-01-01T00:00:00Z) or ms(mydatefield)
http://localhost:8983/solr/job/select?fl=ms(2000-01-01T00:00:00Z)&indent=on&q=:&wt=json
result: 946684800000
Obs2: Dates are relative to midnight, January 1, 1970 UTC (you can use function queries and calculate milliseconds between to dates)
Obs1: your date field type (mydatefield in the above example) should be a TrieDateField

Related

Convert date to dateTtime format in SQL

I am trying to convert a date column (ie. 2012-10-02) to the first day of the year with time (ie. 2012-01-01T00:00:00) in sql.
Is there a way to do so in the SELECT query?
for BigQuery use below
select timestamp_trunc('2012-10-02', year)
with output
2012-01-01 00:00:00 UTC
Note - if you column is of date type - the output will be
2012-01-01T00:00:00
and finally, you can use datetime_trunc instead of timestamp_trunc and you will get expected result - 2012-01-01T00:00:00
Look at the YEAR() function.
It would allow you to extract just the year, and then just as the date and time you need.

Select rows which date (epoch) field equals a specific year [duplicate]

I store date from Calendar.getTimeInMilliseconds() in SQLite DB.
I need to mark first rows by every month in SELECT statement, so I need convert time in milliseconds into any date format using SQLite function only. How can I avoid this?
One of SQLite's supported date/time formats is Unix timestamps, i.e., seconds since 1970.
To convert milliseconds to that, just divide by 1000.
Then use some date/time function to get the year and the month:
SELECT strftime('%Y-%m', MillisField / 1000, 'unixepoch') FROM MyTable
Datetime expects epochtime, which is in number of seconds while you are passing in milliseconds. Convert to seconds & apply.
SELECT datetime(1346142933585/1000, 'unixepoch');
Can verify this from this fiddle
http://sqlfiddle.com/#!5/d41d8/223
Do you need to avoid milliseconds to date conversion or function to convert milliseconds to date?
Since sqlite date functions work with seconds, then you can try to
convert milliseconds in your query, like this
select date(milliscolumn/1000,'unixepoch','localtime') from table1
convert millis to seconds before saving it to db, and then use date function in sql query

Datediff function in Access Database

I am trying to use Datediff function while subtracting two dates. One date is with Date and time stamp and the other with date only. How to get the difference of dates?
Here Column1 is 7/11/2017 4:24:38 PM and Column2 is 15/12/2017 where there is no timestamp.
DateDiff("d",[Column1],[Column2])
Convert the date column into datetime using Format function. See example below.
EDIT: since you want the difference in days and decimal point, I get the difference in hours then divide by 24. You can be as accurate if you want by getting the difference in minutes or seconds but using a different divisor.
SELECT DateDiff("h",
Now(),
Format('04/05/2018','mm/dd/yyyy hh:nn:ss'))/24 AS Expr1;
result: 1.125 days

Date Time Difference in SQL 2008

How to get date and time difference between 10/12/2010 07:35:02 PM and 2010-11-19 21:51:01.713. Where first date is in MM-DD-YYYY format and Second date is in YYYY-MM-DD Format Rest is time it is also in different format as first format has "pm" in it. Please let me know how to write a query in sql 08 to calculate date and time difference?
The datetime data type in SQL Server is actually a 8-byte number. It may be represented in different formats to please humans but the format has no meaning to SQL Server itself.
To calculate the time difference between to datetime values you can use the built-in DATEDIFF function, which you can find details about here: http://technet.microsoft.com/en-us/library/ms189794.aspx
This will work thanks to the SQL Server ability to parse formatted dates for us:
select datediff(day, '10/12/2010 07:35:02 PM', '2010-11-19 21:51:01.713')
-----------
38

Convert date value to PST for comparison:Oracle

I have 2 questions:
I want to compare a field whose data type is "Date" against a given date. The DB is oracle and being a mysql guy I'm finding it difficult to come up with simple queries.
The field("date_closed") stores date in UTC format (24-Aug-2011 18:55:11 for example) and I want to convert it to PST for comparison.
I tried this query but it returns some extra rows in the data set(obviously):
select * from table1 where trunc(date_closed)=to_date('2011-08-24','yyyy-mm-dd')
How do I covert to PST format before comparison?
In the same query how do I compare "date_closed" against the current date?
You need the NEW_TIME function
Dates don't include timezone in Oracle, and are assumed to be in the database timezone (which may by UTC but probably isn't). You should look at the TIMESTAMP WITH TIMEZONE data types.
Also, bear in mind that if you are comparing to the current date - I assume that you want to strip off the timestamp and compare only the day.
So, if new_time(date_closed,'GMT','PST') translates the date , your where clause will be comparing something like
trunc(new_Time(date_closed,'GMT','PST')) = trunc(sysdate)
to get all records with date_closed on the current day in PST.