SQL timestamp Query - sql

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 )

Related

SQLite - TZ format to Date time only using SQL queries

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.

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.

Date in where clause

I'm having to reverse engineer an Oracle query. I'm familiar with SQL but not Oracle and I'm using SQL OPENQUERY to the linked Oracle server. I've gone through the larger portions of the query and figured most of the syntax and getting results but when I get to the following in the where clause I get "missing expression" error. (simplified for clarity)
SELECT USER_DATE
FROM TABLE
WHERE USER_DATE >= {1}
I can query the table and see that the column USER_DATE is indeed a date field so I don't understand the meaning of >= {1}. This query came to me as "this is how the other dept uses this query, make it work for us" and I don't have access to this other dept. Can someone explain how this supposedly works?
The {1} is just a placeholder for an actual value. Once you've populated this with a proper date value it will run just fine like this:
SELECT USER_DATE
FROM TABLE
WHERE USER_DATE >= '01/01/2019'
You can also use the to_date function for a specific format:
SELECT USER_DATE
FROM TABLE
WHERE USER_DATE >= to_date('01/01/2019', 'mm/dd/yyyy')
Most likely it is a matter of default format of date in your remote Oracle database is different from the "other" database. Most databases allow you to select date into string and also.conpare date to string. The comparison implicitly converts string to date.
Most likely that implicit conversion is failing here. Correct way would be to see if the parameter is a really a string and in what format is date presented there. Then use to_date on right side around {1}.
To make it not error temporarily you can put to_char on left side. Ofcourse your output will be incorrect but since SQL would run, you may be able to troubleshoot

Categorizing date by week with dynamic alias name

We have one column in our table which stores a number of records in "date" type, we want to separate them by each week and find the number of repetitions for each week that we have in our column.
the database is in oracle.
I think for creating a dynamic alias name we need cursor.
this image below may help:
you can use mysql function week()
here the info: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week
an example
select concat('Week ',week(date)), count(date)
from table
group by week(date)
Try this syntax;
SELECT to_char(date,'WW') week, COUNT(date) number
FROM your_table
GROUP BY to_char(date,'WW')
Have a look at TO_CHAR function.

how to find fifference between 2 dates in DB2

In my database i have a TIMESTAMP field.Now I want to find the difference between the value stored in this field and the current TIMESTAMP .I want the result in days.
How can i get it?
Note:I am using DB2 database
days(TIMESTAMP)_-days(your_col)
You don't say what version of DB2 you're using.
In older versions, you have to SELECT against a table, even though you're not retrieving any columns.
SELECT days(TIMESTAMP)-days(your_col)
FROM SYSIBM.SYSTABLES
Substitute a creator and table that you have SELECT authority for.
I shall reiterate what Gilbert answered. He was correct, but I don't think the original author understood the idea. As an example:
create table test ( mycolumn timestamp )
insert into test values (current timestamp)
select * from test
select days(current timestamp)-days(mycolumn) from test
The current timestamp is a special register that DB2 has available. Days is a default function that will return the number of days component of the given timestamp counting from January 1, year 1.
The CURRENT_DATE returns the current timestamp then this can be used
DAYS(CURRENT_DATE)-DAYS(TIMESTAMP)