SQLite - TZ format to Date time only using SQL queries - sql

I have a SQLite database with a simple table in the format of:
ID,DateText,Name
1,2020-09-01T18:57:17Z,John
2,2022-12-01T04:00:09Z,Laurel
...
The DateText column is declared as TEXT on the "create table" statement. Using only SQL, I need to:
Create a new column with the DateText data.
Obtain the "oldest" date
Obtain the "newest" date
Note that I need to resolve this with a SQL query. I cannot read into a programming language, parse, and update table--I need to do everything on SQL. For example, SQL Server DateTime with timezone is the opposite, but they are using node.js, something I cannot do.

You can get the oldest and newest using min() and max():
SELECT ID, min(DateTime), Name FROM YourTable; -- Oldest
SELECT ID, max(DateTime), Name FROM YourTable; -- Newest
The nice thing about ISO-8601 date and time format strings is that they sort lexicographically without having to do anything special with them.
These queries would give an error on most SQL database engines because of the mix of non-grouped columns with an aggregate function, but SQLite explicitly will return the row that goes with the minimum or maximum column value. And of course if you don't want the other columns, just leave them out of the SELECT.

Related

SQL query to retrieve last twelve months data for Oracle database

I am using Oracle database. I have a table called "TEST" where the dates/timestamps(These are stored as "Char" in my case) are stored in the following format. Now I want to retrieve the records of last twelve months based on today's date. What would be the correct way to do that?
TESTCOLUMN
------------
2019-06-28-02.01.07.327240
2020-06-28-04.49.12.480240
2020-06-28-05.05.10.681240
I think you need to use the ADD_MONTHS function and BETWEEN clause as follows:
SELECT * FROM YOUR_TABLE
WHERE TO_TIMESTAMP(YOUR_COLUMN,'YYYY-MM-DD HH24.MI.SS.FF')
BETWEEN ADD_MONTHS(SYSTIMESTAMP,-12) AND SYSTIMESTAMP;
Although storing values in a string is not recommended, your format is comparable. So you can do the comparison using strings rather than date/timestamps. Assuming your values are only in the past:
where testcolumn >= to_char(SYSTIMESTAMP, -12), 'YYYY-MM-DD HH24.MI.SS.FF')
This has an advantage over Tejash's solution, because this can make use of an index (or partitions) on testcolumn. Moving the date manipulations only on the "constants" (i.e. the system timestamp) helps the Oracle optimizer.

SQL timestamp Query

Table has Created_time column with values like 25-MAY-2012.10.12.320000 PM.
I need to write a query to display the all the fields from table XYZ which were created between the month of March and April, agnostic to the year they are in.
I would like to display the records in ascending order based on their id.
Can someone help me?
Check out month and year function.
if created_time is of type datetime:
select *
from TABLE
where month(Created_time) in (3, 4)
If you need to check whether the Created_time was in March of any year, then the idea is to do something like this:
select *
from XYZ
where Created_time like '%-MARCH-%'
order by id;
Or, if this is not supported for your column's type, then convert it to varchar. Also, there are some RDBMS-specific functions that could help you, but for a more thorough answer I would need to know the RDBMS you are using and the type of Created_time.
EDIT
As pointed out by Jarhl, EXTRACT should be usable in this case to check the month. Source, proving that he is right says:
Name
EXTRACT
The ANSI SQL scalar function for extracting parts from a date is
EXTRACT. ANSI SQL Standard Syntax
The ANSI SQL EXTRACT function takes a date_part and an expression that
evaluates to a datetime value. MySQL, Oracle, and PostgreSQL support
the ANSI SQL standard syntax:
EXTRACT( date_part FROM expression )

Validate dates before conversion, aka. ISDATE() equivalent

DB2 version is 9.7.0.7
I have a flat file, and need to validate fully prior to insert into a production table. For analysis, I've parsed it into a table where all columns are VARCHAR.
One of the tasks is to validate dates. I need to be able to locate the specific invalid dates, to report on the scope (frequency) and solution (reason).
I use ISDATE() in Sybase and SQL Server, which returns a 1 for a valid date, and a 0 for an invalid date. In Teradata, I left join to the SYS_CALENDAR table in the system catalog. It's been about 15 years since I've last been in a DB2 environment, but I believe analogs to either do not exist.
In this DB2 environment my role is limited to QA, meaning I cannot create T-SQL procedures or UDFs.
This thread is clever and makes me think there may be some Common Table Expression logic that could be employed in a query:
ISDATE equivalent of DB2
That one falls short as a solution, however, because it only considers format - the presence of an invalid (but properly formatted) date like '2016-04-31' or '2016-02-30' will raise an error and the query will return no rows.
I need to return all rows, identifying if each is valid or invalid (or just return the invalid rows for investigation, even) - so doing a CAST or CONVERT, or inserting into a formatted table in a test environment won't work.
Is there an analog to ISDATE(), SYS_CALENDAR, or another solution that gets to the same end product of a row-wise presentation of dates that can't be cast to DATE, prior to performing that conversion/insert?
You can do it with the PureXML extension as follows:
SELECT
XMLCAST(XMLQUERY('string($D) castable as xs:date' PASSING mycolumn as D ) AS INT)
FROM
mytable
which will return 1 or 0.

SSIS: Creating a variable expression but it is throwing error "DT_WSTR" and "DT_DATE" are incompatible

I'm creating an incremental load which would be pulling data from ORACLE to SQL Server. The incremental load will be based off a MODIFIED_DATE column.
I have created a result set variable that stores the MAX modified_date from the destination table. So the engine will only check the rows of the MODIFIED_DATES that are greater than the variable and perform a lookup to see if the row needs to be added, updated or deleted.
So I have my MAX MODIFIED DATE RESULT SET and I also have created another variable that will house the SOURCE QUERY which will be have a WHERE clause that see if the MODIFIED_DATE column is greater than the MAX MODIFIED_DATE variable.
Example:
Select column_name,column_name
From table
Where modified_date > '"+ #[User::LastModifiedDate]+ "'"
It is throwing me an error off:
The data types "DT_WSTR" and "DT_DATE" are incompatible for binary operator "+". The operand types could not be implicitly cast into compatible types for the operation. To perform this operation, one or both operands need to be explicitly cast with a cast operator.
Now, I have done a ton of searching but I cant seem to find a way to do this. The only solution that I found online is to ADD A (DT_WSTR, 25) in front of the variable which causes the variable expression to evaluate and this is the only way I can get the variable expression to evaluate.
Example:
(DT_WSTR, 25) #[User::LastModifiedDate]+ "'"
When I run it it is telling me it is NOT A VALID MONTH
The MODIFIED_DATE column in the DESTINATION table is in SQL Server and it has a DataTime as the date type which reads like this:
2008-06-10 22:22:25.000
YYYY-MM-DD
The MODIFIED_DATE column in the SOURCE table in oracle reads like:
6/10/2008 10:22:25 PM
MM/DD/YY HH:MM:SS
How would I be able to resolve this? Also what do you think is the best way to perform an incremental load based the MODIFIED_DATE column? Is my way one of the more efficient ways or is there another route I can take?
You need to make your SSIS component call the following statement:
Select column_name,column_name
From table
Where modified_date > to_date('whateverformat','"+ (DT_WSTR,25)#[User::LastModifiedDate]+ "')"
The problem is you are tangling the strings versus dates. Your lastmodifieddate must be a string for the expression builder to function, but i suspect Oracle is expecting modified_date to be a date, so just use the to_date function

sorting data by date?

I am sorting my database depending on date. The date field in SQLite is nothing but a string, stored in the form of dd-mm-yyyy.
How do i carry out the sorting.
My idea is to create a dummy table.Convert the date in a format yyyymmdd. Then sort it simply using sort by date column.Then again drop the table.
Is there an efficient or a better way ?
You should recreate your database to store data as ISO date yyyy-mm-dd (as recommended) then the sorting will be fine in SQLite.
Otherwise from the above, you can always substring fields from this field, and ordey by them, but that is so oldschool. Too bad on Android you can't have user defined functions.
The better way is to have a Date datatype in your database. In that way you can easily:
SELECT * FROM table ORDER BY dateColumn;
or
SELECT * FROM table ORDER BY dateColumn DESC;
if you want it in the other order.
It will be easier for you if you just make it a date data type.
edit
Wrong link. Thanks for comment, here's the correct link for datatypes: http://www.sqlite.org/datatype3.html
edit