What number formats would to_number(to_char(sysdate, 'ydddhh24mi')) produce? - sql

We're transferring a legacy Oracle SQL application to a new platform and I cannot locate proper date format documentation.
What type of numbers would be produced by
SELECT to_number(to_char(sysdate, 'ydddhh24mi')) FROM dual
if sysdate were
January 3, 2015 at 6:04 AM
December 23, 2000 at 4:17 PM

TO_CHAR documentation for Oracle 8 is at http://docs.oracle.com/cd/A87861_01/NT817EE/index.htm; this leads to the format model documentation at http://docs.oracle.com/cd/A87861_01/NT817EE/server.817/a85397/sql_elem.htm#34512. Using the table there...
y returns the last digit of the year.
ddd returns the day of the year.
hh24 returns the military hour of the time.
mi returns the minute of the hour.
For January 3rd, 2015 # 6:04 AM this is 50030604.
For December 23rd, 2000 # 4:17 PM this is 03571617.

Related

Why is DATEDIFF parsing these dates as dd/mm/yyyy?

I do:
SELECT DATEDIFF(month, Convert(datetime,'01/10/2018',103),
Convert(datetime,'30/04/2019',103)) AS 'Month1',
DATEDIFF(month, '10/01/2018','04/30/2019') AS 'Months2',
DATEDIFF(month, '10/02/2018','05/01/2019') AS 'Months3'
Please show me why it return 6,6,7?
here date format is day month year
DATEDIFF(month, Convert(datetime,'01/10/2018',103),
Convert(datetime,'30/04/2019',103)) AS 'Month1'
so difference of 10 month(october) and 4(april) month =6
below code date format is month day year
DATEDIFF(month, '10/01/2018','04/30/2019') AS Months2,
so again 10(october) and 4(april) month difference is 6
and the 3rd date format is like 2nd
DATEDIFF(month, '10/02/2018','05/01/2019') AS Months3
so difference between 10(october) 2018 and may(5) 2019 is 7
As a result your query showing output 6,6,7
The first 6 is the difference (in months) between 01/10/2018 (October 1, 2018) and 30/04/2019 (April 4, 2019). The CONVERT call is to force the second date to be parsed using date format 103 (i.e. British/French, i.e. dd/mm/yyyy), which makes me suspect that your SQL implementation is set up to parse them as some other format (e.g. US: mm/dd/yyyy).
The second 6 is the difference (in months) between 10/01/2018 (October 1, 2018) and 04/30/2019 (April 30, 2019).
The 7 is the difference (in months) between 10/02/2018 (October 2, 2018) and 05/01/2019 (May 1, 2019). The fact that these dates could be parsed as Feb 10 and Jan 5, but aren't, confirms that your SQL implementation is parsing dates in mm/dd/yyyy format.
If you are really passing dates as strings, I recommend using an unambiguous format (e.g. "October 1, 2018"), as it will make the code clearer, and less brittle.

How to get how many days passed since start of this year?

I have a query which uses needs to know how many days passed since 1st of January in the current year.
Which means that if the query runs for example in:
2nd Jan 2017 than it should return 2 (as 2 days passed since 1st Jan
2017).
10th Feb 2016 than it should return 41 (as 41 days passed since 1st
Jan 2016).
basically it needs to take Current Year from Curent Date and count the days since 1/1/(Year).
i have the current year with: SELECT EXTRACT(year FROM CURRENT_DATE);
I created the 1st of Jan with:
select (SELECT EXTRACT(year FROM CURRENT_DATE)::text || '-01-01')::date
How do I get the difference from this date to Current_Date?
Basically this question can be Given two dates, how many days between them?
Something like age(timestamp '2016-01-01', timestamp '2016-06-15') isn't good because I need the result only in days. while age gives in years,months and days.
An easier approach may be to extract the day of year ("doy") field from the date:
db=> SELECT EXTRACT(DOY FROM CURRENT_DATE);
date_part
-----------
41
And if you need it as a number, you could just cast it:
db=> SELECT EXTRACT(DOY FROM CURRENT_DATE)::int;
date_part
-----------
41
Note: The result 41 was produced by running the query today, February 9th.
Given two dates, how many days between them
Just subtract one from the other.
In your case you could just round the current_date to the start of the year and subtract that from the current date:
select current_date - date_trunc('year', current_date)::date
The ::date cast is necessary to get the result as an integer, otherwise the result will be an interval.
Another solution is to use DATEDIFF
SELECT DATE_PART('day', now()::timestamp - '2016-01-01 00:00:00'::timestamp);

PL/SQL Food Industry "Julian" Date Conversion

I have some 5 digits dates in a format that the food industry generally calls "Julian" dates (Warning: these are not the "Julian" dates you are familiar with. It's misused terminology that "stuck" and became the standard in the industry. Don't comment on that, it's just how it is.)
The first 3 digits of these "Julian" dates are a number representing the day of a year.
Example:
January 1 = 001
January 2 = 002
December 31 = 365 or 366
The next two digits are the last two digits of the year. 2015 = 15.
For example 22215 = August 10, 2015 (I believe).
I need an Oracle SQL statement that convers these "dates" into standard dates to join to other date data.
I'd add (count of days) -1 to 1st Jan of year
select to_date('20'||15||'01.01','yyyy.mm.dd') + 222-1 from dual
10/08/2015
A co-worker found the answer. The "DDD" format:
SELECT to_date('36515', 'DDDYY') FROM DUAL

SQL Between Two Dates Missing End Date (SSRS)

I'm using SSRS 2008r2 to generate reports. Using following WHERE statement in SQL query
WHERE (NonPMJobs.npmJobCreateDate >= #Created_Parameter) AND
(NonPMJobs.npmJobCreateDate <= #Created_Parameter2)
I'm using parameters with the data type of DateTime. Users then select day from a calendar. I want to get results where jobs have been created between date 1 (#Created_Parameter) AND date 2 (#Created_Parameter2) INCLUSIVE.
But results being returned do not include the second date (#Created_Parameter2). If I select 01/07/2013 - 05/07/2013 I get 01, 02, 03, 04 but no 05. If I select 01/07/2013 - 06/07/2013 I get 01, 02, 03, 04, 05.
I've tried using:
WHERE (NonPMJobs.npmJobCreateDate BETWEEN #Created_Parameter AND #Created_Parameter2)
but get same results.
What am I missing here and why isn't WHERE statement inclusive? Any pointers would be very much appreciated.
Well, you need to think about this: a DATETIME like this: 05/07/2013 means the 5th of July (or 7th of May - depending on your locale) at midnight when the day starts (a 00:00 hours, so to speak).
So this does NOT include the events that happen on that day!
The best solution would be to use
WHERE (NonPMJobs.npmJobCreateDate >= #Created_Parameter) AND
(NonPMJobs.npmJobCreateDate < DATEADD(DAY, 1, #Created_Parameter2))
and basically getting everything that's smaller than the next day based on #Created_Parameter2. So for your 5th of July, that would be anything before the 6th of July - which includes all events from the 5th of July.

Standardizing dates in SQL. Arithmetic overflow

I have a string field dob which has birthdates in various formats e.g.
dob
July 1, 1945
1967-1-7
13 May 1956
8 May 1947
27 September 1953
1952-3-25
I have attempted to use MS SQL 2005 to create a standardized date field
select convert(datetime,dob,103)
from myTable
I get an arithmetic overflow which appears to be associated with the last value, presumably because it is trying to convert the 25 to a month when it is a day
I have tried
setting the language to british and various other styles without success
Any suggestions?
Why would you attempt to store it in a given format? Can you not just store it as a datetime type? The biggest advantage to storing it in a locale independent is that you can offload the presentation logic to the UI where it belongs. Plus, you can avoid messy situations where you have a DOB of 3-4-5 (2003 April 5th, March 4th 2005, 3rd April 2005).
Conversion to a datetime works just fine for me (US locale).
; with myTable (dob)as
(
select 'July 1, 1945'
UNION ALL SELECT '1967-1-7'
UNION ALL SELECT '13 May 1956'
UNION ALL SELECT '8 may 1947'
UNION ALL SELECT '27 september 1953'
UNION ALL SELECT '1952-3-25'
)
SELECT
cast(T.dob AS datetime) AS real_datetime
, T.dob
FROM
myTable T
Results
real_datetime dob
1945-07-01 00:00:00.000 July 1, 1945
1967-01-07 00:00:00.000 1967-1-7
1956-05-13 00:00:00.000 13 May 1956
1947-05-08 00:00:00.000 8 may 1947
1953-09-27 00:00:00.000 27 september 1953
1952-03-25 00:00:00.000 1952-3-25