How to convert DateTime to MSSQL integer in java - java

I have to use a web service, where one of the parameters is date represented as a 5-digit number like this:
DATETIME INT
--------- ----
1/1/2009 39814
2/1/2009 39845
3/1/2009 39873
4/1/2009 39904
After googeling this I found out that this is MS-SQL representation. Do you guys have any idea how to make this integer in Java?
Thanks

That integer is the number of days since 1899-12-30. This is how Microsoft Excel stores dates, not SQL Server.
--integer to date
SELECT DATEADD(day,#integer,'1999-12-30')
--date to integer
SELECT DATEDIFF(day,'1999-12-30',#date)

Related

#SqlDelete bigint to timestamp [duplicate]

How do I convert the following format to UNIX timestamps?
A value like: 01-02-2015 10:20 PM should be converted to: 1418273999000
I did try to_timestamp function but its not working for me.
If your data is stored in a column called ts, in a table called data, do this:
select extract(epoch from ts) from data
To add Joe's answer, you can use date_part, i think it's syntax is clearer than 'extract'.
select date_part('epoch', ts) from data;
Adding to haoming answer,
for UNIX epoch this was my approach.
I also added a 180 day interval which can be changed/removed upon requirements.
date_part('epoch', (column_name + INTERVAL '180 day')) * 1000

How to convert db2 date format to H2 date format for in memory testing in java

I am trying to convert the DB2 data base Date field format into H2 in memory database date format as shown below. It did not work for me.
Please help.
select CREATE_DATE from PX.MY_DB2TABLE ;
Here DB2 DATE format in database table is 'MM/DD/YYYY'. CREATE_DATE is DATE data type in db2.
H2 database is accepting only 'yyyy-MM-dd' format.
Tried the following way in H2:
select PARSEDATETIME(CREATE_DATE,'MM/dd/yyyy') FROM PX.MY_DB2TABLE;
select PARSEDATETIME(CREATE_DATE,'yyyy-MM-dd') FROM PX.MY_DB2TABLE;
Error:
Cannot parse DATE constant
To obtain a character-string representation from a DATE data-type value with the format 'YYYY-MM-DD', the DATE field selected in the original query [from the OP] can be wrapped in a CHAR casting scalar with an additional argument requesting that particular date-formatting for the Datetime to character casting; no mention of which variant of DB2, but here is some doc: DB2 for Linux UNIX and Windows 9.7.0->Database fundamentals->SQL->Functions->Scalar functions->CHAR:
select CHAR(CREATE_DATE, ISO) from PX.MY_DB2TABLE ;
For the second argument, see Date Strings Table 1. Formats for String Representations of Dates in [imagining LOCAL as the valid argument, in place of the LOC as seen in the table]:
DB2 for Linux UNIX and Windows 9.7.0->Database fundamentals->SQL->Language elements->Data types->Data type list->Datetime values

Java Timestamp to BigInt for Impala

I am reading a text file which has a field in Timestamp in this format "yyyy-MM-dd HH:mm:ss"
I want to be able to convert it to a field in Impala as BigInt and should like yyyMMddHHmmss in Java.
I am using Talend for the ETL but I get this error "schema's dbType not correct for this component"
and so I want to have the right transformation in my tImpalaOutput component
One obvious option is to read the date in as a string, format it to the output you want and then convert it to a long before sending it to Impala.
To do this you would start by using Talend's parseDate function with something like:
TalendDate.parseDate("yyyy-MM-dd HH:mm:ss",row1.date)
This parses the date string into a Date type object. From here you can convert this into your desired string format with:
TalendDate.formatDate("yyyMMddHHmmss",row2.date)
Alternatively this can be done in one go with:
TalendDate.formatDate("yyyMMddHHmmss",TalendDate.parseDate("yyyy-MM-dd HH:mm:ss",row1.date))
After this you should have a date string in your desired format. You can then cast this to a Long using a tConvertType component or the following Java code:
Long.valueOf(row3.date)
Or, once again we can do the whole thing in a one liner:
Long.valueOf(TalendDate.formatDate("yyyMMddHHmmss",TalendDate.parseDate("yyyy-MM-dd HH:mm:ss",row1.date)))
From here you should be able to send this to Impala as a Java Long to an Impala BIGINT field.

Difference between a range of dates to dates already registered

I need an way of to calculate the difference of a range of dates to other dates already persisted in database, i.e.:
As arguments I would like to inform something like:
from 15/JAN/2013 to 27/JAN/2013
In database/collection I have:
START_DATE, END_DATE
-------------------------------------------
01/JAN/2013 00:00:00, 17/JAN/2013 22:30:00
23/JAN/2013 17:00:00, 31/JAN/2013 23:59:00
And the return must be:
17/JAN/2013 22:30:01, 23/JAN/2013 16:59:59
REASON:
I have files that contains up to 40 thousand rows in range of dates. Files that intersect between dates contains the same data. This is why I need to insert only data in date range gaps.
Thanks to everyone that could help me!
select start_gap, end_gap
from(
select
end_date start_gap,
lead(start_date) over (order by start_date) as end_gap
from your_table
)
where end_gap is not null; --last line is not interesting
The question is unclear. Use Between operator: where dates between 15/JAN/2013 and 27/JAN/2013. And you will get exactly what is in your return.
Try this:
select START_DATE,END_DATE from tablename where START_DATE>max(START_DATE) and END_DATE<max(END_DATE)

Empty result set from a Compass Lucene query with Dates

I am using Compass to make queries on data inside in memory data structure. It works fine for searching String and enum values, now I want to search dates.
Search criteria are annotated by #SearchRestriction annotation. Example about someDate:
#SearchRestriction(path="fooBar.someDate" type = SearchRestrictionType.EQUAL)
String someDate;
At searchable data SomeDate is annotated like the following:
#SearchableProperty
Date someDate;
SomeDate inside the searchable data is generated with new Date();) and query String is given as 20120802.
Situation on debugger:
This code generates queries like this:
someDate:20120802
Here someDate is the name of the field I am looking for and 20120802 is a date in order yyyyMMdd.
Problem:
No results is returned, when this query is run. I get an empty list. The Date in query is the same as in the Date object.
What is wrong??
Is this wrong way to search Dates with Compass? I can find only range queries about Date, but a search with exact Date or part of exact Date I cannot find.
You need to specify the format for Searchable property [Date]
#SearchableProperty(format = "yyyyMMdd")
To some extent, it relates to Grails: Lucene, Compass Query Builder and date ranges

Categories

Resources