Syntax for SOQL Query - Dynamic Date - salesforce

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

Related

How to extract day of week from timestamp

I am trying to extract the day of the week from a timestamp in SQL Server.
I am specifically looking for the SQL Server equivalent syntax to EXTRACT.
I want to count how many fields are in each day of the week.
This is how I would do it on BigQuery:
SELECT
EXTRACT(DAYOFWEEK FROM order_date ) as day,
count(*) count_trips
FROM `sales.orders`
group by EXTRACT (DAYOFWEEK FROM order_date)
Try this:
SELECT DATENAME(WEEKDAY, DATE(timestamp))
example:
SELECT DATENAME(WEEKDAY, '2022/05/08 18:50:30');
Output: Sunday
P.S.
I am helping you the day part only considering you know the rest of your code. Feel free to reply this for exact query.

T-SQL search for returning customer

I've got a question, where I'm struggeling with at the moment.
I really don't know how to solve this problem, it seems so simple.
I've got a Customer ID, and an Order Date.
I only want to show customer, that ordered things before 2015 AND buyed something lets say in the last 10 days.
I created a little test table for that - lets say it's January 2016:
Now there is Customer 1, that did a purchase on January and in year 2010. Ok that fits my need, I want to show him.
But customer 2 did a purchase on December last year, so he is a not a "returning" customer, but a customer that often buys my things. I dont want to show him.
I tryed something like this, but it didn't work:
SELECT [Kunden_ID],Bestellung
FROM [Immo].[dbo].[TEST] AS A
WHERE (Bestellung >=DATEADD (day,-10,getdate())
AND Bestellung <= DATEADD (month,-12,getdate()))
You need two separate queries. The first finds those customers that bought something in the last 10 days. The second uses the exists query to find those same customers (join using ID) that bought more than 12 months ago.
Try this:
SELECT [Kunden_ID],Bestellung
FROM [Immo].[dbo].[TEST] AS A
WHERE (Bestellung >=DATEADD (day,-10,getdate()))
and exists (
select 1
from [Immo].[dbo].[TEST] AS B
where a.[Kunden_ID] = b.[Kunden_ID]
AND b.Bestellung <= DATEADD (month,-12,getdate())
)
Another way to do this uses a Common Table Expression (CTE). It's a little easier to see the different queries.
With Get10Days as (
SELECT [Kunden_ID],Bestellung
FROM [Immo].[dbo].[TEST] AS A
WHERE (Bestellung >=DATEADD (day,-10,getdate()))
)
select b.Kunden_ID
from [Immo].[dbo].[TEST] AS B
join Get10Days as A on a.Kunden_ID = b.Kunden_ID
where b.Bestellung <= DATEADD (month,-12,getdate())

Only filter my time in SQL Server where clause

I am new to SQL and Programming in general. I have a datetime field which I want to use to filter my results but only on the time portion.
For example I need to run a report from 2pm yesterday to 7am current day. I cannot hard code the dates because this report needs to run daily. This Query will run from a stored procedure automatically.
I have tried AND clm.createDtTm > DATEADD(d, -1, GETDATE()), which goes back one day but not the time range I need.
I tried this: AND (datepart(hh, '11:02:54.107') = 7)<--To be honest not even sure what I am doing here.
I am not sure if this is even possible, but if I can get results for one day back I am assuming there has to be a way to narrow that day between hours.
Any help would be appreciated.
Thanks,
Abdul
This should do the trick, it filters out rows that do not fall in the date interval, from 7pm yesterday to 7am today.
WHERE
createDtTm >= DATEADD(HOUR, 14,CAST(DATEADD(DAY,-1, CAST(GETDATE() AS DATE)) AS DATETIME))
AND createDtTm <= DATEADD(HOUR, 7, CAST(CAST(GETDATE() AS DATE) AS DATETIME))
I suggest something like this:
SELECT(DATEADD(d,-1,GETDATE()))
FROM table
WHERE (DATEADD(d,-1,GETDATE())) = rowloadtimestamp
HAVING DATEPART(HH,rowloadtimestamp) BETWEEN 13 AND 24
UNION
SELECT GETDATE()
FROM table
WHERE GETDATE() = rowloadtimestamp
HAVING DATEPART(HH,GETDATE()) BETWEEN 1 AND 7
Assuming you have a rowloadtimestamp field this should work for you.
What do you mean with „I can not hardcode the dates” ? Do you mean the time interval is variable, or is it fixed, or are you referring to the exact dates? I'm not sure I understand.
In case of MySQL you can do something like:
SELECT data, date FROM table WHERE date < DATE_SUB(NOW(), INTERVAL 2 DAY);
This Query will get you the last two days. You can vary the interval by replacing the „2” with a variable if that is possible in your case.

Filtering columns based on a date in a field for current week, past weeks, and future weeks

Example Table:
(Column Heading) Total Interviews of everyone --- (Row Heading) Interview Requested
(Column Heading) Number of interviews this week -- (Row Value) Count all the dates scheduled this week that have the status: "Interview Requested" e.g 4
(Column Heading) Number of interviews next week -- (Row Value) Count all the dates scheduled next week that have the status: "Interview Requested" e.g 6
First of all i'll like to aploigise about my makeshift table, it seems that i cant use pictures since im a new member.
I am also new to using reportbuilder and sql and have been trying to figure out how i can count the amount of dates that fall between certain date ranges in a report.
Like ive stated in the makeshift field called "Number of interviews this week" I wish to count all the dates scheduled for the current week, the week after, the week before and so on based on a date field. I then plan to break this table down to show all the interviews a single person has schedueled.
The issue I am having is that there will be many dates spanning different months/days. So Im not sure how i can represent this with DateDiff or other date features, because the table is meant to be a live representation of the current interviews.
Is what i'm trying to do even possible using report builder?? If so any tips would be great, if not then thanks for taking the time to look at my question.
As Requested Dataset Fields:
[UserName] (User table), [InterviewStatus] (Interview table), [DateUpdated] (Interview Table)
Those are the main ones that will be used in the report
If you just want to count the number of columns of each field, you can use the sql statement COUNT() see COUNT().
You can do the common query like this:
SELECT COUNT(fieldName1), COUNT(fieldName2), ... , COUNT(fieldNamen) FROM <tableName>
Also try to show your codes so that your question will be more clarified.
You can use the generic select statement from your interview table filtering in the where clause for dates between the beginning of the current week and the end of the next.
Then the two fields would be a sum of 1 if the date falls within the current week or next.
EDIT:
Example below will give the number of dates within this week. Taken from another question:
Select InterviewStatus,
Sum(Case when DateUpdated >= DATEADD(dd, -(DATEPART(dw, GETDATE()) -1), GETDATE())
AND DateUpdated < CAST(CONVERT(VARCHAR(10), DATEADD(dd, (8 - DATEPART(dw, GETDATE())), GETDATE()), 120) AS DATETIME)
Then 1
Else 0 end)
as NumberInCurrentWeek
From InterviewTable
Where DateUpdated >= DATEADD(dd, -(DATEPART(dw, GETDATE()) -1), GETDATE())
AND DateUpdated < CAST(CONVERT(VARCHAR(10), DATEADD(dd, (8 - DATEPART(dw, GETDATE())), GETDATE()), 120) AS DATETIME)
Group by InterviewStatus
This just needs to be tweaked to your exact circumstance. For example alter the 8 by 7 will capture the current fortnight.
Then produce a simple table based on the query.

multiple count rows based on date ranges (access db)

I can get a single count row from a specified date range like this:
SELECT table.[EVENT NAME], Count(*) AS [Count]
FROM table
WHERE [EVENT]='alphabetical' And table.DATE>=#11/20/2010# And (table.DATE)<=#11/26/2010#
GROUP BY table.[EVENT NAME];
but how could I add multiple rows with different date ranges?
[EVENT NAME],[DATE 11/20-11/26],[DATE 11/27-12/3], etc...
EDIT
the data would look something like this
event1;1/11/2010
event1;1/11/2010
event2;1/11/2010
event2;1/11/2010
event2;1/11/2010
event3;1/11/2010
event1;1/12/2010
event1;1/12/2010
event2;1/12/2010
event2;1/12/2010
event4;1/12/2010
event4;1/12/2010
etc.
and would like something like this (preferably with more columns) :
event1;2;2
event2;3;2
event3;1;0
event4;0;2
You'd use a group by clause and group by the date.
You didn't provide example records with expected results, that helps us help you :).
In other words post more information..
But from what I can tell you want a count based on a date range.
So if you had 1/1/2010 with 10 rows
and 1/2/2010 with 20 referenced rows
and 1/3/2010 with 6 reference rows...you'd want output like this:
1/1/2010 10
1/2/2010 20
1/3/2010 6
So SELECT COUNT(*), MyDate FROM MyTable GROUP BY MyDate
To answer your question about a date range, think of how group by works, it works by grouping a set of data by combining all sets that match a criteria. So when you say group by date it groups by a single date. You want a date range, so each row should know about or understand a range (Start to End). So you need to include these columns in each of your rows by generating them via SQL.
Edit
For instance
SELECT Count(*), DATEADD(day, -10, GetDate()) AS StartDate, DATEADD(day, 10, GetDate()) AS EndDate
FROM MyTable GROUP BY StartDate, EndDate
Access has similiar functions to add days to dates so look that up for MS Access. Then just generate a start and end date for each column.

Resources