T-SQL DateDiff between the times but 1 datecolumn is 1900 - sql-server-2008

I have a column that pulls in two dates, 1 of the dates has the correct date and time so I can easily compare the number of minutes between this date and GetDate() but some values have the date 01/01/1900 and then the time i need to use.
How can I do datediff but ignore the date and only use the times?
So
My date | 01/01/1900 14:25:00
GetDate() | 06/02/2014 14:26:00
Would give me 1 minute

Convert your datetime to the time data type and then everything works as expected.
Ex:
Declare #d1 DateTime,
#d2 DateTime
Select #d1 = '01/01/1900 14:25:00',
#d2 = '06/02/2014 14:26:00'
Select DATEDIFF(minute, Convert(Time, #d1), Convert(Time, #d2))
The time data type was added in SQL2008 (which you have tagged in your question).

Related

MySQL searching timestamp columns by date only

I am building out a query to search a table by a timestamp column value. An example of the format I am passing to the api is 2018-10-10. The user has the ability to select a date range. Often times the date range start date is 2018-10-10 and end date is the same day, 2018-10-10. The below doesn't seem to do the trick. What is the simplest way to accomplish this without having to specify the time? Obviously, I'd like to query for the entire day of 2018-10-10 from start to end of day.
SELECT
count(*)
FROM
contact
WHERE
created_at >= '2018-10-10'
AND created_at <= '2018-10-10';
The problem here is that Timestamp datatype will have HH:MM:SS (time) values also. While comparing a datetime with date, MySQL would automatically assume 00:00:00 as HH:MM:SS for the date value.
So, 2018-10-10 12:23:22 will not match the following condition: created_at <= '2018-10-10'; since it would be treated as: 2018-10-10 12:23:22 <= '2018-10-10 00:00:00, which is false
To handle this, you can add one day to the date (date_to in the filter), and use < operator for range checking.
SELECT
count(*)
FROM
contact
WHERE
created_at >= '2018-10-10'
AND created_at < ('2018-10-10' + INTERVAL 1 DAY);

Create datetime with specific time mySql

I'm looking to create a date time field in a MySQL script that has a specific date and time.
I've tried using
CONCAT(DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 9 DAY) ,'%Y%m%d'), ' 13:00:00')
but it doesn't insert correctly.
How can I achieve this so that it will insert a date time with the time as above?
It inserts the record as 0000-00-00 00:00:00 with the above
mysql accepts datetime values in yyyy-mm-dd hh:mm:ss format. In your formula you do not separate the year, month, day values, hance the result is not in the date format mysql expects the dates in. Change it to:
CONCAT(DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 9 DAY) ,'%Y-%m-%d'), ' 13:00:00')
But I do not really understand why you need to do the formatting, just use CURDATE() function instead of the NOW():
CONCAT(DATE_SUB(CURDATE(), INTERVAL 9 DAY), ' 13:00:00')

MySQL substract two datetimes and returns in nanoseconds

I have 2 datetime columns in a MySQL table.
I want to subtract it and return the results and a nanoseconds precision
This is my code so far.
select end,start,end-start from job where id=1;
I have the results like this
'2014-04-02 12:30:00', '2014-04-02 10:30:00', 20000.000000
Just for curiosity how MySQL subtract directly, how 20000.000000 came from.
I am not good handling dates. Which is the best approach?
unix_timestamp: If you want to convert a UNIX TIMESTAMP into seconds since '1970-01-01'
If called with no argument, returns a Unix timestamp (seconds since
'1970-01-01 00:00:00' UTC) as an unsigned integer. If UNIX_TIMESTAMP()
is called with a date argument, it returns the value of the argument
as seconds since '1970-01-01 00:00:00' UTC. date may be a DATE string,
a DATETIME string, a TIMESTAMP, or a number in the format YYMMDD or
YYYYMMDD.
Consider this :
SELECT unix_timestamp(now()) - unix_timestamp('2007-03-19 09:50:00')
You want this:
SELECT unix_timestamp(column1) - unix_timestamp(column2) from tableName
See it in action
SELECT start_time,
end_time,
( Unix_timestamp(start_time) - Unix_timestamp(end_time) ) * 1000000000 AS
NanoSeconds
FROM job
Output:
start_time | end_time | NanoSeconds
April, 02 2014 12:30:00 April, 02 2014 10:30:00 7200000000000
As we can format this datetime column value also with DATE_FORMAT(). TO_SECONDS() is available beginning with MySQL 5.5.0.
MYSQL - datetime to seconds also gives some lightness on another solution of the output you want.

T-SQL Updated date field not the same as date format in select statement

In a SQL Server 2008 database I have a Fiscal Year table where the end_date for each fiscal period is set up wrong. The period start_date is the first day of a calendar month at midnight in the format smalldatetime (2015-01-01 00:00:00). The period end_date is supposed to be the last second of the last day of the start_date's month (2015-01-31 23:59:59). The data type for both fields is smalldatetime.
The following gives me the desired date and time that I would like to put in the end_date field:
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,start_date)+1,0))
But it adds on milliseconds to the end of the time: 2015-01-31 23:59:59.000
When I try converting or casting that as smalldatetime to omit the milliseconds it sets the time to midnight of the last day of the start_date's month: 2015-01-31 00:00:00. This also happens if I just update the end_date field with the code in the select statement above.
How can I update the end_date with the correct format and value (2015-01-31 23:59:59)?
smalldatetime does not store seconds, it's always 00. That's why there's an automatic conversion when you subtract one second.
If you want to store seconds, you have to convert the columns to a different data type.
I didn't realize that smalldatetime doesn't store seconds. KekuSemau's answer wasn't quite the right answer, but it did make me realize I wasn't trying to do the right thing, given the datatype of the column I had to update (No, I couldn't change the datatype of the column).
All I had to do was adjust the query to subtract a minute versus a second, which the smalldatetime field will work with:
cast(DATEADD(MINUTE,-1,DATEADD(mm, DATEDIFF(m,0,start_date)+1,0)) as smalldatetime)
So I ended up updating the end_date column with 2015-01-31 23:50:00, as required.

How to compare dates which is stored as String(varchar) in database?

I have a database(table), in which 2 fields are:
fromdate varchar(20)
todate varchar(20)
Dates are stored in this fashion:
YYYY-MM-DD HH:mm:ss
For ex: '2014-10-30 10:10:10' in database.
Now I want to compare two dates and fetch records from database by using query, 2014-09-10 10:10:10(fromdate) to 2014-10-10 10:10:10(todate)
How to fetch all accurate records.. Is there any kind of solution..
Thanks.
Just compare the string without extra overhead.
This format "YYYY-MM-DD HH:mm:ss" shares chronological and literal alphabetical order
SELECT * FROM someTable
WHERE fromdate >= '2014-09-10 10:10:10' AND todate <= '2014-10-10 10:10:10'
Also, I would create an index on those columns.
i have a database(table), in which 2 fields are: fromdate varchar(20)
todate varchar(20)
It is a design flaw. Date should always be a DATE data type and never be string.
dates are stored in this fashion YYYY-MM-DD HH:mm:ss
DATE is never stored in any format. it is for us, human beings to understand.
Oracle stores DATE in total of 7 bytes. Each byte in it stores values for an element of the DATE as follows:
Byte Description
---- -------------------------------------------------
1 Century value but before storing it add 100 to it
2 Year and 100 is added to it before storing
3 Month
4 Day of the month
5 Hours but add 1 before storing it
6 Minutes but add 1 before storing it
7 Seconds but add 1 before storing it
for eg :"2014-10-30 10:10:10" in database.
Now i want to compare two dates and fetch records from database by
using query, 2014-09-10 10:10:10(fromdate) to 2014-10-10
10:10:10(todate)
Just use to_date('2014-10-30 10:10:10', 'YYYY-MM-DD HH24:MI:SS')
NOTE This is for Oracle database. I see you have tagged SQL Server too. I don't understand why did you do that.
Use STR_TO_DATE()
select * from your_table
where str_to_date(fromdate, '%Y-%m-%d %H:%i:%s') >= '2014-09-10 10:10:10'
and str_to_date(todate, '%Y-%m-%d %H:%i:%s') <= '2014-10-10 10:10:10'
First, you can use convert function:
SELECT CONVERT(Datetime, '2014-10-30 18:00:00', 120)
Second, if you can't change the existing columns and their type, it does not mean that you can't add additional column with correct date type that duplicates the meaning of "wrong" column. This would both save your legacy code and help you in new development, as all the operations with convertation in queries are very expensive in terms of performance.