how to convert a number to date in oracle sql developer - sql

I have a excel format dataset that need to be imported to a table, one column is a date, but the data is stored in number format, such as 41275, when importing data, i tried to choose data format yyyy-mm-dd, it gives an error: not a valid month, also tried MM/DD/YYYY also error: day of month must be between 1 and last day of month. does anyone know what is this number and how can i convert it to a date format when importing it into the database?Thanks!

The expression (with respect to the Excel's leap year bug AmmoQ mentions) you are looking for is:
case
when yourNumberToBeImported <= 59 then date'1899-12-31' + yourNumberToBeImported
else date'1899-12-30' + yourNumberToBeImported
end
Then, you may either
Create a (global) temporary table in your Oracle DB, load your data from the Excel to the table and then reload the data from the temporary table to your target table with the above calculation included.
or you may
Load the data from the Excel to a persistent table in your Oracle DB and create a view over the persistent table which would contain the above calculation.

The number you got is the excel representation of a certain date ...
excel stores a date as the number of days, starting to count at a certain date ... to be precise:
1 = 1-JAN-1900
2 = 2-JAN-1900
...
30 = 30-JAN-1900
so, to get your excel number into an oracle date, you might want to try something like this...
to_date('1899-12-31','yyyy-mm-dd') + 41275

Related

Standard SQL query returning correct results in BigQuery but not in Data Studio

I need to return a list of names which have parameters which fall between 2 dates. I have hard-coded two dates as part of the data verification. My sql query in BigQuery is as follows:
declare DS_START_DATE FLOAT64;
declare DS_END_DATE FLOAT64;
SET DS_START_DATE = 1578390532050;
SET DS_END_DATE = 1578391211289;
SELECT DISTINCT
Name AS block_names
FROM
my_data_source
LEFT JOIN
UNNEST (holes) AS n_holes
ON
1=1
WHERE
(n_holes.LastModifiedHoleDate ) > CAST(DS_START_DATE as FLOAT64)
AND n_holes.LastModifiedHoleDate < CAST(DS_END_DATE as FLOAT64)
Note: the DS_START_DATE and DS_END_DATE are both in UNIX time.
So basically, I am querying for results that were modified on the 7th of January, which will return only one result.
The above query return only one result, which is correct.
I have changed the format of the query slightly so that I can use it in the connection to my table in BigQuery from Data Studio:
SELECT DISTINCT
Name AS block_names,
n_holes.LastModifiedHoleDate as LM
FROM
my_data_source
LEFT JOIN
UNNEST (holes) AS n_holes
ON
1=1
WHERE
(n_holes.LastModifiedHoleDate ) >= CAST(#DS_START_DATE as FLOAT64)
AND n_holes.LastModifiedHoleDate <= CAST(#DS_END_DATE as FLOAT64)
I have enabled the date parameters in my Data Studio data source, and have finished creating the data source.
I have then made a test report using the above data source for the report. I simply have a date range control and a chart on my report.
No matter what range I choose on the date range, I get two results on the chart, where I should only get one.
Normally the tables and charts I have used in Data Studio have the option to select a date range dimension which links the data to the date range selected on the date time picker:
The table that I have added to this report has no such option. I am assuming that this is correct because we are using a data source that needs a start and end date?
The parameters option in the chart has nothing in it:
Again, I am assuming that Data Studio doesn't need parameters specified because I am using start and end dates.
What have I missed that my date range picker is not affecting the data displayed in my chart? It seems like it is linked to the chart automatically, but the results are wrong.
Many thanks in advance!
You need to convert #DS_START_DATE and #DS_END_DATE into a Unix timestamp if you want to compare them with one. For me this did the trick:
WHERE
n_holes.LastModifiedHoleDate >= UNIX_MILLIS(PARSE_TIMESTAMP('%Y%m%d', #DS_START_DATE))
AND n_holes.LastModifiedHoleDate <= UNIX_MILLIS(PARSE_TIMESTAMP('%Y%m%d', #DS_END_DATE))
I hope it also works for you!

Limiting data on monthly basis from start date to system date dynamically in Tibco spotfire

I've tried limiting data on monthly basis in spotfire and it's working fine.
Now I'm trying to do like getting the records from the current date to month start date.
For suppose if the current date is Sept 21, then i should get the records from Sept 21 to Sept-01(dynamically).
I have a property control to input the number of months.
The easiest way to do this is with Month and Year. For example, in your visualization:
Right Click > Properties > Data > Limit Data Using Expressions (Edit)
Then, use this expression:
Month([TheDate]) = Month(DateTimeNow()) and Year([TheDate]) = Year(DateTimeNow())
This will limit the data to only those rows with the current Year/Month combination in your data column. Just replace [TheDate] with whatever your date column name is.
In other places, you can wrap this in an IF statement if you'd like. It's redundant in this case, but sometimes helps with readability.
IF(Month([TheDate]) = Month(DateTimeNow()) and Year([TheDate]) = Year(DateTimeNow()),TRUE,FALSE)
#san - Adding to #scsimon answer. If you would like to precisely limit values between 1st of the current month to current date, you could add the below expression to 'Limit data using expression' section.
[Date]>=date(1&'-'&Month(DateTimeNow())&'-'&year(DateTimeNow())) and [Date]<=DateTimeNow()

Store date range in a single column in Oracle SQL

Here trip 1 involves 2 activity_code in a single day and also concludes in a single day and most other activities are just single day but i have one trip that span over more than one day.
What could be the best possible way to store date range for that column that span more than one days.
Splitting the column into multiple begin date and end date just doesn't make sense as there would be many blank columns?
trip_id(pk,fk) Activity_code(pk,fk) date
1 a1 1st October 2015
1 a2 1st October 2015
2 a3 2nd -5th October 2015
Keep in mind that i need to search the activity_code on basis of month. such as list all the activity code that occur in October ?
Is it possible to insert a range of date in a single column or any other design solution ?
Is there any datatype that can represent the date range in single value ?
PS: oracle 11g e
Store the date ranges as FirstDate/LastDate or FirstDate/Duration.
This allows you to store the values in the native format for dates. Storing dates as strings is a bad, bad idea, because strings don't have all the built-in functionality provided for native date types.
Don't worry about the additional storage for a second date or duration. In fact, the two columns together are probably smaller than storing the value as a string.
Splitting the date into start date and end date would be ideal. Storing dates as strings is not recommended. If you store your dates as strings then there is a possibility of malformed data being stored in the column since a VARCHAR2 column will allow any value. You will have to build strong validations in your script while inserting the data which is unnecessary.
Secondly, you will not be able to perform simple operations like calculating the duration/length of the trip easily if both the start_date and end_date are stored in the same column. If they are stored in different columns it would be as simple as
SELECT trip_id, activity_code, end_date - start_date FROM trips;

SQL on IBM i - Selecting data based on combined fields from DB

We are trying to select records from a file containing sales data where the date in the IBM i file is stored in 3 separate numeric fields (Year, Month, Day). We have a .net application that is using date pickers to select a from and through date range. Is there a way in an SQL statement to combine the 3 fields from the database into a single value so that the database date can be used to make the SQL select easier?
Use the digits command to tie them together. Your parameters will be YYYYMMDD.
Where digits(year) || digits(month) || digits(day) BETWEEN ? and ?

Difference in minutes from datetime

I'm trying to obtain the total amount of time difference from two timestamp columns (datetime)
I currently have a Table 1 setup like the following:
Time_Line_Down => datetime
Time_Line_Ran => datetime
Total_Downtime => Computed column with formula:
(case when [Time_Line_Down] IS NULL then NULL else CONVERT([varchar],case when [Time_Line_Ran] IS NULL then NULL else [Time_Line_Ran]-[Time_Line_Down] end,(108)) end)
Every time some conditions occur, I am copying those three columns (I have more columns but the problem is on this ones) into another Table 2 originally setup like the following:
Time_Line_Down => datetime
Time_Line_Ran => datetime
Total_Downtime => datetime
I then use an excel spreadsheet to "Get External Data" from SQL Server and use a pivot table to work with the data.
Example
Time_Line_Down = 2015-02-20 12:32:40.000
Time_Line_Ran = 2015-02-20 12:34:40.000
Total_Downtime = 1900-01-01 00:02:00.000
Desired Output
I want the pivot table to be able to give me a Grand Total of downtime from all rows in that table
Let's say it was forty five hours, fifty minutes and thirty seconds of accumulated downtime it should read like (45:50:30)
The problem:
Even if I format the Total_Downtime column in the excel pivot table as h:mm:ss to read like this:
Total_Downtime = 0:02:00
As rows accumulate and the Grand Total is calculated the "Date" part of the timestamp is messing the result is the total exceeds 24 hours
What I have tried
I changed the data type format of column Total_Downtime in Table 2 to time(0) so that it won't send the "Date" part, only the "Time" part of the timestamp, it is working and reads out 00:02:00
But now all the values in my pivot table on excel for that column are 0:00:00 no matter what value is actually in the SQL table.
Any suggestions?
You can use the Excel time format [h]:mm:ss which can go beyond 24 hours.
Alternatively, you can use the SQL function DATEDIFF to get the total downtime in seconds, and then convert that to however you need to display it in Excel, e.g.
case when [Time_Line_Down] IS NULL then NULL else case when [Time_Line_Ran] IS NULL then NULL else datediff(ss, Time_Line_Ran, Time_Line_Down) end end
I don't think you need the CASE statements here, you can just use
datediff(ss, Time_Line_Ran, Time_Line_Down)
Thank you all for your help,
I went ahead an tried the function DATEDIFF as suggested, I changed Table 1 computed column formula and Table 2 Total_Downtime column data type to int. Once imported into excel this numeric value needed some extra calculations.
In principle is the best answer and should work for anyone trying to calculate the difference from two timestamps, as mentioned before, is pretty straight forward.
But in my situation I needed to maintain two things:
1) The format 00:00:00 for the column Total_Downtime in Table 1, which changed to an integer value when using DATEDIFF
2) The pivot table Total_Downtime column format [h]:mm:ss (suggested by TobyLL) in excel, which required several calculations to convert from seconds
Solution
After learning that every time I copied from Table 1 to Table 2 the computed value (e.g. 00:02:00) changed to 1900-01-01 00:02:00.000 and that when imported to Excel it equaled to 1.001388889, I decided to force the "Date" part of the time stamp to be 1899-12-31 so that Excel would only calculate the Grand total in the pivot table with the "Time" (decimal) part.