I have a report in Cognos, in this report I have a date filter that will be passed by the user and I want the result set to include dates from the last 30 days. Essentially (?date? - <30days>) I am unfamiliar with the syntax to accomplish this because date is not a primitive type.
[Time stamp] <= ?date?
and
[Time stamp] >= (?date? - <30days>)
I would appreciate any advice you guys may have.
Use [Time stamp] >= _add_days( current_date(), -30)
In case you in fact need a month, not just 30 days, you might want to use _add_months(?date?, -1)
Related
The sqlite timestamp here is 18 digits, but how can one know its specific date. 132079170460000000 should correspond to July 2019, but this year cannot be obtained regardless of interpretation or other conversions. I want to get time like 2019-07-30 13:23:40.
This is sqllite timestamp, but product specific model does not know,only know that this timestamp is 18-digit just like these:
132079170420000000
132079218060000000
If 132089746940000000 is equivalent to "2019-07-31 13:42:31" as mentioned in the comments, then try this:
SELECT datetime( DateColumnWith18Digits / 1.0E11 + 1137798.60179213 )
FROM SomeTable
If the example you shared was just a random value, this technique might still work, but you'll need to find the offset factor. The conversion assumes that the 18-digit value is the number of milliseconds since some epoch. I simply assumed that one of the comments contained a valid equivalence and found the offset to match the Julian date value.
The reverse encoding would be like
SELECT (julianday('2019-07-31 13:42:31') - 1137798.60179213) * 1E11
I have 2 fields of datetimestamp type. I need to compare them based on the quarters those dates occur in and determine whether one occurs in a past, same as, or future quarter.
2 fields: pay.check_dt and pay.done_in_dt. I want to know if pay.check_dt occurs in a prior, same as, or future quarter in comparison to pay.done_in_date
I originally thought to use a case statement converting them using to_Char(fieldname, 'Q-YYYY'), but then I can't to the mathematical comparison because they are then character strings.
Thanks for the help!
Craig
Use the TRUNC(date) function: documentation
I have no DB available now, but something like:
TRUNC(pay.check_dt, 'Q') < TRUNC(pay.done_in_dt, 'Q')
In SQL Server Reporting Services, how would I calculate the last day of the current month?
Here is the answer I came up with
=DateSerial(Year(Now()), Month(Now()), "1").AddMonths(1).AddDays(-1)
As it took me a while to Figure this out, and JC's answer was the simplest to modify and had a good logical structure. I expanded it Slightly for others searching for answers on this topic.
I have found normally you don't just want the Last Day of a month / year you also want the first day of the month / year. So here they are the full lines to calculate just that. wtih the SQl version there also.
First Day of Current month
SSRS=Today.AddDays(1-Today.Day)
SQL=SELECT DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0,getdate()),0))
Last day of Current Month
SSRS=Today.AddDays(1-Today.Day).AddMonths(1).AddDays(-1)
SQL=SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
First Day of Current year
SSRS=Today.AddMonths(1-Today.month).AddDays(1-Today.day)
SQL=SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
Last Day of Current Year
SSRS=Today.AddDays(1-Today.Day).AddMonths(13-today.month).AddDays(-1)
SQL=SELECT DATEADD(dd,-1,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0)))
I hope this helps somone.
I know you've found your own answer, but I'd suggest this alternative:
=Today.AddDays(1-Today.Day).AddMonths(1).AddDays(-1)
It's a little easier to read, in my opinion, and might have slightly better performance (though most likely unnoticeable)
And, of course, if you wanted to pad out that date to 23:59:59, as is often necessary, just modify slightly:
=Today.AddDays(1-Today.Day).AddMonths(1).AddSeconds(-1)
From the blog of a Microsoft SQL Team member:
-- returns the last day of the current month.
select dbo.Date(year(getdate()), month(getdate())+1,0)
http://weblogs.sqlteam.com/jeffs/archive/2007/01/02/56079.aspx
Hope this helps!
--Dubs
You may try this expression, Just replace now with your date field.
=DateSerial(Year(Now), Month(Now), 1)
Hope this helps.
Regards
There is an even easier way as in the marked answer:
=DateSerial(Year(Now()), Month(Now())+1, 0)
Since DateSerial() accepts integers as parameters one does not have to use AddMonths() and AddDays(). As in the example an instant calculation inside DateSerial() is possible.
Furthermore day 1 is the first day of the month while the first day minus one day is the last day of the month before (1-1=0). So the example will return the date of the last day of the current month.
you can use an assembly for doing this work by adding it as a reference.
I would like to convert a yyyy-mm-dd to something like this:
"Saturday, 2 October 2009"
I would like also to have the option to modify the language both day of the week and month (make it customizable)
thanks in advance ;)
Since you never told us the language, here's a discussion on how to do it in COBOL.
Assuming .NET (from the datetime tag):
DateTime.Parse("2009-10-02").ToString("D", CultureInfo.CreateSpecificCulture("en"));
In C, you would use a combination of the localtime() and strftime() functions. They should handle internationalization more or less automatically, if your application is set up for it.
Convert the date into an integer, representing the days since a specific date, then add a number and apply the modulus operator with the operand 7. That will give you a number 0-6 that represents the day of week.
However, most languages have this functionality built-in.
Essentially, I have a query that is responsible for fetching all records (with specific filters) within the last month. I'm using Oracle's interval keyword and all was working great until today (December 31st, 2009). The code I'm using is
select (sysdate - interval '1' month) from dual
and the error I get it
ORA-01839: date not valid for month specified
How can I use the interval keyword to be compatible with any date? Or if anyone has a better way of approaching the issue, I'm all ears.
Thank you.
try
select add_months(sysdate,-1) from dual
Being pedantic...
The requirements are not quite specified perfectly unambiguously. What does the business mean by "within the last month"? Most people would take that to mean "within the current calendar month" in which case I'd use:
TRUNC(SYSDATE,'MM')
Otherwise, perhaps they want an arbitrary period of 1 month prior to the current date - but then how do you define that? As you've found, INTERVAL '1' MONTH simply subtracts one from the month portion of the date - e.g. 15-JAN-2009 - INTERVAL '1' MONTH returns 15-DEC-1999. For some dates, this results in an invalid date because not all months have the same number of days.
ADD_MONTHS resolves this by returning the last day in the month, e.g. ADD_MONTHS(31-DEC-2009,-1) returns 30-NOV-2009.
Another possibility is that the business actually wants to use an average month period - e.g. 365/12 which is approximately 30.4. They might want you to use SYSDATE-30, although of course twelve iterations of this will only cover 360 days of the year.