Query a date from a concatenated attribute in SQL - mysql

Okay SQL gurus.....I need some help. I have an attribute ReportBatchID that is a concatenated value of the date and some other numbers. For example, all of the ReportBatchID values are in the format "201105115485452652". I need to SELECT LEFT off of the first 8 digits of that value to get the date and then select all of those dates that are more than 90 days old. I am able to use
select Left (ReportBatchID, 8) from [Table]
to pull it as a date, but when I use
where ReportBatchID < CONVERT (VARCHAR(10), DATEADD(DAY, -90, GETDATE()),121)
following my select I get the error "Error converting data type varchar to numeric." Anybody know how to do this?

When You are using Dates in where clause Always use Proper casting on both sides
where CAST(LEFT(ReportBatchID ,8) as DATE) <
CAST(DATEADD(DAY, -90, GETDATE()) as DATE)
select CAST( DATEADD(DAY, -90, GETDATE()) as DATE)
select CAST(LEFT(201105115485452652,8) as DATE)
OUTPUT
2014-08-10
2011-05-11

Related

Using MySQL how do I select data between two dates if the dates are in separate columns

In my database, I have a column with a check-in date and a column with a check-out date. I need to select every row that has a check-in date <= 7/30/2017 and a check-out date that is >= 7/30/2017.
This is the code I have now:
SELECT *
FROM `v_reservation_records`
WHERE cast(checkin as date) <= '7/30/2017'
AND cast(checkout as date) >= '7/30/2017'
Here is an example date from the DB:
2018-09-18
When I run this query, I do not get any results, but I know that I have a check-in date equal to 7/30/2017. What am I missing? Or is there an easier way to accomplish my goal?
Assuming that you are casting valid values for date
You should convert also the literal the date properly
SELECT *
FROM `v_reservation_records`
WHERE cast(checkin as date) <= str_to_date('7/30/2017' , '%d/%m/%Y')
AND cast(checkout as date) >= str_to_date('7/30/2017' , '%d/%m/%Y')
and you can also use between
SELECT *
FROM `v_reservation_records`
WHERE str_to_date('7/30/2017','%d/%m/%Y')
between cast(checkin as date) AND cast(checkout as date)
Try like this
SELECT *
FROM `v_reservation_records`
WHERE DATE_FORMAT(checkin, '%m/%d/%Y') between '7/30/2017'
AND '7/30/2017'

Most efficient way of flooring todays date

This seems like overkill but is the only way I have been able to floor todays datetime to 00:00:00.000 at database level:
select CAST(FLOOR(CAST(CURRENT_TIMESTAMP AS float)) AS DATETIME)
I have tried using:
select FLOOR(getdate())
But get the following message:
Implicit conversion from data type datetime to float is not allowed. Use the CONVERT function to run this query.
Can anyone recommend another way of doing this?
Since you are using SQL Server 2008 you could make use of the date data type.
declare #Today date
set #Today = getdate()
select #Today
Or without the variable.
select cast(getdate() as date)
If you need to have the value as a datetime just cast it back to a datetime.
select cast(cast(getdate() as date) as datetime)
There are a lot of ways of doing this i have seen the floor one before. Here are a few more.
select cast(cast(CURRENT_TIMESTAMP as date) as datetime)
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 0)
SELECT CAST(CAST(CURRENT_TIMESTAMP - 0.50000004 AS int) AS datetime)
I normaly do the Cast to date version.

Calculate difference between dates

The title might be a bit misleading, but what I want is:
SELECT * FROM table ORDER BY pid ASC
And in one of the columns I have a DATE(). I want to compare the current date (not time) and return how many days are left till that date. Let's say the date is 2013-04-20 and today's date is 2013-04-16 I don't want to get any data if it's < current date. If it is I want it returned in days.
I've been looking around here and I've found no way to do it, and I can't for the love of me figure it out.
If you're looking for the difference between two date you can use the GETDATE function in MS SQL
SELECT DATEDIFF(DD, DateOne, DateTwo) FROM TABLE
This will return the difference in number of days between the two dates.
If you only want rows where the date field is less than or equal to today's date you can use:
SELECT DATEDIFF(DD, DateField, GETDATE())
FROM TableName
WHERE DateField <= GETDATE()
If you're using MySQL you can use DATEDIFF()
SELECT
DATEDIFF(NOW(), date_column) AS days_diff
FROM
tablename
Get the difference between two dates (ANSI SQL)
select the_date_column - current_date as days_left
from the_table
where the_date_column - current_date <= 4;
SQLFiddle: http://sqlfiddle.com/#!12/3148d/1

Compare datetime in stored procedure

I have a sql 2008 database and I am creating a stored procedure that shall check if a datetime is more than 3 hours old but I don't know how to do it.
Do you have some way to do it?
the datetime is a field in the table.
BR
Rather than applying DATEDIFF to the column value, which will negate an index, I suggest using a comparison of the column to an expression (which can use an index).
If you want this as a filter:
SELECT columns
FROM dbo.table
WHERE DateTimeColumn < DATEADD(HOUR, -3, CURRENT_TIMESTAMP);
(If you want only the rows that are newer than 3 hours old, change < to > or >=.)
If you want to return all rows with a column showing whether it is more than 3 hours old:
SELECT columns, [3HoursOld] = CASE
WHEN DateTimeColumn < DATEADD(HOUR, -3, CURRENT_TIMESTAMP)
THEN 'Yes, older than 3 hours.'
ELSE 'No, not older than 3 hours.'
END
FROM dbo.table;
Take at look at the DATEDIFF function.
DATEDIFF ( datepart , startdate , enddate )
You would then use with datepart set to hh and the enddate set to the current time. To get the current database time you could use GETDATE(). Compare the result with 3 since it will return the number of hours passed.
#date is the date you want to compare
declare #date datetime
set #date= '2012-02-15 14:20:42.797'
SELECT DateDiff(hh, DATEADD(hh,-3,#date), GETDATE()) --if it's > 3
you better create a Boolean function that does the trick that you can use where ever you like

format date to 105 in SQL Server after casting

I have a problem: I have a datetime and I need the date to specific format
So I just casted datetime to time
SELECT CAST (GETDATE() AS DATE) -- result (2011-06-08)
and for formatting I use convert
SELECT CONVERT(DATE, CAST (GETDATE() AS DATE), 105) --result (2011-06-08)
105 format (dd-mm-yy)
but, the result of both is same,
CONVERT is not working for 105 formatting,
Any ideas?
thanks
To get the results you're looking for you need to convert the DATE to a VARCHAR like this:
SELECT CONVERT(VARCHAR(10),CAST (GETDATE() AS DATE),105)
If you cast to a DATE, you will always get the full DATE.
You can truncate the date by re-casting to the DATE type.
SELECT CAST(CONVERT(VARCHAR(10),CAST (GETDATE() AS DATE),105) as DATE)
You can use as
SELECT Convert(varchar, getdate(), 105)
select convert(varchar(50),date,105) as Date