I have 10 rows for today's date but my select statement based on date dosen't seem to work....
SELECT Id,WirelessId,RegNo,DriverName1,MobileNo1,DriverName2,MobileNo1 from
DailySchedule where IsDeleted=0 and CreatedDate='2010-05-28'
Any suggestion...
Only if the dates assigned to them are midnight today. It might be better to do:
CreatedDate BETWEEN '2010-05-28 00:00' AND '2010-05-29 00:00'
If you want all the entries for May 28th I would do
and CreatedDate >='20100528'
and CreatedDate < '20100529'
Notice the safe ISO format (YYYYMMDD) no dashes
Also take a look at How Does Between Work With Dates In SQL Server? to see why between can not give you all the results you want
Assuming that CreatedDate is a datetime or a date, I don't see what's wrong with the syntax of the where clause. Maybe the IsDeleted field for those columns is actually NULL? (Or non-zero, at any rate.)
Edit: what Dave says, or DATE(CreatedDate) = '2010-05-28'
I'd check to see the type of the CreatedDate column and make sure that the string you're passing is being converted to a date properly.
When you say you 'have' 10 rows, is that confirmed visually or with a query executed in a console?
Not enough info to go on but I suspect that your dates have a time component and so don't match the string exactly.
Try using:
datediff(day, CreatedDate, '28-may-2010')) = 0
SELECT Id,WirelessId,RegNo,DriverName1,MobileNo1,DriverName2,MobileNo1 from
DailySchedule where IsDeleted=0 and date_format(CreatedDate, "%Y-%m-%d")='2010-05-28'
This should work too
select Id,
WirelessId,
RegNo,
DriverName1,
MobileNo1,
DriverName2,
MobileNo1
from DailySchedule
where IsDeleted=0
and CONVERT(varchar,CreatedDate,101) ='05/28/2010'
Related
I'm trying to add a wildcard to my date select query so i only pull the day not time. I.e. 2021-03-11 17:54:30.123. I thought a number could be substituted for a #.
select AID, LocalCoAltIn,LocalCoAltOut,EventTime
from EXCDS.dbo.WKS_LOG_VIEW
where EventTime like '2021-03-11 ##:##:##:###';
My query is returning no values even though they are in the table. Thanks.
No! Don't use strings! One method is to convert to a date:
select AID, LocalCoAltIn,LocalCoAltOut,EventTime
from EXCDS.dbo.WKS_LOG_VIEW
where convert(date, EventTime) = '2021-03-11';
Another method is to use a range:
where EventTime >= '2021-03-11' and
EventTime < '2021-03-12'
The LIKE operator in most flavors of SQL only support _ and * wildcards (matching any one single, or multiple characters). Gordon has given you a better approach, but if you wanted to fix your current query on SQL Server you could try:
SELECT AID, LocalCoAltIn, LocalCoAltOut, EventTime
FROM EXCDS.dbo.WKS_LOG_VIEW
WHERE EventTime LIKE '2021-03-11 [0-9][0-9]:[0-9][0-9]:[0-9][0-9]:[0-9][0-9][0-9]';
SQL Server extended the LIKE operator to accept a few extra things, such as character classes. Here [0-9] inside LIKE would match any single digit.
Not sure that like operator would work for date as you want, but you still have few options.
Use DATEPART function to retrieve year\month\etc and compare it with exact value that you need
select AID, LocalCoAltIn,LocalCoAltOut,EventTime from EXCDS.dbo.WKS_LOG_VIEW where DATEPART(year,EventTime) = 2021 AND DATEPART(month,EventTime) = 3 AND DATEPART(day,EventTime = 11);
Or use Gordon Linoff suggestion if you dont care about exact date part and only need to compare entire date without time
I've written a query to check whether more than one record exists on the same day. Currently the excerpt from my query that performs the restriction looks like this :
GROUP BY
entry_date
HAVING
COUNT(entry_date) > 1
As the entry date column is defined as a datetime, does it check against the full datetime or just the date?
Thanks.
if entry_date is DATETIME ,your group by wont work as expected.you need to CAST it to DATE.Cast(Datetime)to date is sargable as well.
GROUP BY cast(entry_date as DATE)
having count(cast(entry_date as DATE)) > 1
Since you don't cast or convert it to anything else, naturally it uses all data available. So it would group data together with the exact same datetime. Why would you expect anything else?
It should be full datetime.
CONVERT(date, entry_date)
should separate out the date.
There is this strange error in sql query.
The query is something like this.
select * from student where dob between '20150820' and '20150828'
But in the database the column of dob is varchar(14) and is in yyyyMMddhhmmss format,Say my data in the row is (20150827142545).If i fire the above query it should not retrive any rows as i have mentioned yyyyMMdd format in the query.But it retrives the row with yesterday date (i.e 20150827112535) and it cannot get the records with today's date (i.e 20150828144532)
Why is this happening??
Thanks for the help in advance
You can try like this:
select * from student
where convert(date,LEFT(dob,8)) between
convert(date'20150820') and convert(date,'20150828'))
Also as others have commented you need to store your date as Date instead of varchar to avoid such problems in future.
As already mentioned you would need to use the correct date type to have between behave properly.
select *
from student
where convert(date,LEFT(dob,8)) between '20150820' and '20150828'
Sidenote: You don't have to explicitly convert your two dates from text as this will be done implicitly as long as you use an unambiguous date representation, i.e. the ISO standard 'YYYYMMDD' or 'YYYY-MM-DD'. Of course if you're holding the values in variables then use date | datetime datatype
declare #startdate date
declare #enddate date
select *
from student
where convert(date,LEFT(dob,8)) between #startdate and #enddate
Sidenote 2: Performing the functions on your table dob column would prevent any indexes on that column from being used to their full potential in your execution plan and may result in slower execution, if you can, define the correct data type for the table dob column or use a persistent computed column or materialised view if your performance is a real issue.
Sidenote 3: If you need to maintain the time portion in your data i.e. date and time of birth, use the following to ensure all records are captured;
select *
from student
where
convert(date,LEFT(dob,8)) >= '20150820'
and convert(date,LEFT(dob,8)) < dateadd(d,1,'20150828')
All you have to do is to convert first the string to date.
select *
from student
where dob between convert(date, '20150820') and convert(date, '20150828')
Why is this happening?
The comparison is executed from left to right and the order of characters is determined by the codepage in use.
Sort Order
Sort order specifies the way that data values are sorted, affecting
the results of data comparison. The sorting of data is accomplished
through collations, and it can be optimized using indexes.
https://msdn.microsoft.com/en-us/library/ms143726.aspx
There are problems with between in T-SQL.
But if you want a fast answer convert to date first and use >= <= or even datediff to compare - maybe write a between function yourself if you want the easy use like between and no care about begin and start times ...
What do BETWEEN and the devil have in common?
The date in my table looks like 2010-06-16 00:00:00.000
When I use the below regular expression, no rows get selected.
select mydate from mytable where mydate like '2010%'
However, when I use this one, rows do get selected correctly.
select mydate from mytable where mydate like '%2010%'
So, I thought that probably leading white spaces are getting added. To take those into account, I used the below query. But this doesn't work either.
select mydate from mytable where LTRIM(RTRIM(mydate)) like '2010%'
Since the first one works, it means, that there is something before the 2010? What else apart from white spaces, could it be? I tried pasting it onto a text file, and don't see anything.
If the datatype is Datetime you should use Year() function to filter the rows instead of like. You should not use like operator to filter the data from Datetime type.
If the datatype is Datetime then you won't be having leading or trailing spaces.
You do have appropriate Date functions in Sql Server use those to filter the rows.
select mydate from mytable where year(mydate) = 2010
or even Datepart
select mydate from mytable where Datepart(YYYY,mydate) = 2010
You have to convert datetime that first in varchar to use like
select mydate from mytable
WHERE CONVERT(VARCHAR(10),mydate,120) like '2010%'
DEMO
You probably should use just date range fetches, for example
select mydate from mytable where mydate >= '20100101' and mydate < '20110101'
Using functions for the column (e.g. year(mydate)) is not a good idea because then indexes can't be used. Using 'between' can also cause problems with datetime columns because the time part can cause you to accidentally leave out rows.
The reason you found something with your search is probably due to typecasting the date into varchar before comparison, and format is such that the year is at the end, but please don't use that.
This seems stupid but, I simply need a list of dates to be ordered with the most recent date at top. Using order by DESC doesn't seem to be working the way I want it to.
SELECT *
FROM vw_view
ORDER BY EventDate DESC
It gives me the date ordered by month and day, but doesn't take year into consideration.
for example:
12/31/2009
12/31/2008
12/30/2009
12/29/2009
Needs to be more like:
12/31/2009
12/30/2009
12/29/2009
12/28/2009
and so on.
I'm guessing EventDate is a char or varchar and not a date otherwise your order by clause would be fine.
You can use CONVERT to change the values to a date and sort by that
SELECT *
FROM
vw_view
ORDER BY
CONVERT(DateTime, EventDate,101) DESC
The problem with that is, as Sparky points out in the comments, if EventDate has a value that can't be converted to a date the query won't execute.
This means you should either exclude the bad rows or let the bad rows go to the bottom of the results
To exclude the bad rows just add WHERE IsDate(EventDate) = 1
To let let the bad dates go to the bottom you need to use CASE
e.g.
ORDER BY
CASE
WHEN IsDate(EventDate) = 1 THEN CONVERT(DateTime, EventDate,101)
ELSE null
END DESC
try ORDER BY MONTH(Date),DAY(DATE)
Try this:
ORDER BY YEAR(Date) DESC, MONTH(Date) DESC, DAY(DATE) DESC
Worked perfectly on a JET DB.
You have the field in a string, so you'll need to convert it to datetime
order by CONVERT(datetime, EventDate ) desc
Assuming that you have the power to make schema changes the only acceptable answer to this question IMO is to change the base data type to something more appropriate (e.g. date if SQL Server 2008).
Storing dates as mm/dd/yyyy strings is space inefficient, difficult to validate correctly and makes sorting and date calculations needlessly painful.
what is the type of the field EventDate, since the ordering isn't correct i assume you don't have it set to some Date/Time representing type, but a string. And then the american way of writing dates is nasty to sort
If you restructured your date format into YYYY/MM/DD then you can use this simple string ordering to achieve the formating you need.
Alternatively, using the SUBSTR(store_name,start,length) command you should be able to restructure the sorting term into the above format
perhaps using the following
SELECT *
FROM vw_view
ORDER BY SUBSTR(EventDate,6,4) + SUBSTR(EventDate, 0, 5) DESC
Try this
SELECT *
FROM vw_view
ORDER BY DATE_FORMAT(EventDate, "%m-%d-%y") DESC