I have a Employee table like below
ID Name Mobile Ondate Address
1 Ankit 1234567895 2016-11-08 10:10:04.540 abc
2 Amit 4521545258 2016-11-08 11:10:04.540 bcd
3 Amit2 7541258562 2016-11-08 12:10:04.540 gfd
Now i write select query like below then it gives all records of Employee table
select * from Employee where convert(date,ondate)='2016-11-08 12:10:04.540'
but when i pass getdate() direct in where condition then it gives empty
select * from Employee where convert(date,ondate)=getdate()
while select getdate() result is 2016-11-08 12:10:04.540
so please give proper reason about it.
This is data type precedence at work. In your first query, in the WHERE clause you have a date on one side of a comparison and a varchar on the other. date wins, your string is converted to a date, the time is ignored and every row matches.
In your second query, you have a date on one side of the comparison and a datetime on the other side. datetime wins, the date is converted (back) into a datetime, and the datetimes don't match on their time components.
If you want to select values for today, use something like:
select * from Employee
where ondate >= DATEADD(day,DATEDIFF(day,0,GETDATE()),0) and
ondate < DATEADD(day,DATEDIFF(day,0,GETDATE()),1)
Where the DATEADD/DATEDIFF expressions are effectively computing "midnight at the start of today" and "midnight at the start of tomorrow". Both expressions will be computed once, and any index on the ondate column can then be used, if one exists, and we avoid excessively transforming column data.
try this.
select * from Employee where convert(date,ondate)=Convert(date,getdate())
SELECT * from Employee where DATEDIFF(DAY,Ondate,'2016-11-08 10:10:04.540') = 0
OR
SELECT * from Employee where DATEDIFF(DAY,Ondate,GETDATE()) = 0
Related
I am trying to exclude the results of an inner query in SQL (I am currently working on Googles cloud platform), I have the following table:
date | name
-----------+------------
2019-09-10 | gas_300x10
2019-09-10 | gas_250x10
2019-09-10 | gas_3x3
2019-09-11 | gas_300x10
2019-09-11 | gas_250x10
2019-09-11 | gas_4x4
I am trying to exclude the values where the name is equal to gas_300x10 and gas_250x10 for the date of 2019-09-10 only!
I want to keep the other values from that date and also want to keep where gas_300x10 and gas_250x10 occur on other days for example on the day 2019-09-11.
I have the following query which excludes the values for the date I do not want - so I don't want those two values for 2019-09-10:
SELECT *
FROM my_table
WHERE date = '2019-09-10'
AND (name = 'gas_300x10' OR name = 'gas_250x10')
This query would essentially return those values I do not want - how can I embed this as an inner query so that these results are excluded from the rest of the data?
I have tried using EXCEPT and NOT IN as a subquery but have not found any luck!
I think the code would work like this but I am unsure:
SELECT *
FROM my_table
EXCEPT
SELECT *
FROM my_table
WHERE date = '2019-09-10'
AND (name = 'gas_300x10' OR name = 'gas_250x10')
Use a combined expression:
select *
from mytable
where not (date = date '2019-09-10' and name in ('gas_300x10', 'gas_250x10'));
or
select *
from mytable
where date <> date '2019-09-10' or name not in ('gas_300x10', 'gas_250x10');
I would suggest NOT:
SELECT *
FROM my_table
WHERE NOT (date = '2019-09-10' AND
name IN ('gas_300x10', 'gas_250x10')
);
Note the use of IN to simplify the logic.
Alternatively, you can write this as:
SELECT *
FROM my_table
WHERE date <> '2019-09-10' OR
name NOT IN ('gas_300x10', 'gas_250x10');
Both of these assume that date and name are not NULL. The logic can be tweaked to handle this pretty easily, if that is possible.
I would NOT recommend using EXCEPT. First, it removes duplicates, so it does not do exactly the same logic. Second, it is doing much more work than necessary, matching the results of two subqueries rather than just filtering one table.
I would like to get one row with the maximum date. I cannot use group by as I need to retrieve all data in that row.
I have this:
ID Date Country
1 05/05/2019 US
2 05/06/2019 UK
I want to get this:
ID Date Country
2 05/06/2019 UK
I've tried the below but it didn't work for me
select TOP 1 ID, Date, country
from table
order by Date desc
I don't believe you. Here is a db<>fiddle that shows three different interpretations of the date in your sample data:
as a string
as mm/dd/yyyy
as dd/mm/yyyy
All three of them produce the same result.
I suspect that your actual data is more complicated and you have oversimplified the example for the question. Further, my suspicion is that the date column is stored as a string rather than a date.
As a string, you might have some hidden characters that affect the sorting (such as leading spaces).
If this is the case, fix the data type and your code will work.
This depends on what DB system you are using.
In Microsoft SQL server, you can use row_number() function:
select top 1 *
from facts
order by ROW_NUMBER() over (order by dateKey)
Can you try this?
select Top 1 ID,Date, country from table where date = max(date)
First set the DATE or DATETIME Datatype in your [Date] column
then try this code:
SELECT TOP 1 ID, [Date] , country FROM TableName ORDER BY Date DESC
SELECT ID,Date,Country from TableName Where Date = MAX(Date) AND Rownum <= 1
I want to select rows for a field MRD which is declared as date where it is prior for that date only.
So
(case when sum (transPoints) > 4 and MRD is that same date then 4
So if a row has a date of today, I want the case when to be triggered when the transaction points are bigger than 4 against all columns with the same date.
As you can imagine the date field will be different against many rows.
Based on what I can understand from your question, it seems that the GROUP BY clause may be what you're looking for. If your date column is in the correct format then you may have to use something like:
SELECT CAST(DateColumn as DATE)
FROM YourTable
GROUP BY CAST(DateColumn as DATE)
I have below information in table and want to retrive the count if difference between two dates is >= 1.
Id testdate exdate
1 20120502 20120501 --> This should included, because diff is 1
2 20120601 20120601 --> This should not included, because diff is 0
3 20120704 20120703 --> This should included, because diff is 1
4 20120803 20120802 --> This should included, because diff is 1
Based on the above data, my select count should return 3.
I am trying the following, but it's not giving any results:
select count(to_char(testdate,'YYYYMMDD')-to_char(exdate,'YYYYMMDD')) from test ;
select count(*)
from my_table
where testdate <> exdate
You really should convert those to a date data-type though... it saves a lot of problems in the long run.
Your query will give you results. It will return 4. It gives you results because as long as the result of testdate - exdate is not null it will return a value for that row.
However, as you're not using dates Oracle will most probably convert those to numbers, which won't help for date comparisons should you do that in the future.
20120901 - 20120831 = 70 -- not 1
Okay, from your comment:
Working with ,if i use down voteaccept select count(*) from test where
to_char((testdate,'YYYYMMDD') - to_char(exdate,'YYYYMMDD')) >= 1; .But
count is one of the column.how to retrive above select statement as
one of the column
you're trying something completely different.
Your dates are actually dates; it's helpful to post this. You're looking for an analytic function, specifically count().
select a.*, count(*) over ( partition by 1 ) as ct
from my_table a
where trunc(exdate) <> trunc(testdate)
Note the trunc function, which, without additional parameters will remove the time portion of the date this enabling a direct comparison without resorting to converting the date to a character.
select count(*)
from test
where to_date(testdate,'YYYYMMDD') - to_date(exdate,'YYYYMMDD') >= 1;
or
select count(*)
from test
where to_date(testdate,'YYYYMMDD') <> to_date(exdate,'YYYYMMDD');
Looking at testdate and exdate it looks more like the columns are VARCHAR type so you would require apropriate date conversion.
In Oracle if the type is date you can calculate with them. 1 equal 1 day. 1/24 equals 1 hour.
Your case is rather easy because you could even compare the strings.
SELECT count(*)
FROM test
WHERE testdate <> exdate
But it sounds like you want to be able to be variable, so you rather convert them to a date and then you can do
SELECT count(*)
FROM test
WHERE to_date(testdate,'YYYYMMDD')-to_date(exdate,'YYYYMMDD') >= 1
I am not sure what you want if testdate minus exdate is -1 or more because the exdate is after testdate. Then you can work with ABS
SELECT count(*)
FROM test
WHERE ABS(to_date(testdate,'YYYYMMDD')-to_date(exdate,'YYYYMMDD')) >= 1
See the image below. I have a table, tbl_AccountTransaction in which I have 10 rows. The lower most table having columsn AccountTransactionId, AgreementId an so on. Now what i want is to get a single row, that is sum of all amount of the agreement id. Say here I have agreement id =23 but when I ran my query its giving me two rows instead of single column, since there is nano or microsecond difference in between the time of insertion.
So i need a way that will give me row 1550 | 23 | 2011-03-21
Update
I have update my query to this
SELECT Sum(Amount) as Amount,AgreementID, StatementDate
FROM tbl_AccountTranscation
Where TranscationDate is null
GROUP BY AgreementID,Convert(date,StatementDate,101)
but still getting the same error
Msg 8120, Level 16, State 1, Line 1
Column 'tbl_AccountTranscation.StatementDate' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Your group by clause is in error
group by agreementid, convert(date,statementdate,101)
This makes it group by the date (without time) of the statementdate column. Whereas the original is grouping by the statementdate (including time) then for each row of the output, applying the stripping of time information.
To be clear, you weren't supposed to change the SELECT clause
SELECT Sum(Amount) as Amount,AgreementID, Convert(date,StatementDate,101)
FROM tbl_AccountTranscation
Where TranscationDate is null
GROUP BY AgreementID,Convert(date,StatementDate,101)
Because you have a Group By StatementDate.
In your example you have 2 StatementDates:
2011-03-21 14:38:59.470
2011-03-21 14:38:59.487
Change your query in the Group by section instead of StatementDate to be:
Convert(Date, StatementDate, 101)
Have you tried to
Group by (Convert(date,...)
instead of the StatementDate
You are close. You need to combine your two approaches. This should do it:
SELECT Sum(Amount) as Amount,AgreementID, Convert(date,StatementDate,101)
FROM tbl_AccountTranscation
Where TranscationDate is null
GROUP BY AgreementID,Convert(date,StatementDate,101)
If you never need the time, the perhaps you need to change the datatype, so you don't have to do alot of unnecessary converting in most queries. SQL Server 2008 has a date datatype that doesn't include the time. In earlier versions you could add an additional date column that is automatically generated to strip out the time companent so all the dates are like the format of '2011-01-01 00:00:00:000' then you can do date comparisons directly having only had to do the conversion once. This would allow you to have both the actual datetime and just the date.
You should group by DATEPART(..., StatementDate)
Ref: http://msdn.microsoft.com/en-us/library/ms174420.aspx