IF condition with getdate() in sybase - sql

I have a requirement where if
getdate()
returns time between midnight 12 to 2 AM, I should consider Date of previous day. How to frame this in a select query ?

The simplest method is to subtract two hours from the date and do the comparison. For instance:
where col_date < convert(date, dateadd(hour, -2, getdate()))

Related

Get last 7 days data from today 7am to last 7 days 7am

I have a query that currently can view the timestamp between yesterday 7am and today 7am data. How do I extract the timestamp of last 7 day timestamp. For example, if today is 10th August 2022, I would like to see the data of 3rd August 7am to 10th August 7am data. This is my current SQL query:
select TOP (10000000) id, PartNum, TimeStamp,Station
from test_module
where TimeStamp >= '2022-07-16 07:00:00'
and Timestamp <= '2022-07-17 07:00:00'
order by TimeStamp asc
datetimes can participate in addition. Thus, you can construct the day parts(remove the time first), and then add 7 hours as a time:
declare #start datetime = dateadd(day,-6,convert(datetime,(convert(date,getdate()))) + convert(datetime,convert(time,'07:00:00'))
declare #end datetime = dateadd(day, 0,convert(datetime,(convert(date,getdate()))) + convert(datetime,convert(time,'07:00:00'))
Be careful tossing the term "timestamp" around in SQL Server, it could lead to...miscommunications...
Timestamp >= dateadd(hour, 7, dateadd(day, -7, convert(datetime,
convert(date, getdate()))))
and Timestamp <= dateadd(hour, 7, convert(datetime, convert(date,
getdate())))
Can use getdate() function and for last 7 days, -7
For instance,
where DateCol between DateAdd(DD,-7,GETDATE() ) and GETDATE()
Try dateadd
See also thread here

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')

Show record data in the last 1 hour in sybase ASA11

select * from Trx
where
RequestTimestamp BETWEEN DATE(NOW()) AND DATEADD(HOUR, -1, GETDATE())
I have this sql code from sybase ASA11, hoping to show data in last 1 hour, but it just shows today's record from 00:00:00.000 AM till now(). What is wrong with my script, so it can show all of record from 1 last hour to now(). Can somebody help me?
It looks like you want:
where requestTimestamp >= dateadd(hour, -1, getdate())
If you have requestTimestamp in the future, then an upper bound is also needed:
where requestTimestamp between dateadd(hour, -1, getdate()) and getdate();

get todays date with last 6 months back

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)

Datediff GETDATE Add

In this type of code,
AND Orders.ShipDate >= DATEADD(Day, Datediff(Day,0, GetDate() -6), 0)
It supposed to pull records with the date 6 days ago, until today. How can I make it pull records from 7 days ago until yesterday?
I know changing -6 to -7 will pull records from 7 days ago, but which variable is the end of the date span so I can change it to -1?
It's not a date span.
The condition you have there is really only one condition: greater than. The right side of the greater than is 6 days ago, so your condition matches any date that is later than the date six days ago. In other words, it doesn't stop at Today; it includes tomorrow, next week, and next year, too.
AND ( Orders.ShipDate >= DATEADD(Day, Datediff(Day,0, GetDate() -7), 0)
AND Orders.ShipDate < DATEADD(Day, Datediff(Day,0, GetDate()), 0) )
That's what you really want. It matches dates which are later than midnight of the day 7 days ago, and dates which are before midnight today (which is any time yesterday).
The "end of the date span" isn't in your query.
Change your code to:
AND (Orders.ShipDate BETWEEN DATEADD(Day, -1, GetDate()) AND DATEADD(Day, -7, GetDate()))
This should work too and it eliminates the needless addition of 0 days:
select *,DATEDIFF(Day, orders.ShipDate, GETDATE()) AS DAYS_SINCE_TODAY
from Orders
where DATEDIFF(Day, orders.ShipDate, GETDATE()) >= 1 AND --This many days since today
DATEDIFF(Day, orders.ShipDate, GETDATE()) <= 7 --Going back this many days
I like the BETWEEN function. This may be a different flavor of SQL than you are using, but this is what I use.
BETWEEN dateadd(second, 0, dateadd(dd,datediff(dd,7,getdate()),0)) AND dateadd(second,-1, dateadd(dd,datediff(dd,0,getdate()),0))