Querying from SQL Server datetime conditions - sql-server

I have a datetime stored in SQL Server like '2021-10-12 18:46:31.047' and '2021-10-225 23:54:08.667' in one row..
How can I query from datetime by using date only not hours
WHERE str_day = '2021-10-23' AND end_day = '2021-10-24'
Help please

Use date boundaries:
WHERE str_day >= '20211023'
AND str_day < '20211024'
AND end_day >= '20211024'
AND end_day < '20211025'

If your columns str_day and end_day are of time DATETIME (or DATETIME2(n)), then you can use something like this:
WHERE CAST(str_day AS DATE) = '2021-10-23'
AND CAST(end_day AS DATE) = '2021-10-24'
DATE is the date-only (no time portion) datatype - if your time portion is never needed, it might be useful to store those things as DATE from the beginning...

Use this; this will work for many scenario and types
WHERE str_day = '23 OCT 2021'
AND end_day = '24 OCT 2021'

Related

Measure does not work for Month Threshold

I build this Dax measure
_Access_Daily = CALCULATE(
DISTINCTCOUNTNOBLANK(ApplicationAccessLog[ApplicationUserID]),
FILTER('Date','Date'[DateId]=SELECTEDVALUE('DateSelector'[DateId],MAX('DateSelector'[DateId]))))+0
_Access__PreviousDay = CALCULATE(
DISTINCTCOUNTNOBLANK(ApplicationAccessLog[ApplicationUserID]), FILTER('Date','Date'[DateId]=SELECTEDVALUE('DateSelector'[DateId],MAX('DateSelector'[DateId]))-1 ))+0
The Date Selector table is a disconnected table containing dates from the 20th Jan to now. Dateid is a whole number like 20200131.
The Date table is a standard date table with all the dates between 1970 and 2038. Date id is a whole number like 20200131.
However it does not seems to work for the month threshold between Jan and Feb ? So if selected date is 01/02/2020 then it does not return correctly for the 31/01/2020.
As mentioned in the comments, the root problem here is that the whole numbers you use are not dates. As a result, when you subtract 1 and cross month (or year) boundaries, there is no calendar intelligence that can adjust the numbers properly.
Your solution (using 'Date'[DayDateNext]) might work, and if for some additional considerations this design is a must, go with it. However, I'd suggest to revisit the overall approach and use real dates instead of "DateId". You will then be able to use built-in DAX time intelligence, and your code will be more elegant and faster.
For example, if your "Date" and "DateSelector" tables have regular date fields, your code can be re-written as follows:
_Access_Daily =
VAR Selected_Date = SELECTEDVALUE ( 'DateSelector'[Date], MAX ( 'DateSelector'[Date] ) )
VAR Result =
CALCULATE (
DISTINCTCOUNTNOBLANK ( ApplicationAccessLog[ApplicationUserID] ),
'Date'[Date] = Selected_Date
)
RETURN
Result + 0
and:
_Access_PreviousDay =
CALCULATE ( [_Access_Daily], PREVIOUSDAY ( 'Date'[Date] ) )

How to add days to date time in Salesforce Apex?

Hi I am using Salesforce Apex,
I have a date as String as below. I need to add days to it using Apex.
String dateTime = '2017-07-08T23:59:59Z';
If I add one day to it then it should be 2017-07-09T23:59:59Z as string. How will I do this?
Thanks!
Beware the DST issue! The "addDays" function is not DST-aware, so if you step over a DST transition during the addition of days (in a time zone that has DST) then the time will be messed up.
To resolve this one split the date/time into separate date and time parts first, add the days to the date part then re-combine at the end, like:
DateTime dt = ...;
Integer days = ...;
Date d = dt.date().addDays(days);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
If you are working in the UK (London) time zone the following anonymous Apex illustrates the issue nicely:
DateTime dt = DateTime.newInstance(2017, 10, 28, 23, 59, 59);
System.debug('Adding days directly: ' + dt.addDays(2));
Date d = dt.date().addDays(2);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
System.debug('Adding days in parts: ' + dt);
You need to convert the string to a DateTime and then add days. You can format it back after
String stringDateTime = '2017-07-08T23:59:59Z';
DateTime dt = DateTime.valueOfGmt(stringDateTime);
DateTime tomorrow = dt.addDays(1);
DateTime nextMonth = dt.addMonths(1);
DateTime anniversary = dt.addYears(1);
String formattedDateTime = dt.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');

salesforce SOQL can't get datetime in local time

My objective is to count Lead records based on simple date range query in local time
Integer TotalLeads = [ Select Count() From Lead where createddate >= fromdate CreatedDate <= todate];
Fairly basic. However, the issue I'm running into is I only want to count the leads for the "local" time not UTC; createddate is in UTC for lead records.
Sample dates:
From: 03/23/2017
To: 03/29/2017
For these sample dates and my local time is UTC - 7 (Los Angeles), so my query would be
Integer TotalLeads = [ Select Count() From Lead where createddate >= 2017-03-23T07:00:00z AND CreatedDate <= 2017-03-30T06:59:59z];
If these are my dates, how do I append the local time so from date is 2017-03-23T07:00:00z and to date is 2017-03-30T06:59:59z?
Using from date first, I was able to do the following but can't figure how to keep it in local time
// Date
date ds = date.valueof('2017-03-23');
string dm = string.valueOf( ds.month());
string dd = string.valueOf(ds.day());
string dy = string.valueOf(ds.year());
// DateTime Midnight (UTC)
String SDate = string.valueof(dm +'/' + dd + '/' + dy + ' 12:00 AM');
system.debug(SDate); // -> 3/23/2017 12:00 AM
// DateTime (Local Time)
datetime ds2 = datetime.parse(SDate );
system.debug(ds2); // -> 2017-03-23 07:00:00
system.debug(ds2.format('yyyy-MM-dd\'T\'hh:mm:ss'')); // -> 2017-03-23T12:00:00
As you can see, using ds2.format put its in the format I need but back to UTC (midnight), I need it to be 2017-03-23T07:00:00
Any suggestions?
Thanks!
Figured what I was doing wrong. The date calculation was fine, it had to do with how this was being passed to a batch job.

Netezza date function for current date - 16 days

I want to pull today's date plus the last four weeks. Does anyone know a function for this in Netezza? What I have below is a guess that doesn't work. Also, I don't want to extract the date.
Select c.BUSINESS_UNIT_NBR, c.BUSINESS_UNIT_NAME, b.STORE_NBR, b.INV_CUST_ACNT_NBR,c.INV_CUST_NAME, a.NDC_NBR, a.GENERIC_NAME, a.INV_NBR, a.CONTRACT_ID, a.CONTRACT_NAME, a.ORD_DT, b.INV_DT, b.SHIP_DT, a.ORD_QTY, a.SHIPPED_QTY, a.INV_PRICE_AMT, a.INV_COST_AMT, a.MARKUP_MARKDOWN_PCT, a.INV_LINE_AMT
from fct_dly_invoice_detail a, fct_dly_invoice_header b, dim_invoice_customer c
where a.INV_HDR_SK = b.INV_HDR_SK
and b.DIM_INV_CUST_SK = c.DIM_INV_CUST_SK
and a.SRC_SYS_CD = 'ABC'
and a.NDC_NBR is not null
**and b.inv_dt(current_date)-16**
and b.store_nbr in (813, 1197, 2771, 3048, 3177, 3387, 3477, 3602, 3766, 3912, 4020, 4138, 4228, 4434, 4435, 4507, 4742, 4791, 5353, 5392, 5775, 5776, 5890, 6177, 6692, 6736, 6806, 7933, 9175, 9472)
Assuming inv_dt is the column you want to filter on, your where predicate should include:
WHERE
...
inv_dt between CURRENT_DATE - 16 and CURRENT_DATE
...
16 days does not equal four weeks, but adjust that number accordingly to your needs.

find the Day of week of a particular date

i have an object which has 2 dates startdate_c and enddate_c .
i need to find a way to find the days of week these dates fall in
For example
startdate = 1 jun 2012 and enddate = 3 jun2012
I need to know which days of the week the days between these dates fall in.
In this example
Mon = false, tue = false, wed = false, thu=false, fri=true,sat=true,sun=true
I want to use this in a Vf page to render the somefields based on the boolean value.
Any pointers would be of great help.
Date has a method called toStartOfWeek which you could leverage, assuming your two dates do lie within the same week you could simply do something like this:
date weekStart = startdate.toStartOfWeek();
list<boolean> days = new list<boolean>();
for(integer i = 0; i < 7; i++)
{
days.add(weekStart.addDays(i) >= startdate && weekStart.addDays(i) <= enddate);
}
A little bit crude, but it'll give you an array of 7 boolean values. For longer/unknown ranges you could use a date cursor and increment that instead of an integer here, but this should get you started. Note, I've not tested this code ;)

Resources