select where the date is 1 day before/ after a given date. in base SQL - sql

I don't really have an example but is there a way in the basic SQL to select rows where the date is 1 day before or after a date given by the user?

Yes, if you specify the parameter in the SQL of the query as a date and then perform a numeric comparison:
Parameters [Enter Date] As DateTime;
Select *
From YourTable
Where Abs(Fix([YourDateField])-Fix([Enter Date])) = 1

Related

SSIS Expression to get julian date from yesterday

Can anyone help me with SSIS Expression
I have this query in the expression:
Select period, * from table
I want to add a where clause to get period = yesterday
But, period column is in julian date format.
at the end i want the same result like this query
Select period, * from table
Where getdate() - 1
Thank you
you only can use you table date time column to compare with the getdate
Select period,
dataDate, *
from table
Where dataDate = getdate() - 1

Billing Day Code

I'm new to sql but, I'm trying to create code that will look at a Calendar table and determine if the Last Day of Billing Month, (Last field in the table) was the day before the day I am providing.
Here is what I have so far:
Select * from Calendar_table (for any given day)
Any help would be great
You could use the following in a parameterized query:
DECLARE #pGivenDatetime DATETIME
SET #pGivenDatetime = <your date to test>
SELECT * FROM Calendar_table where DATEDIFF(d, #pGivenDatetime, [Last Day of Billing Month], ) = 1
Assuming your flag column is a date type
select * from Calendar_table where date1 > [date provided]

Get previous month of a given date sql

I had this:
select company
from alldata
where **{previous month of date("01.05.2011")}**
IN
(select month from alldata where SumNrSpot=0)
I need to find the previous month in the where clouse.
Please, anybody can help?
In case you're using MySQL:
SELECT MONTH(DATE_SUB('2011-05-01', 1 MONTH))
in your case
select company
from alldata
where MONTH(DATE_SUB('2011-05-01', 1 MONTH))
IN
(select month from alldata where SumNrSpot=0)
Read more about both functions here.
You may use DateAdd() method with parameter, month, and -1 as value to get previous month.
Following will give you
select DatePart(mm,DateAdd(m,-1,GetDate())) as PreviousMonth
For your query you can try:
select company
from alldata
where datepart(mm,dateadd(m,-1,Convert(DateTime, Convert(DateTime,'01.05.2011',104))))
IN (select month from alldata where SumNrSpot=0)
For SQL Server
You can just use the function MONTH() to get a month from a date.
You can use DATEADD(month,-1,) to adjust a date by a month.
You should always use the format YYYYMMDD if you present dates to SQL Server in textual form.
where MONTH(DATEADD(month,-1,'20110501') IN
That's assuming the table column alldata.month contains a numeric value of the month from 1-12.
extract month from given date and add -1
select ( extract (month from now()) -1 ) as previous_month
Select
DATEPART(mm,(DATEADD(m,-1,CONVERT(DATETIME,'01.05.2011')))) AS PreviousMonth
IN
(Select month from alldata where SumNrSpot=0)
Assuming your "Month" column is an integer value
I did it this way:
select company
from alldata
where MONTH(`Mon`) -1
IN
(select MONTH(Mon) from alldata where SumNrSpot=0)
Thanks anyway, some of your responses was useful and appreciated.

T-SQL select records for the month

I have a table called logD, with a field named date (datefield type). Format is "year-month-day" IE: 2011-04-11
If today's date is 2011-07-31, I want all the records for the month of July. If today's date is 2011-02-14, I want all the records for the month of Feb and so on.
I am using SQL 2008 and reporting services to run a monthly report.
Try this:
SELECT *
FROM logD
WHERE YEAR(DATE) = YEAR(GetDate())
AND MONTH(DATE) = MONTH(GetDate())
If you have an Index on the column DATE then you can try this one:
SELECT *
FROM logD
WHERE [DATE] BETWEEN CONVERT(VARCHAR(6),GETDATE(),112)+'01' AND DATEADD(DAY,-1,DATEADD(MONTH,1,CONVERT(VARCHAR(6),GETDATE(),112)+'01'))
But, man, it looks ugly...
Try this:
DECLARE #firstOfMonth smalldatetime = dateadd(month,datediff(month,0,getdate()),0);
SELECT * FROM logD
WHERE [date] > #firstOfMonth
AND [date] < dateadd(month,1,#firstOfMonth);

Return just the last day of each month with SQL

I have a table that contains multiple records for each day of the month, over a number of years. Can someone help me out in writing a query that will only return the last day of each month.
SQL Server (other DBMS will work the same or very similarly):
SELECT
*
FROM
YourTable
WHERE
DateField IN (
SELECT MAX(DateField)
FROM YourTable
GROUP BY MONTH(DateField), YEAR(DateField)
)
An index on DateField is helpful here.
PS: If your DateField contains time values, the above will give you the very last record of every month, not the last day's worth of records. In this case use a method to reduce a datetime to its date value before doing the comparison, for example this one.
The easiest way I could find to identify if a date field in the table is the end of the month, is simply adding one day and checking if that day is 1.
where DAY(DATEADD(day, 1, AsOfDate)) = 1
If you use that as your condition (assuming AsOfDate is the date field you are looking for), then it will only returns records where AsOfDate is the last day of the month.
Use the EOMONTH() function if it's available to you (E.g. SQL Server). It returns the last date in a month given a date.
select distinct
Date
from DateTable
Where Date = EOMONTH(Date)
Or, you can use some date math.
select distinct
Date
from DateTable
where Date = DATEADD(MONTH, DATEDIFF(MONTH, -1, Date)-1, -1)
In SQL Server, this is how I usually get to the last day of the month relative to an arbitrary point in time:
select dateadd(day,-day(dateadd(month,1,current_timestamp)) , dateadd(month,1,current_timestamp) )
In a nutshell:
From your reference point-in-time,
Add 1 month,
Then, from the resulting value, subtract its day-of-the-month in days.
Voila! You've the the last day of the month containing your reference point in time.
Getting the 1st day of the month is simpler:
select dateadd(day,-(day(current_timestamp)-1),current_timestamp)
From your reference point-in-time,
subtract (in days), 1 less than the current day-of-the-month component.
Stripping off/normalizing the extraneous time component is left as an exercise for the reader.
A simple way to get the last day of month is to get the first day of the next month and subtract 1.
This should work on Oracle DB
select distinct last_day(trunc(sysdate - rownum)) dt
from dual
connect by rownum < 430
order by 1
I did the following and it worked out great. I also wanted the Maximum Date for the Current Month. Here is what I my output is. Notice the last date for July which is 24th. I pulled it on 7/24/2017, hence the result
Year Month KPI_Date
2017 4 2017-04-28
2017 5 2017-05-31
2017 6 2017-06-30
2017 7 2017-07-24
SELECT B.Year ,
B.Month ,
MAX(DateField) KPI_Date
FROM Table A
INNER JOIN ( SELECT DISTINCT
YEAR(EOMONTH(DateField)) year ,
MONTH(EOMONTH(DateField)) month
FROM Table
) B ON YEAR(A.DateField) = B.year
AND MONTH(A.DateField) = B.Month
GROUP BY B.Year ,
B.Month
SELECT * FROM YourTableName WHERE anyfilter
AND "DATE" IN (SELECT MAX(NameofDATE_Column) FROM YourTableName WHERE
anyfilter GROUP BY
TO_CHAR(NameofDATE_Column,'MONTH'),TO_CHAR(NameofDATE_Column,'YYYY'));
Note: this answer does apply for Oracle DB
Here's how I just solved this. day_date is the date field, calendar is the table that holds the dates.
SELECT cast(datepart(year, day_date) AS VARCHAR)
+ '-'
+ cast(datepart(month, day_date) AS VARCHAR)
+ '-'
+ cast(max(DATEPART(day, day_date)) AS VARCHAR) 'DATE'
FROM calendar
GROUP BY datepart(year, day_date)
,datepart(month, day_date)
ORDER BY 1