I have a custom aspx application that directly queries the CRM database base tables such as ActivityPointer to get the data such as due date, created on etc. The custom aspx page need to filter data based on certain date range such as for today, last 7 days etc. It does so by executing custom sproc stored in CRM database. However I am facing challenge with the user timezone issue getting the correct data.
DECLARE #StartDate DATETIME;
DECLARE #EndDate DATETIME;
DECLARE #NoOfDay INT= 7 -- passed as param
SET #StartDate = DATEADD(day, DATEDIFF(day, #NoOfDays-1, GETDATE()), 0)
SET #EndDate = DATEADD(day, DATEDIFF(day, - 1, GETDATE()), 0)
This will always return the startdate and enddate based on the machine timezone where the sql instance is running. What would be the correct approach to set the start date/end date so it also considers user timezone settings? I don't think I can use CRM specific functions such as dbo.fn_UTCToLocalTime() etc because my custom aspx will be using a different account to access/execute the sproc.
Aside from maybe a direct SQL call being a little overkill for this, I'd just change the SP to accept a start and end date, and send in the UTC version of the dates you want to constrain the results to
You really shouldn't be hitting the tables directly. CRM provides a whole bunch of views that add additional fields (and cater for security roles etc).
If you look at the Views, you should find FilteredActivityPointer. This view will then have the date-time formatted and time-zone adjusted based on your current user.
The Filtered* views should be used for all reports
Related
since few weeks I started working on SQL developer.
I noticed an important issue that I do not know how to solve. This is related to the way my SQL developer visualizes the date.
First of all, here are my settings:
and here is my problem:
select to_date('01-01-0001', 'DD-MM-YYYY') from dual;
Instead of correctly visualizin 01-01-2001, it visualizes 03-JAN-0001
select select to_date('01-01-2001', 'DD-MM-YYYY') from dual;
It is interesting to observe that the issue of the day disappear when we change year.
So this -
defines for your session how to display dates and timestamps.
Example:
If you were querying a date w/o supplying a date format, then it adopts said settings.
But if you query a string, and ask Oracle to treat it as a date
select to_date('1-1-1990', 'DD-MM-YYYY') from dual;
The DD-MM-YYYY tells Oracle how to interpret your string as a date.
And then the output is shown using the format defined for NLS
Now, for your scenario, the year '1' - there is a bug in SQL Developer 18.1 that is causing the date to come back as 3 vs 1.
Bug 28093149 - DATE IS RETURNED INCORRECTLY FOR 01/01/0001 - COMES BACK AS 03/01/0001
My current application is running with NLS_lang as American.america on Oracle forms. We are planning make this application available on multilanguage. we are in the process of the modifying the code to handle application labels, prompts,hint, messages,etc run time. we dont want to change the time/date formats and leave it as it would in American.america. But for example, When i change the NLS_lang to French_canadian or anything else. Along with the language the date formats and date language paramters are changing. I am in need to change the NLS_lang as that is the only way i can translate the oracle error messages. But my date formats having DD-MON-YYYY format in Oracle DB create a problem in my weblogic sesssion. Is there are way to only change my weblogic session for my forms to be French_canadian but leave the date formats/ date format languages in American? Can we make this happen? except Language other paramters to remain same as american??? Please help.
Advance thanks for your time and valuable ideas...
You can set language and date/time formats individually.
Have a look at these parameters:
NLS_DATE_FORMAT
NLS_DATE_LANGUAGE
NLS_LANGUAGE
NLS_TERRITORY
NLS_NUMERIC_CHARACTERS
NLS_LANG is used to define the default for all these, but each of them can be different.
With NLS_TERRITORY you can commonly set "NLS_DATE_FORMAT", "NLS_NUMERIC_CHARACTERS", "NLS_CURRENCY", and "NLS_ISO_CURRENCY"
See example here:
ALTER SESSION SET NLS_TERRITORY = 'germany';
ALTER SESSION SET NLS_LANGUAGE = 'polish';
ALTER SESSION SET NLS_DATE_LANGUAGE = 'french';
SELECT SYSDATE AS german_date_format, TO_CHAR(SYSDATE, 'Day') AS french_day FROM dual;
SELECT 1/0 AS polish_error_message FROM dual;
GERMAN_DATE_FORMAT FRENCH_DAY
--------------------------------
23.01.14 Jeudi
SELECT 1/0 AS polish_error_message FROM dual
*
Error at line 1
ORA-01476: dzielnik jest równy zero
Upon logging into their accounts, each user has their login date and time stored to the database. What I was looking to do however is figure out the amount of days (or preferably convert into months if greater than a month) so that if a user views their profile they can see how active the band are. Also, this could benefit me in terms of keeping active profiles top of the agenda for content on the site so that it doesn't become stale from inactive users content filling up main page content.
I'm using ColdFusion so i'd be looking for a way to find for example how many days ago #lastLogin# was from #now()#. So say if the date of the last login was 23/04/2013 and todays date is 29/04/2013 it would read "Last Active, 1 day ago." However if the last login was 23/03/2013, it would read "Last Active, 1 month ago".
Anybody know how to do this? Thanks.
P.S I currently have no code from testing this as I have no idea where to start in terms of achieving this.
Use DateDiff
<cfset days = dateDiff("d", LoginDateVariable, now()) />
It's as simple as that.
P.S I currently have no code from testing this as I have no idea where
to start in terms of achieving this.
This doesn't answer your direct question but to help you know where to get started, I would strongly suggest reviewing the built in ColdFusion functions and tags that are available to you.
Tags
Tags by function
Functions
Functions by category
Also, Google searches usually land you at the docs, just add "coldfusion" to your search string. Searching google for coldfusion date functions yields very helpful answers, the first of which are a list of all ColdFusion date functions.
Dale's answer is spot on. But I would also suggest returning it as a variable with your query. Let the SQL server do the work. It's very efficient for those types of calculations. Not that CF can't do them well, too. But it's probably more appropriate for SQL to do that lifting. Especially if you're already returning the lastLogin date.
It would be similar to the CF solution:
SELECT ...., lastLogin, DATEDIFF(d, lastLogin, GETDATE()) AS LastLoginDays
FROM ....
WHERE ....
That would give you the number of days. You'd have to decide how you wanted to define a month if you wanted to break it out by month/day. That would get a bit more complex. You could write a SQL function that could be run on both dates and give you an accurate count of days/months/years since last login.
One other thing to keep in mind: Where are the dates being generated? When you insert loginDate into the database, are you doing a now() in CF before you insert it or are you doing a getDate() in SQL when you insert it? Again, I would let the database do your date logic, but you'd want to compare the two dates from the same source. For instance, if your loginDate was a database getDate() then you may not want to compare that to a CF now(). One goes by the datetime of the SQL server and the other goes by the datetime of the CF server. They could be different.
I am currently dealing with a slightly strange relation layout. The relation contains several fields, two of them, datetimeA and datetimeB are used to hold a datetime value.
I am not sure why it was set up this way, but datetimeA is used to store the date components with all of the time components being 00:00:00.0 while datetimeB is used to store the time component with all of the date components as 1970-01-01.
I am creating a simple reporting interface which uses this table and I need to extract the date from datetimeA, the time from datetimeB and join them into a new datetime. If possible I would like to make this happen in the query run against the database.
An ideal solution would be something similar to the datepart function but with 'date' and 'time' parts.
This is SQL Server TSQL, but the date functions appear to be more or less the same in both products. I'm not sure how Sybase handles date literals so you might need to change them to get this example to work:
declare
#date datetime = '2013-02-15T00:00:00',
#time datetime = '1970-01-01T15:15:15'
select dateadd(ss, datediff(ss, '19700101', #time), #date)
I have 2 parameters in my report to select a date range:
StartDate & EndDate
I want to hide the StartDate and allow users to just select the EndDate which should then dynamically change the start date to 1 year before the EndDate.
I need this to happen every time a user changes the EndDate changes.
I'm pretty sure I have to use cascading parameters, but I don't know how.
Any suggestions?
Yes, cascading parameters are the trick if you want to do this at the report level. (You could also handle this pretty effectively at the query level.)
First arrange the parameters in your report in order of dependence: EndDate should be be listed above StartDate. Use the up and down arrows to rearrange the parameters.
Set the StartDate parameter to be "Internal" and set the default value appropriately. Select "Specify values" and create a value of =DATEADD( DateInterval.Year, -1, Parameters!EndDate.Value )
Now you can use both #EndDate and #StartDate in your query without initializing them and they will be passed the SSRS value.