I have to compare datetimes using sql server 2012, that is, I have to see if certain datetimes from mytable1.start_time1 fall within the range of mytable2.start_time2 and mytable2.end_time2. By the datetime I mean the format of yyyy-mm-dd hh:mm:ss, and I can ignore the time.
What I do (and don't succeed) is as follows:
Select CUSTOMER_IDENTITY_NUMBER, a.CREATE_DATE, b.CREATE_DATE, b.END_DATE
from [mytable1]a
join [mytable2]b
on a.CUSTOMER_FK=b.CUSTOMER_PK
where cast(a.CREATE_DATE as date) not between cast(b.CREATE_DATE as date) and cast(b.END_DATE as date)
and cast(a.CREATE_DATE as date)<>cast(b.CREATE_DATE as date)
and cast(a.CREATE_DATE as date)<>cast(b.END_DATE as date)
Well, it doesn't work - the a.create_date returned does fall between the two other dates. Does anyone have an idea?
Thanks a bunch!
Try using a slightly different condition. I don't quite understand
the logical condition you used. Seems you got it incorrect.
select CUSTOMER_IDENTITY_NUMBER, a.CREATE_DATE, b.CREATE_DATE, b.END_DATE
from [mytable1]a
join [mytable2]b
on a.CUSTOMER_FK=b.CUSTOMER_PK
where
cast(a.CREATE_DATE as date) >= cast(b.CREATE_DATE as date)
and
cast(a.CREATE_DATE as date) <= cast(b.END_DATE as date)
Related
I have a table with Settlement details and I need a report in SSRS that returns settlement items with settlement date yesterday or older. I know this is trivial but there is something I am doing wrong and would appreciate help
This is for Microsoft SQL Server and basic table for simplification
Select
SETL.SettlementDay as SD,
SETL.Amount as Amount,
SETL.quantity as Q
From
Setlement as SETL
Where
SETL.SettlementDate < getdate()
This doesn't work for me. I expect the output would be settlements with settlement date older or equal to yesterday
Thanks
If you want yesterday or older, then use:
where SETL.SettlementDate < convert(date, getdate())
The conversion to date gets rid of the timestamp on getdate() (despite the name, it has the time as well as the date).
You might find this less confusing if you use current_timestamp (which is part of the SQL standard):
where SETL.SettlementDate < convert(date, current_timestamp)
could you try by using cast
Select
SETL.SettlementDay as SD,
SETL.Amount as Amount,
SETL.quantity as Q
From
Setlement as SETL
Where
cast( SETL.SettlementDate as date) < cast( getdate() as date)
I've tried looking around for this but I can't find an answer that seems to work with the code I'm using. Basically, the below query searches on any result from the current date. I'm trying to make it so it will search only on the last hour.
In oracle I could do this using sysdate-1/24, is there a simple equivalent within SQL server? Bearing in mind I'm already using cast to get the current sysdate.
Select distinct m_record_server
from search_recording_file1
where tr_date_recorded >= cast(convert(varchar(10), getdate(), 110) as datetime)
and m_record_server is not null
You can use:
getdate() - 1.0/24
SQL Server allows you to use such arithmetic on datetime.
The more common way would be:
dateadd(hour, -1, getdate())
In your query, you do not need to cast to a string at all:
Select distinct m_record_server
from search_recording_file1
where tr_date_recorded >= getdate() - 1.0/24 and m_record_server is not null;
If you want the date that was there one hour ago (which seems to be the intent of the code, if not the rest of the question):
Select distinct m_record_server
from search_recording_file1
where tr_date_recorded >= cast(getdate() - 1.0/24 as date) and m_record_server is not null;
I wrote the following query. This query returns two fields. Firstly, it returns an integer that represents a date, and it then converts that integer into a date.
select DISTINCT date_column,
cast(convert(varchar(10), date_column) as date) as [Week Label]
from table_one
order by date_column desc;
The conversion works well. However, the date that is returned contains a time. How can I get rid of the time?
You can simply use this to remove time part
convert(date, getdate())
OR
you can use this also
convert(varchar(10), getdate(),120)
convert(varchar(10), getdate(),101) doesn't have a time part, just the date.
I used http://www.w3schools.com/sql/func_convert.asp for the options but this one from MSDN may be better: http://msdn.microsoft.com/en-us/library/ms187928.aspx
select *
from Advertisements
where DepartureDate < DATEPART('dd.mm.yy', '09.10.2010');
but i get
Msg 1023, Level 15, State 1, Line 1
Invalid parameter 1 specified for datepart.
in plsql is this very simple here is so complicated...
Can someone tell me please how can i get all dates that are smaller than today.
You can use this to get the current date:
CONVERT(date, GETDATE())
See the documentation.
Can someone tell me please how can i get all dates that are smaller than today.
SELECT *
FROM Advertisements
WHERE DepartureDate < CONVERT(date, GETDATE())
You seem to be confusing DATEPART with FORMAT_DATE (which does not exist anyway).
DATEPART extracts certain part of a date. Exactly one part.
Dates that are smaller than today are < dbo.CropTime(getdate()), where CropTime is a function which can be written in different ways (such as those described in this question).
Or, in case you are using SQL Server 2008, it's as simple as < cast(getdate() as date).
Would that code really work in PL/SQL? The DATEPART in T-SQL function is used to extract individual portions of a date.
This will get you all the dates before now.
select * from Advertisements where DepartureDate < getdate()
If you're planning to hardcode the date (as your sample code suggests), you just need to format in a way that SQL Server will understand. eg.
select * from Advertisements where DepartureDate < '2010-10-09'
I've been told that date format works on every server regardless of its localization settings. It's certainly worked on every server I've tried it on - but I'm happy to be overruled :-)
What you are looking for I think is
select *
from Advertisements
where DepartureDate < Convert(Date, '09.10.2010', 102)
or possibly
SELECT *
FROM Advertisements
WHERE DepartureDate < Cast(CURRENT_TIMESTAMP as date)
DatePart is used for getting components of the date such as the month, year or day. To get dates that are smaller (older) than now I would do this.
select * from Advertisements where DepartureDate < GetDate();
If I wanted Departure dates that were yesterday or before I could do this.
select * from Advertisements where DepartureDate < Convert(DateTime,Convert(Char(10),GetDate(),121));
or
select * from Advertisements where DepartureDate < Convert(DateTime,floor(convert(int,GetDate())))
I have two tables where column [date] is type of DATETIME2(0).
I have to compare two records only by theirs Date parts (day+month+year), discarding Time parts (hours+minutes+seconds).
How can I do that?
Use the CAST to the new DATE data type in SQL Server 2008 to compare just the date portion:
IF CAST(DateField1 AS DATE) = CAST(DateField2 AS DATE)
A small drawback in Marc's answer is that both datefields have been typecast, meaning you'll be unable to leverage any indexes.
So, if there is a need to write a query that can benefit from an index on a date field, then the following (rather convoluted) approach is necessary.
The indexed datefield (call it DF1) must be untouched by any kind of function.
So you have to compare DF1 to the full range of datetime values for the day of DF2.
That is from the date-part of DF2, to the date-part of the day after DF2.
I.e. (DF1 >= CAST(DF2 AS DATE)) AND (DF1 < DATEADD(dd, 1, CAST(DF2 AS DATE)))
NOTE: It is very important that the comparison is >= (equality allowed) to the date of DF2, and (strictly) < the day after DF2. Also the BETWEEN operator doesn't work because it permits equality on both sides.
PS: Another means of extracting the date only (in older versions of SQL Server) is to use a trick of how the date is represented internally.
Cast the date as a float.
Truncate the fractional part
Cast the value back to a datetime
I.e. CAST(FLOOR(CAST(DF2 AS FLOAT)) AS DATETIME)
Though I upvoted the answer marked as correct. I wanted to touch on a few things for anyone stumbling upon this.
In general, if you're filtering specifically on Date values alone. Microsoft recommends using the language neutral format of ymd or y-m-d.
Note that the form '2007-02-12' is considered language-neutral only
for the data types DATE, DATETIME2, and DATETIMEOFFSET.
To do a date comparison using the aforementioned approach is simple. Consider the following, contrived example.
--112 is ISO format 'YYYYMMDD'
declare #filterDate char(8) = CONVERT(char(8), GETDATE(), 112)
select
*
from
Sales.Orders
where
CONVERT(char(8), OrderDate, 112) = #filterDate
In a perfect world, performing any manipulation to the filtered column should be avoided because this can prevent SQL Server from using indexes efficiently. That said, if the data you're storing is only ever concerned with the date and not time, consider storing as DATETIME with midnight as the time. Because:
When SQL Server converts the literal to the filtered column’s type, it
assumes midnight when a time part isn’t indicated. If you want such a
filter to return all rows from the specified date, you need to ensure
that you store all values with midnight as the time.
Thus, assuming you are only concerned with date, and store your data as such. The above query can be simplified to:
--112 is ISO format 'YYYYMMDD'
declare #filterDate char(8) = CONVERT(char(8), GETDATE(), 112)
select
*
from
Sales.Orders
where
OrderDate = #filterDate
You can try this one
CONVERT(DATE, GETDATE()) = CONVERT(DATE,'2017-11-16 21:57:20.000')
I test that for MS SQL 2014 by following code
select case when CONVERT(DATE, GETDATE()) = CONVERT(DATE,'2017-11-16 21:57:20.000') then 'ok'
else '' end
You may use DateDiff and compare by day.
DateDiff(dd,#date1,#date2) > 0
It means #date2 > #date1
For example :
select DateDiff(dd, '01/01/2021 10:20:00', '02/01/2021 10:20:00')
has the result : 1
For Compare two date like MM/DD/YYYY to MM/DD/YYYY .
Remember First thing column type of Field must be dateTime.
Example : columnName : payment_date dataType : DateTime .
after that you can easily compare it.
Query is :
select * from demo_date where date >= '3/1/2015' and date <= '3/31/2015'.
It very simple ......
It tested it.....