Informix iSQL date function help - function

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.

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

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'

Date calculations - how to use today's date if date not specified

I have a question regarding Microsoft Access 2010. I'm working on an inherited complaints database and I've been asked to produce a report outlining how long complaints have/ or were active.
Using the following I'm able to calculate the difference between when a complaint was opened and when it was closed and show the number of days active.
DaysActive: DateDiff("d",[COMPLAINTS]![DateRcvd],[COMPLAINTS]![DateClosed])
My issue is when a complaint hasn't been closed I don't get a value returned. Is there a way to modify the expression so that if the DateClosed is empty it will use the current date instead?
Assuming this is a query expression, the db engine supports the Date() function which returns today's date. So you can use an IIf expression to give you Date() when DateClosed is Null, or otherwise DateClosed
DateDiff("d", COMPLAINTS.DateRcvd, IIf(COMPLAINTS.DateClosed Is Null, Date(), COMPLAINTS.DateClosed))
If the query will always be run from within an Access session, you can use Nz instead of IIf ...
DateDiff("d", COMPLAINTS.DateRcvd, Nz(COMPLAINTS.DateClosed, Date()))
Note that Nz is a VBA function and IIf is supported directly by the db engine, so IIf should theoretically be faster . But the difference may not be perceptible in your context.
Try using this.
DaysActive: DateDiff("d",Nz(COMPLAINTS.DateRcvd, Date()),[COMPLAINTS]![DateClosed])
Nz will check if the date is available or not, if not , it will replace today's date for that, using DATE() function.

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

VB.net - Get date from MySQL, add it with a month and compare it with today's date

I'm making an invoice system using VB.Net and MySQL.
First of all, I have to retrieve a date from mysql, add it with a month and compare it with today's date.
My date format in mysql is "yyyy-MM-dd"
My problems are:
How should I add a month to the date which I get from mysql?
How should I check if that date have already exceed?
First Get the data from MYSQL
Dim dateFromMySQL as DateTime = //Get from MYSQL
Then
1) How should I add a month to the date which I get from mysql?
Just use AddMonths to add month to date.
Dim newDate as DateTime = dateFromMySQL.AddMonths(1)
2) How should I check if that date have already exceed?
if dateFromMySQL <= DateTime.Now Then
Look, I don't know vb.net but as it uses .Net and I know C# it's probably the same.
If you're not using an ORM, you'll have to get the date representation from MySql, convert it to a Date or DateTime object using the class constructor, and this object has methods to add a month and compare to a limit date.
I think it'll solve your problem.