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

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

Related

Convert date format while using date_add function in SQL

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

Pass a specific time with today's date as a value to the input field in Informatica Cloud

There is a mapping task which has a input field, the value provided to the field should be today's date and time is 18:00:00.000000000. What is the formula to build this one?
Suppose the current month is 9, it should be passed as 09. Same with the date, if the date is a single digit, 0 should be appended in the last.
The expected output for today is :
09/24/2021 18:00:00.000000000
The formula to build this value?
can you use this ?
to_char(sysdate,'MM/DD/YYYY') || '18:00:00.000000000'
Pls note this will produce a string.
If you want a datetime, pls convert this to datetime using to_date(above_st, format)

substract two dates using sql loader

I Have two dates. One is cur_phy_date and next one is Last_Phy_Date. But problem here is flat file has only one date i.e.,cur_phy_date but to calculate Last_Phy_Date I need to substract 6 months from cur_phy_date in SQL LOADER control file.
My sample date formats for cur_phy_date: 2018MAY01.
Is it possible to substract 4 or 5 months from cur_phy_date and get Last_Phy_Date in the same format as cur_phy_date
Hopefully you have the power to make your columns a proper DATE datatype. If not, well, we all have to work with a bad design at one point or another. Either way, you need to convert the current date to a proper date by referencing it with a preceding colon character, subtract 6 months, then convert to the format you need. Here's how your control file entry should look:
To a proper date:
Last_Phy_Date date "MM/DD/YYYY" "TO_CHAR(ADD_MONTHS(TO_DATE(:cur_phy_date , 'YYYYMONDD'),-6), 'MM/DD/YYYY')",
To a VARCHAR2:
Last_Phy_Date CHAR "TO_CHAR(ADD_MONTHS(TO_DATE(:cur_phy_date , 'YYYYMONDD'),-6), 'YYYYMONDD')",

How to load data within date range on Qlik Sense?

I would like to load data within a defined date range on Qlik Sense.
When I load the data, I set it to be in the format below:
SET DateFormat='DD/MM/YYYY(WWW)';
I hope to use a where statement to limit the data where the column variable [Date] is within a date range.
However, the below statement placed after the LOAD columns FROM table statement did not work:
where [Date]<'30/11/2016(Wed)' and [Date]>'01/12/2015(Tue)'
May I know what is the syntax for
If you want to limit a date in MySQL to a range, using the date bounds of the range alone is enough, i.e.
WHERE date BETWEEN '2015-01-12' AND '2016-11-30'
Specifying the day of the week is redundant and unnecessary, because for example Novmeber 12, 2015 is always a Tuesday.
If your source date data has the format dd/mm/YYYY then you can use the STR_TO_DATE() function to parse into a date. After that, you can make the same comparison:
WHERE STR_TO_DATE(date, '%d/%m/%Y') BETWEEN '2015-01-12' AND '2016-11-30'

Copy formatted date from excel to notepad

I have extracted some data from mysql table a to excel. Now I have to create insert queries to insert into table b. In the table it has more than million records. Now in excel file, dates are converted into number. I have formatted the number to yyyy-mm-dd hh:mm:ss as required by mysql. But in insert query it remain number. Check the image as well.
image link:
http://s8.postimg.org/6g8fvf851/Capture.png
please tell me how can i copy the formatted date into notepad or sql.
In short, you should use text function to convert excel date to string.
=text(B2, "yyyy-mm-dd hh:mm:ss")
You can use this expression in your concatenate function or convert the dates to string representation, then use them in your formula.
Excel uses number to represent date. 0 means 1900-01-00 which is not exists. 1 means 1900-01-01. 1/24 means 1 hour, 1/24/60 means 1 minute and 1/24/60/60 means 1 second. You can check it by entering expression =1+1/24+1/24/60+1/24/60/60 in a cell then change its format to yyyy-mm-dd hh:mm:ss. It will show 1900-01-01 01:01:01.