Convert date format while using date_add function in SQL - mysql

I have a variable (order_date_key) that contains the number of days since 1/1/1900. For example, 42711 represents December 9, 2016 or 42711 days since 1/1/1900. I want to convert that variable to the week of the year (e.g., 2016-50)
I was able to convert the variable to yyyy-mm-dd format using the date_add function, but when I try to use the DATE_FORMAT function with it, it returns an incorrect week number. For example, 2016-12-09 converted to 2016-5, but that date isn't the 5th week of the year.
Here's the code I'm using. The format also doesn't seem to work when I use the '%' symbol.
SELECT order_date_key,
DATE_ADD('1900-01-01', order_date_key) AS Year_month_day,
DATE_FORMAT(DATE_ADD('1900-01-01', order_date_key), 'Y-u') as year_week
Sample data: 42711, 42714, 42715, 42720
Desired output: 2016-50, 2016-51, 2016-51, 2016-52

Related

Why Str(CDbl(Now())) returns 43185.44166666667?

Today is 26/03/2018.
Why Str(CDbl(Now())) returns 43185.44166666667?
and
Str(CLng(Now())) returns 43185? I am expecting a string starting with 26032018...
Dates in Access and VBA are defined as doubles where the whole numbers are days, and the fractions are fractions of a day, starting from 30-12-1899.
If you want to cast a date to a formatted string, use Format:
Format(Now(), "ddmmyyyy") will return your expected result.
A Date is a the number days since 31 December 1899, and the time component is the decimal fraction of a day.
That's why you're getting a numeric value.
If you want to format the Date as a ddmmyyyy string, you should use the Format$ function
Debug.Print Format$(Now(),"ddmmyyyy")

Convert Date to a number in mysql (Like how a date converts in Excel)

What is the correct function to use when i want to convert a date to a number. Like when you enter a date in excel for example (now()) and then it will show the date but when you click on the number format it will show you a number.
I tried to use Unix timestamp but its not exactly the output i was looking for.
The date i entered is today's date
=now()
and the output i was hoping to get is
42146
what's the correct function to get this result in mysql?
Thank you
Microsoft Excel bases date serial numbers from Jan 1, 1904 or from Jan 1, 1900 (depends on the setting in the workbook.)
To generate a date "serial number" similar to what Excel uses, just calculate the number of days between NOW() (or whatever date you want to convert), and the base date. For example:
SELECT DATEDIFF(NOW(),'1900-01-01') AS excel_serial_date_1900
(You might need to add 1 or subtract 1 to get the exact same value that Excel uses, I've not tested that.) If your Excel workbook is using the 1904 based serial date, use '1904-01-01' as the base date in the expression.
Note: the DATEDIFF function returns integer number of days, operates only on the "date" portion, and doesn't include any fraction of a day.
Reference: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff

Using DateDiff to Calculate the Difference in Days

In the expression below, I am trying to calculate the difference in days between the Created date and today's date. If it is less than 30 days then output "1", otherwise output "0"
=IIF(DateDiff("d",(Format(CDate(Fields!Created.Value), "MM/dd/yyyy")), (Format(CDate(Today()), "MM/dd/yyyy")))<30, "1", "0")
Both values in "Created" and Today() are formatted with the date and time so I use Format and CDate to extract just the date. When I run the report, it displays all "0" and I know that is incorrect. Is there something wrong with the expression?
Yes, there are a number of things wrong with that expression: You take dates, explicitly convert them to dates, then using format convert them to strings then implicitly convert them back to dates to do your date comparison. That's a lot of heavy lifting for no benefit. You are also using SQL syntax in a VBA expression. Your result is also a string when it probably should be an integer.
Your expression should look more like this:
=IIF(DateDiff(DateInterval.Day, Fields!Created.Value, Today) < 30, 1, 0)

reverse date formatting in as3

As3 has some functions that help with converting a date to various formats of strings, but I cannot find any functions that do the opposite.
in the case of
2012-04-16 I'd like to use YYYY-MM-DD as my format string to get a date object
01/01/2013 12:00.13 I would specify `DD/MM/YYYY HH:NN.SS
Is ther something that does the reverse/opposite of formatDate?
Leveraging off a similar question (Parse string pattern into Date in Flex), you can use DateField.stringToDate to get the date portion of the string:
var theDate:Date
theDate = DateField.stringToDate('2012-04-16', 'YYYY-MM-DD'); // Returns 16 April 2012
theDate = DateField.stringToDate('01/02/2013', 'DD/MM/YYYY'); // Returns 1 Feb 2013
Unfortunately this only parses the date, not the time.
There are examples of using RegExp to parse out the different date-time components and then creating the Date with the values thus parsed, but I've not seen or done anything like the generic format string approach.

Informix iSQL date function help

I'm trying to extract just the year out of either of the built-in date functions of Informix
TODAY or CURRENT
Is there a way to do this without SUBSTR()?
Yes: use the YEAR function:
SELECT YEAR(TODAY)
FROM SysMaster::SysDual;
Similarly for MONTH, DAY, WEEKDAY (0..6). You could use CURRENT instead of TODAY, but that generates a DATETIME value which is then converted to DATE and then analyzed.