get todays date with last 6 months back - sql

I want the exact date as 22/06/2015 from the query
whose Joining date should be exact
22/06/2015
which is exact 6 months back from todays date
I tried like below
Select date_of_joining,* from emp_mst Where Dt_Of_Join >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, getdate())), 0)
but it didn't worked.
what is the exact query for that ?
I am using SQL- server- 2005

if you want the EXACT joining date ( /date_of_joining.... /Dt_Of_Join)
what about
select distinct employee.name from emp_mst where date_of_joining = DATEADD(month, -6, GETDATE())
or if you want the actual date returned in a different format:
CONVERT(Date,DATEADD(month, -6, GETDATE()), 103)
which is applicable if you select this field

I'm not a SQL Server guru, but I found easy answers everywhere for this.
Try this link to another post which explains this exact question SQL Server 2005: how to subtract 6 month
You refer to the word "exact" date, so you don't need the datediff section, you can just subtract 6 months from the current date using "dateadd" which will give you a precise date. Just remember to correctly type cast else you will have to be accurate to the millisecond.

SELECT employee_name
FROM emp_mst
WHERE Dt_Of_Join = Cast(DATEADD(month, -6, GETDATE()) As Date)
ORDER BY Dt_Of_Join DESC
I think he just need to know how to remove time from date

Note that you need to handle time part effectively
Select * from emp_mst
Where
Dt_Of_Join >= dateadd(month,-6,dateadd(day,datediff(day,0,getdate()),0)
and
Dt_Of_Join < dateadd(month,-6,dateadd(day,datediff(day,0,getdate()),1)

You want previous 6 month date so use dateadd() and for your date formate DD/MM/YYYY you should try to convert(varchar(10),date,101).
Select date_of_joining,* from emp_mst Where
Dt_Of_Join=convert(varchar(10),dateadd(month,-6,getdate()),101)

Related

SQL : Get yesterdays day with a certain timestamp

i am trying to get yesterdays date in SQL SERVER with a certain timestamp.
I know how to get yesterdays date in SQL without the timestamp using the following query :
Which gives me every startdate starting from '00:00:00':
SELECT se.startdate
FROM sessions se
WHERE se.startdate >= DATEADD(day, -1, CAST(GETDATE() AS date))
Instead I want to get the yesterdays date with a certain timestamp.
For this example every startdate starting from '06:00:00'(AM). Like shown below:
SELECT se.startdate
FROM sessions se
WHERE se.startdate >= '05-09-2022 06:00:00'
If I do it the way it was shown in the second example I would have to change the day manually, which would obviously repetitive.
Is there a way to combine the first example with second one so that we get always yesterdays date at
'06:00:00' ?
(OR ANY GIVEN TIME )
Might as well add another solution, DATETIMEFROMPARTS:
DECLARE #Yesterday date = DATEADD(DAY, -1, GETDATE());
SELECT DATETIMEFROMPARTS(YEAR(#Yesterday),MONTH(#Yesterday),DAY(#Yesterday),6,0,0,0);
For example 06:20:40 of the previous day:
select DATEADD(second, (6 * 60 + 20 ) * 60 + 40, cast(DATEADD(day, -1, cast(GETDATE() AS date)) as datetime)) t
you can do it this way:
SELECT DATEADD(day, DATEDIFF(day, 1, GETDATE()), '06:00:00')

SQL Query + How to look back at a specific date

I have a query that currently looks at receipts back 28 days from the date of purchase.
AND PAYMENT_DATE >= DATEADD(DD, -28, CONVERT(DATE, GETDATE()))
Now I this should go back to November 15 of the previous year, still using PAYMENT_DATE.
Is it possible to get help on how to accomplish this change?
Thank you.
Just subtract a year from todays date and build a new date using DATEFROMPARTS
AND PAYMENT_DATE >= DATEFROMPARTS(YEAR(GETDATE())-1, 11, 15);
Use the follwing code
datediff(dd, PAYMENT_DATE, '20201115') = 0
Another approach
AND CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, PAYMENT_DATE))) = '15/11/2020'

SQL get previous month (in January too)

Hello I'm looking for simple way, how to get data from previous month. I get this code but it didn't work in January (result is 12 2021 and I need 12 2020)
select month(dateadd(month,-1,getdate())), year(getdate())
Presumably, you have some sort of date column.
In SQL Server, you can express this concept using datediff():
where datediff(month, datecol, getdate()) = 1
However, that is not "sargable", meaning that it prevents the use of indexes. So, I would instead recommend:
where datecol < datefromparts(year(getdate()), month(getdate()), 1) and
datecol >= dateadd(month, 1, datefromparts(year(getdate()), month(getdate()), 1))
If you simply want the first day of the previous month, you can use:
dateadd(month, 1, datefromparts(year(getdate()), month(getdate()), 1))
Try This
select CASE WHEN month(getdate())>1 THEN month(getdate())-1 ELSE 12 END ,
CASE WHEN month(getdate())>1 THEN YEAR (getdate()) ELSE YEAR (getdate()) -1 END
Using the answer given here: How can I select the first day of a month in SQL?
SELECT dateadd(month,-1,DATEADD(month, DATEDIFF(month, 0, getdate()), 0)) as previousmonth;
output:
2020-12-01 00:00:00.000
I can provide next query using FORMAT function:
SELECT
-- get current day in previous month
FORMAT(dateadd(month, -1, getdate()), 'yyyy-MM-dd') as current_previousmonth,
-- get first of previous month
FORMAT(dateadd(month, -1, getdate()), 'yyyy-MM-01') as first_previousmonth,
-- previous month without date
FORMAT(dateadd(month, -1, getdate()), 'yyyy-MM') as previousmonth;
test T-SQL here
Here you go!
select dateadd(mm,-1,eomonth(getdate())) as [Previous Month]
Result:
Previous Month
--------------
2020-12-31
You could also use CONVERT() or FORMAT() functions to format the date as you desire.
Try
SELECT FORMAT(DATEADD(month, -1, GETDATE()),'MM/yyyy');
It will give you previous month and the year. If you are comparing to a date column in a existing table, then you need the date part too as you want to know December of which year was the previous month. But if you don't want the year, then just adjust the 'MM/yyyy' part to suit your purpose.

How to go back two days from a date in sql?

I wrote an SQL query that allows me to get the sales of certain stores.
My query runs every mornings and I would like to get the sales from 2 days ago at runtime.
For example if my query runs tomorrow morning, on 08/12, I would like to have the sales whose value in the column "GP_HEURECREATION" starts with "20200612", to have all the sales of the whole day.
The GP_HEURECREATION column has a format like this: "20200612 00:00:00" and is of the DATE type.
I tried with NOW() and DATEADD() but I have 2018 values that stand out for example.
How can I get the values only two days before the query is executed?
SELECT
T_ETABLISSEMENT, ET1.ET_LIBELLE AS C1, GL_ETABLISSEMENT,
GP_HEURECREATION, GP_REFINTERNE, GL_CODEARTICLE,
LIBDIM2, LIBDIM1, GL_QTEFACT, GL_PUTTC,
(GL_TOTALHT * GP_COTATIONDOS) AS TOTALHTDEV, GL_DPR, GL_DEVISE,
GL_NATUREPIECEG, GA_LIBELLE
FROM
GCLIGNEARTDIM
LEFT OUTER JOIN
PGI_LOOKUP(TTETABLISSEMENT) ET1 ON GL_ETABLISSEMENT = ET1.ET_ETABLISSEMENT
WHERE
(GP_HEURECREATION <= DATEADD(day, -2, GETDATE())
AND (GL_NATUREPIECEG = "FFO")
AND GL_ETABLISSEMENT = "20897", "10519", "20267", "26451", "20269", "26078", "28047", "20900", "28085", "24984", "27113", "20268", "19994", "28450", "26876", "24063", "18066", "3220"
ORDER BY
GP_REFINTERNE
The syntax of your existing query suggests SQL Server. If you want records that belong to day -2, you can do:
where gp_heurecreation >= dateadd(day, -2, convert(date, getdate()))
and gp_heurecreation < dateadd(day, -1, convert(date, getdate()))
If gp_heurecreation has no time component (in SQL Server, that's a date datatype), this is simpler:
where gp_heurecreation = dateadd(day, -2, convert(date, getdate()))

retrieve day after tomorrow date query

I want to select day after tomorrow date in sql. Like I want to make a query which select date after two days. If I select today's date from calender(29-04-2015) then it should show date on other textbox as (01-05-2015). I want a query which retrieve day after tomorrow date. So far I have done in query is below:
SELECT VALUE_DATE FROM DLG_DEAL WHERE VALUE_DATE = GETDATE()+2
thanks in advance
Note that if you have a date field containing the time information, you will need to truncate the date part using DATEADD
dateadd(d, 0, datediff(d, 0, VALUE_DATE))
To compare 2 dates ignoring the date part you could just use DATEDIFF
SELECT VALUE_DATE FROM DLG_DEAL
WHERE datediff(d, VALUE_DATE, getdate()) = -2
or
SELECT VALUE_DATE FROM DLG_DEAL
WHERE datediff(d, getdate(), VALUE_DATE) = 2
Try like this:
SELECT VALUE_DATE
FROM DLG_DEAL WHERE VALUE_DATE = convert(varchar(11),(Getdate()+2),105)
SQL FIDDLE DEMO
SELECT VALUE_DATE FROM DLG_DEAL WHERE datediff(d, VALUE_DATE, getdate()) = -2
** I think you should try this**
SELECT DATEADD(day,2,VALUE_DATE) AS DayAfterTomorrow
FROM DLG_DEAL WHERE VALUE_DATE= GETDATE();
DATEADD(choiceToAdd, interval, date)
This function allows you to add or substract day,month, year,etc from date. In this interval is nothing but numeric value which you want to add or substract.