Date based on day of 365 Day Year - sql

I have a date in the format [last two year][day of 365]. Using either SQL or excel functions, I need to determine the date this pertains to. The year is always the last two of the 2000s. There are no dates from before 2007. Is there a way to easily do this? I'm using this for a quick check, and am trying to avoid a large coding time for this.

In SQL, the date-handling functions seem to vary widely by SQL vendor, and there don't seem to be any good standard ways of handling date offsets. However, if you were a bit more specific about the specific SQL server you're using, someone might be able to provide an answer that applies.
In Excel, you can build a date for the first day of the year (e.g. 2008-01-01) and then add the number of days numerically, subtracting 1 so that the first day is still 2008-01-01; the resulting value, when formatted as a date, should be the precise date value. For example, =DATE(2008,1,1)-1+320 returns a date of 11/15/08 (at least in LibreOffice Calc).

You can try this formula in Excel where your data is in A1
=DATE(LEFT(A1,2)+100,1,RIGHT(A1,3))

Related

Is it possible in SSMS to pull a DATETIME value as DATE?

I want to start by saying my SQL knowledge is limited (the sololearn SQL basics course is it), and I have fallen into a position where I am regularly asked to pull data from the SQL database for our ERP software. I have been pretty successful so far, but my current problem is stumping me.
I need to filter my results by having the date match from 2 separate tables.
My issue is that one of the tables outputs DATETIME with full time data. e.g. "2022-08-18 11:13:09.000"
While the other table zeros the time data. e.g. "2022-08-18 00:00:00.000"
Is there a way I can on the fly convert these to just a DATE e.g. "2022-08-18" so I can set them equal and get the results I need?
A simple CAST statement should work if I understand correctly.
CAST( dateToConvert AS DATE)

Convert nvarchar to date

Trying to convert a datetime format column (example value: 12-11-2020 18:15:06) which is actually a nvarchar into this date format: yyyymmdd
This is what I tried so far but I'm getting the following error:
What am I doing wrong?
There are many problems here.
Dates should not be stored as strings.
You lose the ability to perform any kind of date math or extract date parts.
You lose built-in validation (both invalid dates like February 31st and any garbage that doesn't even look like a date).
For example, we have no idea if 12-11-2020 is December 11th or November 12th, or if the data was entered consistently. Imagine a person from the US did some of the data entry and a colleague from Germany did the rest.
FORMAT() is the most expensive way to format a date (see this and this - these are articles about removing time altogether, but the overhead with FORMAT() is the same).
An index on MYDATE can't be used to satisfy this query, so you will do a full table scan every time, which will get worse and worse as the table grows.
You can perform your query in your given scenario without changing anything, but I highly recommend you fix the column and make sure data entry can't be arbitrary (use a date picker or calendar control so you dictate a consistent format).
If 12-11-2020 is December 11th:
WHERE TRY_CONVERT(date, MYDATE, 110) >= #DateVariable;
If 12-11-2020 is November 12th:
WHERE TRY_CONVERT(date, MYDATE, 105) >= #DateVariable;
Note that this still might not get the correct and logical results if some people thought they entered December 11th and others thought they entered November 12th.
You can see all the valid style numbers for CONVERT/TRY_CONVERT here.

Will different Windows Time settings mess a WHERE SQL Date statement in ACCESS?

I know that ACCESS's time format depends on your Windows time settings. I use ISO-8601 format (YYYYMMDD) so that I can get away with SQL WHERE statements like this one:
WHERE dates > #2020/02/15#
AND dates < #2021/01/30#
If I run the code from above in another computer, whose Windows time settings are for example DDMMYYYY, will the SQL statement no longer work? I could simply do something like this to solve that problem (will it though?):
WHERE dates BETWEEN Format(date1, "\#YYYY\/MM\/DD\#") AND Format(date2, "#YYYY\/MM\/DD\#")
EDIT: Time format has beign changed as pointed out by #Gustav. The question remains; will the first WHERE Statement no longer work on different Windows time settings? Will the second correct the problem?
In Access SQL, use octothorpes:
WHERE dates > #2020/02/15#
AND dates < #2021/01/30#
WHERE dates BETWEEN Format(date1, "\#YYYY\/MM\/DD\#") AND Format(date2, "#YYYY\/MM\/DD\#")
Nope, Windows time settings will mess with a lot of things, but not with ordering or comparisons with dates.
As long as the field is defined as a date (so with octothorpes, like Gustav said), the 2nd of February 2021 will be less the 11th of February 2021, even though that wouldn't be the case if you cast them to a string first.
Always try to keep columns as they are when filtering, so if dates is actually a date column (and not a formatted string), just use WHERE dates BETWEEN #2020/02/15# AND #2021/01/30#, no formats, no funky stuff. And note that especially when trying to keep your application working in all locales, it's important to avoid casting dates to strings, which can happen if you compare a date with a formatted string.

Grouping by month using a date column (DD.MM.YYYY HH24:MI:SS)

I am trying to create a monthly report for easy review and comparison of runtimes of certain processes.
The raw data needed for that is stored in a Oracle 11g database table, with the runtime being the difference between STARTDATE and ENDDATE which are both formatted as DD.MM.YYYY HH24:MI:SS.
One dateset is created for each run of a process, so there is a huge number of them stored in that table (several datasets per process per hour).
Now normally this would be pretty straight forward by just using a GROUP BY clause, however, I couldn't get it to work in this case. Most likely due to the DATE columns being involved with their special format.
Is there any possibility to group the data sets by the month which is stored within the STARTDATEcolumn? I already tried using some Oracle date functions which I found via googling, e.g. MONTH() and EXTRACT() , but so far I didn't get anywhere with it.
I would greatly appreciate any hints regarding this issue.
Thanks in advance and best regards,
daZza
To get a dates month in Oracle you can convert that part to a string and use this:
select
to_char(startdate, 'yyyymm') as startmonth,
....
from ...
group by to_char(startdate, 'yyyymm');

Date Conversion with SQL Server/Reporting Services

I have 2 fields in the database month (numeric) and year (numeric) and I want to combine them in a report that combines those 2 fields and format them with MMM-YYYY. e.g 7-2008 becomes Jul-2008. How do I do that?
DateSerial is the correct answer:
http://msdn.microsoft.com/en-us/library/bbx05d0c(VS.80).aspx
SSRS uses VB.Net for expressions. Use the expression editor to browse the available functions, one of which is DateSerial.
To format the date, set the Format property on the textbox. You should be able to use "MMM-yyyy" as the format.
Update: As Peter points out, you would specify the parameters as needed. If you just care about year and month, just supply a value of 1 for the day. Since you are formatting the value without the day component it really doesn't matter what value you use (as long as it creates a valid date).
=DateSerial(year, month, day)
Brannon's answer is correct except that he omits the fact that you merely specify a literal for the day. Any value between 1 and 28 will do.