Hi i have a column having record (2017-11-21 02:16:22.870).
I want select only Date and Hour (i.e:- 2017-11-21 02) from above records.
If you want to round down by hour, you can do:
select dateadd(hour, datediff(hour, 0, datetimecol), 0)
If you can live with two columns, I would recommend:
select cast(datetimecol as date) as datepart, datepart(hour, datetimecol) as hourpart
If you want the output in a particular format, you need to convert to a string:
select convert(varchar(13), datetimecol, 121)
use datepart for hour and convert for date
select concat( concat(convert(date,'2017-11-21 02:16:22.870'),':'), datepart(hour,'2017-11-21 02:16:22.870'))
2017-11-21:2
To select date from getdate() format
SELECT CONVERT(date, '2017-11-21 02:16:22.870')--2017-11-21
and select hour from getdate() format
datepart(HOUR, '2017-11-21 02:16:22.870')--2
I would use CONVERT() :
SELECT CONVERT(VARCHAR(13), col, 120)
Related
how can I get the last recorded data of the time 23:59 from yesterday and the day before?
my code doesn't have a filter of the time yet so it only shows all the data from yesterday and the day before.
select *
from tbl_Total
where date between DATEADD(day, -3, GETDATE()) AND DATEADD(day, -1, GETDATE())
In your case,
select * from tbl_Total as of timestamp timestamp '2017-07-19 23:59:59'
and
select * from tbl_Total as of timestamp timestamp '2017-07-18 23:59:59'
Try this
select *
from tbl_Total
where date between dateadd(day,-3,convert(varchar(10),getdate(),112)) AND dateadd(day,-3,convert(varchar(10),getdate(),112)+ ' 23:59:59:997' )
This query will return yestarday date with time 23:59:59.
SELECT CAST(CAST(CAST(DATEADD(day, -1, GETDATE()) as DATE) as varchar(12)) +' 23:59:59' as datetime2)
So you can use it in your query:
select *
from tbl_Total
where date between DATEADD(day, -3, GETDATE()) AND CAST(CAST(CAST(DATEADD(day, -1, GETDATE()) as DATE) as varchar(12)) +' 23:59:59' as datetime2)
EDIT: More elegant way:
SELECT DATEADD(second, -1, DATEADD(dd, DATEDIFF(dd,0,GETDATE()),0))
This query returns yesterday date with time 23:59:59.
EDIT2: If you want to return the day before with time 23:59:59 you need to use this query:
SELECT DATEADD(second, -1, DATEADD(dd, DATEDIFF(dd,1,GETDATE()),0))
If you want to obtain any other day you can change number 2 and test it.
Assuming you don't know the exact time you can get the latest rows using ROW_NUMBER:
with cte as
( select *,
row_number() -- for each day sorted descending
over (partition by DATEADD(dd, DATEDIFF(dd,0,GETDATE()),0)
order by date desc) as rn
from tbl_Total
where -- yesterday between 23:59 and 23:59:99.999
( date >= DATEADD(dd, DATEDIFF(dd,0,GETDATE()),0) - (1.0/1440)
and date < DATEADD(dd, DATEDIFF(dd,0,GETDATE()),0)
)
or -- day before yesterday between 23:59 and 23:59:99.999
( date >= DATEADD(dd, DATEDIFF(dd,1,GETDATE()),0) - (1.0/1440)
and date < DATEADD(dd, DATEDIFF(dd,1,GETDATE()),0)
)
)
select * from cte
where rn = 1 --latest row only
I have a select, and I need to limit data only for two last days. So today is 24th Aug 2016 so I need to get data for 24th Aug 2016, 23th Aug 2016 and 22th Aug 2016. How can I do that? I suppose I should use DATEADD function, or something similar. It needs to be done dynamicaly.
I need to get something like this:
SELECT * FROM TABLE_NAME WHERE
DATE = '2016-08-22'
The date must be updated each day
DateAdd, exactly
SELECT * FROM TABLE_NAME
WHERE DATE BETWEEN dateadd(d,-2,cast(getDate() as date)) AND cast(getDate() as date)
SELECT *
FROM TABLE_NAME
WHERE cast(DATE as date) BETWEEN cast(DATEADD(D, -2, GETDATE()) as date) AND cast(GETDATE() as date)
SELECT * FROM TABLE_NAME WHERE
cast(DATE as date) >= cast(GETDATE()-2 as date)
or
--For limiting future dates
SELECT * FROM TABLE_NAME
WHERE
cast(DATE as date) in (cast(GETDATE()-2 as date),cast(GETDATE()-1 as date), cast(GETDATE() as date))
Use GETDATE() function
If there are future dates in this table you can perhaps use this
select * from table_name
where date <= getdate()
and date >= dateadd(day,-2,getdate())
SELECT * FROM TABLE_NAME WHERE
DATE >=cast(getdate()-2 as date)--so this will be rounded off to midnight and it is sargable
First you have to make today's date in absolute by converting it in float then floor and then convert it into small date time. And then using dateadd subtract 2 days from today's absolute date.
SELECT * FROM
TABLE_NAME
WHERE
DATE >= (SELECT DATEADD(dd,-2,CAST(FLOOR(CAST(GETDATE() as FLOAT)) as SMALLDATETIME)))
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.
I have two dates, From date and To Date.
Also i have two time fields, From Time and To Time.
The date field in the database is Datetime. I need to select data according to both date and time.
This is my query for selecting data between 13:00 to 15:00, but it is not suitable for 20:00 to 08:00.
where Date>= '2/01/2012' AND Date<'2/28/2013'
AND CAST(Date AS TIME) BETWEEN '20:00' AND '08:00'
Without seeing your specific error/unexpected results, I think the problem is that 20 is greater than 8.
You'll have to use two conditions:
where Date>= '2/01/2012' AND Date<'2/28/2013' AND (CAST(Date AS TIME) > '20:00' OR CAST(Date AS TIME) < '08:00')
EDIT: fixed condition
Is this what you are after?
WHERE Date BETWEEN '2012-01-01 20:00:00.000' AND '2012-12-01 08:00:00.000'
It is a little bit unclear whether you are attempting to generate the WHERE clause variables dynamically?
You need to combine your "date" and "time" parts together.
This code will illustrate how to do this:
SELECT the_date
, the_time
, DateAdd(hh, DatePart(hh, the_time), the_date) As hour_added
, DateAdd(mi, DatePart(mi, the_time), the_date) As minute_added
, DateAdd(mi, DatePart(mi, the_time), DateAdd(hh, DatePart(hh, the_time), the_date)) As both_added
FROM (
SELECT Cast('2013-02-28' As datetime) As the_date
, Cast('08:30' As datetime) As the_time
) As example
You can then use the resultant values in your comparison
I am looking for a good SQL Statement to select all rows from the previous day from one table. The table holds one datetime column. I am using SQL Server 2005.
get today no time:
SELECT dateadd(day,datediff(day,0,GETDATE()),0)
get yestersday no time:
SELECT dateadd(day,datediff(day,1,GETDATE()),0)
query for all of rows from only yesterday:
select
*
from yourTable
WHERE YourDate >= dateadd(day,datediff(day,1,GETDATE()),0)
AND YourDate < dateadd(day,datediff(day,0,GETDATE()),0)
To get the "today" value in SQL:
convert(date, GETDATE())
To get "yesterday":
DATEADD(day, -1, convert(date, GETDATE()))
To get "today minus X days": change the -1 into -X.
So for all yesterday's rows, you get:
select * from tablename
where date >= DATEADD(day, -1, convert(date, GETDATE()))
and date < convert(date, GETDATE())
It's seems the obvious answer was missing. To get all data from a table (Ttable) where the column (DatetimeColumn) is a datetime with a timestamp the following query can be used:
SELECT * FROM Ttable
WHERE DATEDIFF(day,Ttable.DatetimeColumn ,GETDATE()) = 1 -- yesterday
This can easily be changed to today, last month, last year, etc.
SELECT * from table_name where date_field = DATE_SUB(CURRENT_DATE(),INTERVAL 1 DAY);
Its a really old thread, but here is my take on it.
Rather than 2 different clauses, one greater than and less than. I use this below syntax for selecting records from A date. If you want a date range then previous answers are the way to go.
SELECT * FROM TABLE_NAME WHERE
DATEDIFF(DAY, DATEADD(DAY, X , CURRENT_TIMESTAMP), <column_name>) = 0
In the above case X will be -1 for yesterday's records
This should do it:
WHERE `date` = CURDATE() - INTERVAL 1 DAY
Can't test it right now, but:
select * from tablename where date >= dateadd(day, datediff(day, 1, getdate()), 0) and date < dateadd(day, datediff(day, 0, getdate()), 0)
In SQL Server do like this:
where cast(columnName as date) = cast(getdate() -1 as date)
You should cast both sides of the expression to date to avoid issues with time formatting.
If you need to control interval in more detail, then you should try something like:
declare #start datetime = cast(getdate() - 1 as date)
declare #end datetime = cast(getdate() - 1 as date)
set #end = dateadd(second, 86399, #end)
Another way to tell it "Yesterday"...
Select * from TABLE
where Day(DateField) = (Day(GetDate())-1)
and Month(DateField) = (Month(GetDate()))
and Year(DateField) = (Year(getdate()))
This conceivably won't work well on January 1, as well as the first day of every month. But on the fly it's effective.
Well, its easier to cast the datetime column to date and than compare.
SELECT * FROM TABLE_NAME WHERE cast(COLUMN_NAME as date) =
dateadd(day,0, convert(date, getdate(), 105))
A simple alternative
Select GETDATE() - 1
Change 1 to go back that many number of days
PS : This gives you timestamp accuracy.
This worked a charm:
SELECT * FROM mytable WHERE date(mydate) = DATE_SUB(CURDATE(), INTERVAL 1 DAY);
subdate(now(),1) will return yesterdays timestamp
The below code will select all rows with yesterday's timestamp
Select * FROM `login` WHERE `dattime` <= subdate(now(),1) AND `dattime` > subdate(now(),2)