Converting number to date in oracle - database

I have a numeric field in my Oracle database that represents the number of days since Dec 28, 1800. However I am trying to select it (for another application) as the current date it represents. I'm not too familiar with Oracle commands (I'm used to SQL), so I was wondering if anyone could provide some assistance. Thanks.
ex: 77650 = Saturday, August 3, 2013

Firstly, get this out of the way, your life would be easier if you stored dates in a date data-type.
However, to answer your question to add days to a date in Oracle you can use the + operator.
Firstly though you have to have a date so I'll convert the 28th December 1800 into a date using to inbuilt to_date function then add the number. In your case you would want:
select to_date('1800/12/28','yyyy/mm/dd') + 77650 from dual
I've set up a little SQL Fiddle to demonstrate for you.

Related

casting INT to Datetime date is 2 days off

So I am parsing out a large text field with several dates in it. the date format comes out like "44445" which should be "9/6/2021" but when I convert to datetime in Microsoft SQL Server 2019 i get "9/8/2021" I found that every date that I convert is 2 days off. I can of course just do a -2 before converting to make sure I get the right date but I found this really odd and wondered if anyone knew why this was happening and if I am doing something wrong
cast(44445 as datetime) result 2021-09-08 00:00:00.000
if I put the same date in excel I get "9/6/2021" which is the correct date
I found something online about excel calculating 1900 as a leap year and therefore having a different date but excel seems to be correct and SQL statement seems to be wrong so I don't know...

How to convert custom string date format to desired output SQL

In SQL server, I have a date string that looks something like 09/08/2021. Representing dd/MM/YYYY. So in this case its 09 August 2021. Every method I attempt converting this explicitly into a date format automatically converts this to 2021-09-08. So SQL incorrectly converts to 08 September 2021.
SELECT TRY_CAST(Convert(VARCHAR(15),'09/08/2021',103) as date);
SELECT TRY_CAST(Convert(VARCHAR(15),'09/08/2021',111) as date);
Both incorrectly give:
2021-09-08
Desired result is:
2021-08-09
I've tried the various different versions of the above but keep getting the incorrect conversion. I don't really want to go down the road of changing the source data either.
I am amazed no similar question has been previously asked regarding this.
How can this be converted explicitly using functions in SQL Server?
As per the comments, it was the conversion that was the mistake. Adjusted to
TRY_CONVERT(DATE, '09/08/2021', 103)
This fixes the issue.

DATEADD Only weekdays

I am trying to produce a report that will pull a date already in a table somewhere and then show this date with 16 working days added on, below is the code I currently have;
SELECT d.CASENUM
,DATEADD (DW, 16, (d.ORDERDATE)) AS [PayDate]
FROM db.ORDERS AS d
WHERE d.CLIENT = 'STORE1'
The only issue I seem to having is that the DATEADD isn't adding 16 working days
27-01-2021 Is the last date in my table, so +16 working days should be 18-02-2021
but my results are giving me a date off 12-02-2021 and I can't figure out why..
I am using SQL Server Management Studio v18
Any help would be greatly appreciated.
I'm afraid what you'll have to do is create a new table for the Working Days. The issue is that different countries (and states) have different definition for Working Days.
So the table should look something like this
Date
State (or Locality)
Day of Week
Is Working Day
You can either use a running id for Working Days, alternatively use Windows Functions (RANK)Rank Windows Function

Microsoft Query Current Month

Sorry guys - new to this - complete novice.
I am pulling data from a SQL Server from Excel using Microsoft Query.
I'm currently limiting fields to just invoices within a date range using:
=#10/1/2017# And <#10/31/2017#
My date format is:
2017-10-02 00:00:00.000
I run this report many times during the current month - so I need to change the string above when the new, current month, begins.
I'd love for someone to give me the command that will always pull the current month - regardless of the month - thus allowing me to not have to alter the condition when a new month begins.
Thank you in advance.
The question is not so clear but here is what you can try with:
To find today with date and time:
=NOW()
To find the beginning of the month:
=EOMONTH(A2,-1)+1
To find the end of the month:
=EOMONTH(A2,0)
Let me know if this is what you are expecting.

SQL date values converted to integers

Ok, I can't understand this thing.
A customer of mine has a legacy Windows application (to produce invoices) which stores date values as integers.
The problem is that what is represented like '01.01.2002' (value type: date) is indeed stored in SQL Server 2000 as 731217 (column type: integer).
Is it an already known methodology to convert date values into integers (for example - I don't know - in order to make date difference calculations easier?)
By the way, I have to migrate those data into a new application, but for as much I googled about it I can't figure out the algorithm used to apply such conversion.
Can anybody bring some light?
It looks like the number of days since Jan 1st 0000 (although that year doesn't really exists).
Anyway, take a date as a reference like Jan 1st 2000 and look what integer you have for that date (something like 730121).
You then take the difference between the integer you have for a particular date and the one for your reference date and you that number of days to your reference date with the DATEADD function.
DATEADD(day, *difference (eg 731217 - 730121)*, *reference date in proper SQLServer format*)
You can adjust if you're off by a day a two.

Resources