SOQL date in week where clause - salesforce

I have a custom object with a Date field and I was wondering how you could query that object if that date field is within the current week (given that the week starts on Sunday and ends on Saturday). I was thinking of creating two functions that find the specific date for the first day of the week and last day of the week and just doing:
SELECT Id, MyObjectDate FROM MyObject WHERE MyObjectDate > getWeekStartDate(todaysDate) AND MyObjectDate < getWeekEndDate(todaysDate)
But I feel like there is an easier way. Any tips would be appreciated!

Use toStartOfWeek method to get Start date of week.Check this.
The toStartOfWeek method returns the start of the week for the Date that called the method, depending on the context user's locale. For example, the start of a week is Sunday in the United States locale, and Monday in European locales.
public Date WeekFrom {get; set;}
public Date WeekTo {get; set;}
.....
WeekFrom = todaysDate.toStartofWeek();
WeekTo = WeekFrom.adddays(6);
SELECT Id, MyObjectDate FROM MyObject WHERE MyObjectDate > WeekFrom AND MyObjectDate < WeekTo
Hope it helps you

Related

soql query to get whole month,except its last week

I want to query for records of a month, except that month's last year.
my soql query is this
select id,name from opportunity where CreatedDate=THIS_MONTH And WEEK_IN_MONTH IN (1,2,3)
soql query to get the whole month, except its last week.
Yoou tagged it apex so can you cheat?
Date today = System.today();
Integer cutoff = Date.daysInMonth(today.year(), today.month()) - 7;
System.debug([SELECT Id
FROM Opportunity
WHERE CreatedDate=THIS_MONTH AND DAY_IN_MONTH(CreatedDate) < :cutoff]);

Values on the frist day of the week

I have a table in snowflake like below
StockName Date Value
GOOG 10/12/2021 $100
GOOG 10/11/2021 $995
Now i want to create a new column which would give me results that would show the value of the stock on weeks first day which would be monday or the next day if monday is a holiday
StockName date Value value_weekstart
GOOG 10/12/2021 $1000 $995
GOOG 10/11/2021 $995 $995
The following should work:
SELECT
stockname,
date,
value,
FIRST_VALUE(value) OVER (PARTITON BY stockname, datetrunc('week',date) ORDER BY dayofweekiso(date)) as value_weekstart
FROM table_name
ORDER BY 1,2;
that will return the "sunday price" if there is one, but will side step the possible jump if you use the stock dayofweek and someone changes the setting on your instance.
If you really want sunday to be last going to (dayofweekiso(date)+6)%7 will roll it around to last position.

Snowflake Extracting Month, year from date

I have a table with a column called created_date that has date like 2020-01-01 00:00:00.000
and I'm trying to create a column that will show only the year, another one the month
and one that shows the month as a string
here what I try
select date_part(year,'created_date ') as year,
date_part(month, 'created_date ') as month
to_char(Month, 'created_date') as month_name,
user_name,
product
from user_table
Unfortunately when running the query above, I get an error that Function Extract do not support VARCHAR(10) argument type
The result I'm trying to get is to who a table like
year month month_name user_name product
2021 01 January John Doe Ninja Mixer
2021 05 May Clide Smith Blender
Any help will be appreciated as I'm mostly used to MS sql and we just switch to snowflake.
Assuming the "created_date" is stored as a timestamp or datetime (synonyms), then you just need to remove the single quotes from around the created_date column name and change "to_char" to use the "monthname" function:
select date_part(year, created_date) as year,
date_part(month, created_date) as month,
monthname(created_date) as month_name,
user_name,
product
from user_table

SQL query for removing current and future dates?

I would like to filter out all records that have a date in the future AS WELL AS any records in the current month. I attempted to use this query:
...
WHERE (Month(DueDate) < Month(getdate()) AND (Year(DueDate) <= Year(getdate())))
The issue is that it doesn't return any results that were previous year but a month that is greater than the current. ie: Today's date is 9/12/18, it would not return any records that had a month of 10, 11 or 12 regardless of the year.
This will filter off any records with a DueDate after the first of the current month.
WHERE DueDate < DATEADD(MONTH ,DATEDIFF(MONTH, 0, GETDATE()), 0)
EDIT: I had >. Should've been <.
Try something like:
SELECT ...
FROM ...
WHERE DueDate < DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 1);
This solution uses DATEFROMPARTS function for which you can see more information in the documentation. The idea is to get the first of the current month for the current year and select only those rows whose DueDate is before the given date and time.
Also, instead of GETDATE() (link) function you might want to check out GETUTCDATE() (link) if it's more suited to your needs.

Syntax for SOQL Query - Dynamic Date

Very new to SOQL. Looking to write a query (to be used in Apex Data Loader) that pulls records with a CreatedDate <= 14 days ago. None of the predefined date options (LAST_N_DAYS, etc.) seem to cover what I'm looking for. I'm guessing/hoping there is something similar to DATEADD(D, -14, DATE()) that can dynamically calculate 14 days ago, so that the criteria would ultimately look like CreatedDate <= DATEADD(D, -14, DATE()).
Thanks in advance!
There isn't any Date Add. However looking at your criteria the LAST_N_DAYS builtin should do the trick for you
Lets say I want to select Data that is older than 14 days ago (including that day) I would do
Select Id, CreatedDate from Account where CreatedDate <= LAST_N_DAYS:14
if I need the opposite ie data created in the last 14 days
Select Id, CreatedDate from Account where CreatedDate >= LAST_N_DAYS:14

Resources