I have a table which stores people's orders, and within this table is a DateTime field. How can I get the information through a query, which includes only today's Date, without needing the time?
WHERE CONVERT(DATE, YourDateField) = CONVERT(DATE, GETDATE())
As you didn't add a RDBMS - this solution works for MS SQL Server:
WHERE DATE(yourdatefield) = Convert(date, getdate())
Related
If I use this in my dataset SQL:
DECLARE #StartDate DATE = '2018-01-01'
DECLARE #EndDate DATE = '2018-03-01'
The report runs and returns the expected Data.
When I comment out the #StartDate & #EndDate variables - delete the two parameters from the Report - and run the report using Date Prompts at run time [using the same Dates] - I get no data returned.
The Date field that I am attempting to filter on is a Datetime field.
I have tried the following two approaches in my SQL:
o.ORDERDATE >= #StartDate And o.ORDERDATE <= #EndDate
cast(o.ORDERDATE as DATE) >= Cast(#StartDate As Date) And cast(o.ORDERDATE as DATE) <= Cast(#EndDate As Date)
No data.
I added a Text Box to the Report Header and put the #StartDate Parameter value in there and got this: 01/01/2018 12:00:00 AM at run time.
I have gone back and forth a few times between uncommenting and commenting the Date variables at the top of the SQL. When I use the local variables - I get data. When I use the Date Parameters - no data.
I use Date Prompts in many of my other reports with no problems. I will go back and see if any of them is on this particular table and this particular date field - or if any of the other dates are Datetime fields ....
Meanwhile, I would appreciate any suggestions.
Thanks!
I would suspect a conversion issue comparing Date and DateTime types. To prove this, set the dates manually in your query to the full date and time. if this produces no results then try something simple like.
WHERE CAST(o.ORDERDATE AS Date) Between #StartDate AND #EndDate
Bear in mind that if you start your date with the year (I think) by default this is interpreted as being in YYYY-MM-DD format.
I need to write a function which will return me the previous month and year.
Like if the input is : function_name('February', 2015) the output will be 'January', 2014.
In SQL Server 2008 there is the date data type, which has no time attached. You can thus remove the time portion quite easily simply by converting, then performing the DateAdd.
SELECT DateAdd(month, -1, Convert(date, GetDate()));
This will return a date data type. To force it to be datetime again, you can simply add one more Convert:
SELECT Convert(datetime, DateAdd(month, -1, Convert(date, GetDate())));
You may not need the explicit conversion to datetime, though.
Working with one of my applications, I am adding two databases support to my project, but I am very limited in my knowledge with MSSQL. My MySQL code is like below
SELECT count(ip_address) AS failed_login_attempt
FROM failed_login
WHERE ip_address = '$ip'
AND date BETWEEN DATE_SUB( NOW() , INTERVAL 1 DAY ) AND NOW()
and I am trying to do same kind of functionality with MSSQL:
SELECT count(ip_address) AS failed_login_attempt
FROM failed_login
WHERE ip_address = '$ip'
AND date between [tHIS pIECE OF cODE I DO NOT KNOW HOW TO FIx ]
SQL Server has several ways of returning the current date, for example getdate(). To subtract one day from the current datetime use the dateadd function. So this:
BETWEEN DATE_SUB( NOW() , INTERVAL 1 DAY ) AND NOW()
should be equivalent to
BETWEEN dateadd(day, -1, getdate()) and getdate()
I need to write the common queries for MySQL and Oracle databases. Problem occurs when I have to put date conditions.
For example: there is one field Txn_date which is in format of '20150116' in MySQL and '16-JAN-2015' in Oracle.
I use date_format(now(),'%Y%m%d') for MySQL and to_char(sysdate,'dd-MON-YYYY') for Oracle.
Is there any common function of way by which I can use the same function in both Oracle and MySQL?
I tried Txn_date in ( date_format(now(),'%Y%m%d') OR to_char(sysdate,'dd-MON-YYYY') ) but did not work because to_char() not recognized in MySQL.
First, in MySQL dates usually have the following format when converted implicitly - 2015-01-16 - rather than 20150116. I think you can do the following in both MySQL and Oracle (it is standard SQL) - I've checked it in Oracle (10g) and it works, and it seems to work in my fiddling with MySQL:
SELECT * FROM mytable
WHERE mydate IN ( DATE '2015-01-16', DATE '2015-01-18' );
The string literal to be converted to DATE has to be of the form yyyy-mm-dd. Now this will work if your dates are dates and don't have a time portion. Now if your dates do have a time portion, then things become more difficult since MySQL uses the DATE() function to get the date portion, while Oracle would use TRUNC(). But you can get around that with judicious use of >= and <, e.g.:
SELECT * FROM mytable
WHERE ( mydate >= DATE '2015-01-16' AND mydate < DATE '2015-01-17' )
OR ( mydate >= DATE '2015-01-18' AND mydate < DATE '2015-01-19' );
Now if you want to use SYSDATE, the best thing to do would be to use the ANSI standard CURRENT_DATE or CURRENT_TIMESTAMP. These can be compared directly with no need for formatting and should work in both MySQL and Oracle. You can also do date arithmetic using INTERVAL, in which case you could try the following:
SELECT * FROM mytable
WHERE mydate > CURRENT_DATE - INTERVAL '1' DAY;
UPDATE I've been doing some thinking about this. The query immediately above doesn't really work if you want to get all the rows that have been entered today. The difficulty is that Oracle recognizes ANSI date literals as dates (that is, with no time portion), but there isn't, as far as I know, an ANSI-standard way of converting a date/time value (which an Oracle DATE is) to a date. That said, both Oracle and MySQL support the EXTRACT() function, so you should be able to do the following to get today's records:
SELECT * FROM mytable
WHERE EXTRACT(YEAR FROM mydate) = EXTRACT(YEAR FROM CURRENT_DATE)
AND EXTRACT(MONTH FROM mydate) = EXTRACT(MONTH FROM CURRENT_DATE)
AND EXTRACT(DAY FROM mydate) = EXTRACT(DAY FROM CURRENT_DATE);
Definitely unwieldy, especially if one has more than one date to consider (which I assume you do since you're using the IN operator), but should work on both platforms. See SQL Fiddle Demo here (MySQL) and here (Oracle).
MySQL and Oracle use different syntax for converting dates to strings - you'll have to use different queries.
I was going through some queries and found some where clauses the following way
convert(varchar, datefield, 101) between convert(varchar, #startdate, 101) and convert(varchar, #enddate, 101)
My question is this the right approach to check a datetime value between date ranges? Will this approach fetch me the wrong value? If so, please provide some explanations.
Don't convert your Date to a varchar and compare because string comparisson is not fast. It is much faster if you use >= and < to filter your date column.
DO NOT use the following, as it could return some records from #enddate if their times are 00:00:00.000.
datefield between #startdate and #enddate
Fastest way to convert datetime to date.
On SQL Server 2008 and higher, you should convert to date:
SELECT CONVERT(date, getdate())
No varchar<->datetime conversions required
No need to think about locale