SQL, check on specific date - sql

I wanna get all records where "F.EINDAT" is smaller or even as yesterday 8pm.
WHERE F.EINDAT <= yesterday at 8pm
How am I supposed to do that?
Thanks.

Try this
select substring(convert(varchar,(getdate()-1)),1,12)+'20:00:00 PM' as Early_Date

Try this.
WHERE F.EINDAT <= CONVERT(VARCHAR(10), DATEADD(day, -1, GETDATE()), 110) + ' 20:00'

Hope you will find this helpful
F.EINDAT <= cast(DateAdd(day, -1, GetDate()) as date) and
RIGHT('0' + CONVERT(VARCHAR(2), DATEPART(HOUR, F.EINDAT)), 2) + ':00' <= '20:00'

select * from tablename
where timestampcolumn > cast(current_date as timestamp) - interval'4'hour;
ISO/ANSI SQL compliant.

Thanks for all answers.
I got a statement which works for me:
where f.eindat >= to_date((to_Char (SYSDATE-1, 'yyyy-mm-dd') || ' 20:00'),'yyyy-mm-dd hh24:mi')

Related

How to get data for the last week data?

I am trying to write a report whereby the report will have the contract updated records for the past week using ms sql script. Every Monday the report will be sent for the last Mon-sun. I tried the below query. it doesnt seems to work. Any idea how to achieve this?
SET DATEFIRST 1
select distinct [...]
where CONTRACT_UPDATE_DATE>= dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate()))
AND CONTRACT_UPDATE_DATE< dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))
Thanks.
select * from table_abc WHERE
CAST(CONTRACT_UPDATE_DATE as date) between
CAST(DATEADD(dd, -7, GETDATE()) as date) and
CAST(GETDATE() AS DATE)
use dateadd to get 1 week before current date.
You can remove the cast as date if you need to validate by timestamp also.
You can try this,
SELECT Created_Date
FROM sample1
WHERE Created_Date >= DATEADD(day,-11117, GETDATE())
I suggest this solution:
select * from DB where Datediff(day, CONVERT(DATE, Sysdatetime()),
CONVERT(DATE, CONTRACT_UPDATE_DATE))<=7 and Datediff(day, CONVERT(DATE, Sysdatetime()),
CONVERT(DATE, CONTRACT_UPDATE_DATE))>0
You could try to achieve this by using the calender week.
Try the following:
-- Get last calendar week in format JJJJWW
DECLARE #jjjjkw varchar(6)
SELECT #jjjjkw =
CONVERT(varchar, DATEPART(YYYY,GETDATE()-7)) +
CASE WHEN LEN(CONVERT(varchar,DATEPART(ISO_WEEK,GETDATE()-7))) = 1 THEN '0'
+ CONVERT(varchar,DATEPART(ISO_WEEK,GETDATE()-7))
ELSE CONVERT(varchar,DATEPART(ISO_WEEK,GETDATE()-7)) END
-- Compare with the calendar week of your data-set
SELECT DISTINCT [...]
WHERE
CONVERT(varchar, DATEPART(YYYY,CONTRACT_UPDATE_DATE)) +
CASE WHEN LEN(CONVERT(varchar,DATEPART(ISO_WEEK,CONTRACT_UPDATE_DATE))) = 1
THEN '0' + CONVERT(varchar,DATEPART(ISO_WEEK,CONTRACT_UPDATE_DATE))
ELSE CONVERT(varchar,DATEPART(ISO_WEEK,CONTRACT_UPDATE_DATE)) END = #jjjjkw
Maybe not the most elegant way but it should work.

Sql Server select datetime without seconds

I have datetime column value below
2015-01-04 20:37:00.000
I tried below
cast(cast(MyDateColumn as date) as datetime)+cast(datepart(hour,MyDateColumn ) as float)/24
as MyDateColumn
and
CAST(CONVERT(CHAR(16),MyDateColumn,113) AS datetime) as MyDateColumn
These are did not work for me
How can i get above datetime as 01-04.2015 20:37 ?
Since MS SQL 2012, you can use FORMAT,
SELECT FORMAT([MyDateColumn], 'dd-MM.yyyy HH:mm')
In MYSQL it will work
SELECT DATE_FORMAT(date, '%Y-%m-%d %H:%i') AS formated_date FROM table;
In MS SQL It will work
SELECT FORMAT(getdate(), 'dd-mm-yyyy HH:mm')
In SQL Server this will work:
DECLARE #now [datetime];
SET #now = GETDATE();
SELECT
CONVERT([varchar](10), #now, 105) + ' ' +
RIGHT('0' + CONVERT([varchar](2), DATEPART(HOUR, #now)), 2) + ':' +
RIGHT('0' + CONVERT([varchar](2), DATEPART(MINUTE, #now)), 2);
In SQL Server this should do the trick:
declare #dt datetime = '2015-01-04 20:37:00.000'
select right('0' + cast(DATEPART(MM, #dt) as varchar), 2) + '-'
+ right('0' +cast(DATEPART(DAY, #dt) as varchar), 2) + '.'
+ cast(DATEPART(YEAR, #dt) as varchar) + ' '
+ right('0' +cast(DATEPART(HOUR, #dt) as varchar), 2) + ':'
+ right('0' +cast(DATEPART(MINUTE, #dt) as varchar), 2)
Simply,
SELECT CAST(CONVERT(varchar, GETDATE(), 100) as datetime)
Here's another way and you get a datetime in return.
SELECT DATEADD(
MILLISECOND,
DATEPART(MILLISECOND, '2016-02-16 13:45:24.573') * -1,
DATEADD(SECOND, DATEPART(SECOND,'2016-02-16 13:45:24.573') * -1,
'2016-02-16 13:45:24.573')
)
this is the way i do it. I needed to get -2 minutes
select CONVERT(datetime, CONVERT(CHAR(18), DATEADD(minute, -2, getdate()) , 113) + '00')
Format(Cast(Convert(varchar(15),Cast(timeval as Time),100) as DateTime),'hh:mm tt') As newtime
This will remove seconds from time as well as add AM,PM with time.

Subtract one day from datetime

I have a query to fetch date diff between 2 datetime as :
SELECT DATEDIFF(DAY, #CreatedDate , GETDATE())
Ex :
SELECT DATEDIFF(DAY, '2013-03-13 00:00:00.000' , GETDATE())
I need to have a query work like this which will subtract a day from created day:
SELECT DATEDIFF(DAY, **#CreatedDate- 1** , GETDATE())
Try this
SELECT DATEDIFF(DAY, DATEADD(day, -1, '2013-03-13 00:00:00.000'), GETDATE())
OR
SELECT DATEDIFF(DAY, DATEADD(day, -1, #CreatedDate), GETDATE())
I am not certain about what precisely you are trying to do, but I think this SQL function will help you:
SELECT DATEADD(day,-1,'2013-04-01 16:25:00.250')
The above will give you 2013-03-31 16:25:00.250.
It takes you back exactly one day and works on any standard date-time or date format.
Try running this command and see if it gives you what you are looking for:
SELECT DATEADD(day,-1,#CreatedDate)
To simply subtract one day from todays date:
Select DATEADD(day,-1,GETDATE())
(original post used -7 and was incorrect)
Apparently you can subtract the number of days you want from a datetime.
SELECT GETDATE() - 1
2016-12-25 15:24:50.403
This should work.
select DATEADD(day, -1, convert(date, GETDATE()))
SELECT DATEDIFF (
DAY,
DATEDIFF(DAY, #CreatedDate, -1),
GETDATE())
Try this, may this will help you
SELECT DATEDIFF(DAY, DATEADD(DAY,-1,'2013-03-13 00:00:00.000') , GETDATE())
To be honest I just use:
select convert(nvarchar(max), GETDATE(), 112)
which gives YYYYMMDD and minus one from it.
Or more correctly
select convert(nvarchar(max), GETDATE(), 112) - 1
for yesterdays date.
Replace Getdate() with your value OrderDate
select convert(nvarchar (max),OrderDate,112)-1 AS SubtractDate FROM Orders
should do it.
You can try this.
Timestamp=2008-11-11 13:23:44.657;
SELECT DATE_SUB(OrderDate,INTERVAL 1 DAY) AS SubtractDate FROM Orders
output :2008-11-10 13:23:44.657
I hope, it will help to solve your problem.

How can I find the data between two specific times in SQL

I need to run a query that finds all login dates (dd/mm/yyyy) and times (hh.mm) that occurred between 2am yesterday and 2am today. The login time in the database is formatted as mm-dd-yyyy hh.mm.ss.
I have tried multiple select and between queries that either return data on the wrong day or outside the time spans.
Based on the information that you provided, I can only give a generic example:
SELECT *
FROM [YourTable]
WHERE [YourDate] BETWEEN '08-15-2011 02:00:00' AND '08-16-2011 02:00:00'
**EDIT**
Per a good suggestion from the comments, here is a reusable version:
SELECT *
FROM [YourTable]
WHERE [YourDate] BETWEEN DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) + '02:00'
AND DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()-1), 0) + '02:00'
Assuming mysql:
SELECT * FROM your_table
WHERE STR_TO_DATE(your_date, '%m-%d-%Y %H.%i.%s') BETWEEN
DATE_SUB(STR_TO_DATE(DATE_FORMAT(NOW(), '%m-%d-%Y'), '%m-%d-%Y'), INTERVAL 22 HOUR) AND
DATE_ADD(STR_TO_DATE(DATE_FORMAT(NOW(), '%m-%d-%Y'), '%m-%d-%Y'), INTERVAL 2 HOUR)
I'm sure there's a better way though.
The query doesn't return any records if we use as follows...
SELECT *
FROM [YourTable]
WHERE [YourDate] BETWEEN DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) + '02:00'
AND DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()-1), 0) + '02:00'
We are using between clause so the oldest date should be first and the query becomes as follows...
SELECT *
FROM [YourTable]
WHERE [YourDate] BETWEEN DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()-1), 0) + '02:00'
AND DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) + '02:00'
This can also help you out
where pt.status in (1)
and pt.is_visible in ('t')
and pt.merchant in (0)
and (date(pt.created_at+'05:30') >= current_date-2
and extract(hour from(pt.created_at+'05:30')) >=17)
and ((pt.created_at+'05:30') <= current_date-1
and extract(hour from(pt.created_at+'05:30')) <=17)
I think you can use these tips :
SET #start = CURDATE() - interval 1 DAY + interval 2 HOUR;
SET #end = CURDATE() + interval 2 HOUR;
And :
SELECT * FROM TABLE_NAME WHERE DATE_FIELD BETWEEN #start AND #end

SQL query for today's date minus two months

I want to select all the records in a table where their date of entry is older then 2 months.
Any idea how I can do that?
I haven't tried anything yet but I am on this point:
SELECT COUNT(1) FROM FB WHERE Dte > GETDATE()
If you are using SQL Server try this:
SELECT * FROM MyTable
WHERE MyDate < DATEADD(month, -2, GETDATE())
Based on your update it would be:
SELECT * FROM FB WHERE Dte < DATEADD(month, -2, GETDATE())
Would something like this work for you?
SELECT * FROM FB WHERE Dte >= DATE(NOW() - INTERVAL 2 MONTH);
TSQL, Alternative using variable declaration. (it might improve Query's readability)
DECLARE #gapPeriod DATETIME = DATEADD(MONTH,-2,GETDATE()); --Period:Last 2 months.
SELECT
*
FROM
FB as A
WHERE
A.Dte <= #gapPeriod; --only older records.
SELECT COUNT(1) FROM FB
WHERE Dte > DATE_SUB(now(), INTERVAL 2 MONTH)
I use this on SQL Server:
SELECT
DATEADD(MONTH,-2,GETDATE()) '- 2 months'
FROM MyTable
SELECT COUNT(1)
FROM FB
WHERE
Dte BETWEEN CAST(YEAR(GETDATE()) AS VARCHAR(4)) + '-' + CAST(MONTH(DATEADD(month, -1, GETDATE())) AS VARCHAR(2)) + '-20 00:00:00'
AND CAST(YEAR(GETDATE()) AS VARCHAR(4)) + '-' + CAST(MONTH(GETDATE()) AS VARCHAR(2)) + '-20 00:00:00'