Filtering data using string YYYYMMDD date in query - sql

Below WHERE clause is used to filter the data using BETWEEN keyword.
WHERE dateCol BETWEEN date1 AND date2
But the table contains column of data type CHARACTER which stores date like 20130407 (YYYYMMDD). I can't change the data type of the column because of privilege and it is already present and in use. Now I need to filter the data between two dates like between 20130701 and 20130801. I can't use BETWEEN because that column is not of DATE type. Might be usage of IN criteria is alternative, but it makes query very lengthy and not dynamic. Can someone please suggest how to do filter this between two dates?

Just use the same format for the comparisons:
WHERE dateCol BETWEEN '20130701' and '20130801'
I do suspect, however, that you really mean:
WHERE dateCol >= '20130701' and
dateCol < '20130801'
To get everything in July without the first of August.

Related

count records in specific date format with SQL

i'm trying to define a rule that count rows where date format is dd/mm/yyyy and greater than a specific date in sql but i couldn't find the right function to use.
The second part is working : SELECT COUNT (*) FROM CUSTOMER WHERE DAT_0 >= '01/01/1995' but how could i specify the format too
In Standard SQL, the WHERE clause would look like:
WHERE DAT_0 >= DATE '1995-01-01'
Many databases would simply accept this without the DATE as well:
WHERE DAT_0 >= '1995-01-01'
In your comment DATE_0 is a date column
Therefore we can change it to any format as below using to_char then to_date
to_date(to_char(DATE_0,'yyyymmdd'),'yyyy-mm-dd')
When comparing you can try this, converting the date to_number, i find it very easy
to_number(to_char(DATE_0,'yyyymmdd')) > 19950101
if datatype of "DAT_0" is date or datetime or timestamp , they are recorded as a specific format specified by default datetime setting in your database engine , therefore you can't have multiple format inside the column , so there is no concern there

use only the year part of a DateTime in where condition (SQL)

I have a column with a DateTime type i want to query the table based on a condition that this data is in a certain year.
I tried this but didnt work
WHERE YEAR(InvoiceDate) = 2013; but didnt work
In general, you don't want to use functions on columns. This impedes the optimizer. This is better written as:
where invoicedate >= date '2013-01-01' and
invoicedate < date '2014-01-01'
Note: date is a SQL standard construct for a date literal. Some databases do not use this. Most databases support YYYY-MM-DD as a date literal.
Use year(invoice date ) = year (date('01-01-2013')) so that the data type on both the sides are same and the equality operator would be able to compare .
Some database though allow the query to compare the way you have written Db2 is one such db

Comparing two dates in Oracle after using TO_DATE function

I have the following Oracle Query that is converting todays date and the date field from the table into the same format. However, when trying to compare the two they aren't coming up as equal.
CAST(dstamp As Date), TO_DATE(CURRENT_DATE,'dd-MON-YY HH24.MI.SS','NLS_DATE_LANGUAGE = American')
The cast is used on the field in my table, both these return.
However, when adding the following where statement no rows are returned. I can't work out why these wouldn't be classed as equal?
WHERE CAST(dstamp As Date) = TO_DATE(CURRENT_DATE,'dd-MON-YY HH24.MI.SS','NLS_DATE_LANGUAGE = American');
Any help appreciated.
If you are trying to check if dstamp belongs to the current day, I would suggest:
where dstamp >= trunc(sysdate) and dstamp < trunc(sysdate) + 1
Athough a bit more verbose, this will be more efficient than applying a date function on the column being compared. Using a function on a column in a predicate makes the query non-SARGable, ie it cannot take advantage of an existing index.
The date is ALREADY a date. You don't need to convert it. You may need to remove the time component. Does this do what you want?
WHERE TRUNC(dstamp) = TRUNC(sysdate)

Find record between two dates

When I write below query it gives record .
SELECT [srno],[order_no],[order_date],[supplier_name],[item_code],[item_name],[quntity]
FROM [first].[dbo].[Purchase_Order]
WHERE order_date BETWEEN '22/04/2015' AND '4/05/2015'
In this query if I don't add 0 in '4/05/2015' it returns record.
But when I add 0 to the date i.e. '04/05/2015' it doesn't give any records.
SELECT [srno],[order_no],[order_date],[supplier_name],[item_code],[item_name],[quntity]
FROM [first].[dbo].[Purchase_Order]
WHERE order_date BETWEEN '22/04/2015' AND '04/05/2015'
The reason it's not working because SQL is trying to do a string comparison because both your types are string types, But what you really want to do a date comparison.
You should do something like this. Since you only need date part you can strip off the time and use style 103 for your format dd/mm/yyyy.
WHERE CONVERT(DATETIME,LEFT(order_date,10),103)
BETWEEN CONVERT(DATETIME,'20150422') AND CONVERT(DATETIME,'20150504')
Alternately you can use this as well if your order_date has dates like this 5/4/2015 03:20:24PM
WHERE CONVERT(DATETIME,LEFT(order_Date,CHARINDEX(' ', order_Date) - 1),103)
BETWEEN CONVERT(DATETIME,'20150422') AND CONVERT(DATETIME,'20150504')
A long term solution is to change your column order_date to DATE/DATETIME
It Better to Cast it to date rather than depend on IMPLICIT conversion
SELECT [srno],[order_no],[order_date],[supplier_name],[item_code],
[item_name],[quntity] FROM [first].[dbo].[Purchase_Order] where
convert(date,order_date,105) BETWEEN cast('22/04/2015' as Date) AND cast('04/05/2015' as date)

SQL: Counting dates formatted as varchar

The task:
I need to count every record in a table that was added after 07/01/2011.
Info:
There is a column in the table "Date_added" which is formatted as varchar and contains dates in format mm/dd/yyyy.
What I've tried:
I tried the following query:
SELECT count(date_added) FROM Abatements_Doc WHERE date_added >= '07/01/2011'
Unfortunately it counted any date where the month was greater than 07 regardless of the year or day. I'm at a loss as to how I should format my query to get it to read the dates properly instead of by ascii sort. In my research I discovered the CAST and CONVERT options, but am not sure how to use them, or which one I should use if any. I'm extremely new to SQL, and want to make sure I don't mess up the data in the table. I just need to get the count as specified above without altering the data in the table in any way. Any help in this regard will be very much appreciated. Thanks in advance.
I didn't test. But try convert statement.
SELECT count(date_added)
FROM Abatements_Doc
WHERE convert(datetime,date_added,1) >= convert(datetime,'07/01/2011',1)
(1) don't use mm/dd/yyyy as your date format. Use a safe and unambiguous format; the only one I trust for DATETIME/SMALLDATETIME is YYYYMMDD.
(2) change the data type in your table to DATETIME/SMALLDATETIME/DATE depending on accuracy required. Then `WHERE date_added >= '20110701' will work just fine (and will use an index on that column, if one exists).
(3) if you can't change the data type, then the following will work just fine as well (though no index will be used):
WHERE CONVERT(DATETIME, date_added, 101) >= '20110701'
Varchar can't be compared, try changing your column to date type.
Convert both date_added column values and your target date to DATETIME and compare; as in:
SELECT COUNT(1)
FROM Abatements_Doc
WHERE (CONVERT(datetime, date_added, 101)) >= CONVERT('07/01/2011', date_added, 101))