I have one field startTime in my database and the value is '7:00:00 PM', I want to add 20 minutes in this time and store this in a new field as endtime. I want endtime in same format as is startTime that is '07:20:00 PM', I used ADDTIME, and many more function to do this, but unable to get the format of AM/PM, I used
DATE_ADD('NEXT_CONTACT_TIME', INTERVAL 20 MINUTE), DATE_FORMAT(ADDTIME(NEXT_CONTACT_TIME,'00:20:00'),'%h:%i:%s %p') as endTime
But, I'm not able to get AM/Pm
Use This
DECLARE #StartTime TIME(0) = '07:00:00 AM'; --Time
DECLARE #MinutesToAdd INT = 20; --Added 20 Minutes
SELECT SUBSTRING((CONVERT(VARCHAR(8), DATEADD(MINUTE, #MinutesToAdd, #StartTime), 109)),0,8) + ' ' + RIGHT(CONVERT(VARCHAR(30), DATEADD(MINUTE, #MinutesToAdd, #StartTime), 9), 2) as Time
As per my knowledge mysql default time format is 24HR that means there is no am/pm you should manage the time when fetching or after fetching the data.
A date and time combination. The supported range is '1000-01-01
00:00:00' to '9999-12-31 23:59:59'. MySQL displays DATETIME values in
'YYYY-MM-DD HH:MM:SS' format, but permits assignment of values to
DATETIME columns using either strings or numbers.
Refer this
Related
I'm attempting to build a "last 30 days" dynamic SQL date filter for a user application. The date column is a unix epoch millisecond timestamp.
Previous iterations of the tool allowed the user to choose a date range, I'm now just changing it to choose the last 30.
The data is stored in Redshift, which does not support from_unixtime.
I have two challenges:
The data is stored in UTC and needs to be filtered with dates in EST (UTC -5).
"Choosing the last 30 days" means cutting off at midnight yesterday, and taking yesterday minus 29.
Previously, my code looked like this:
"datecol" >= DATEDIFF(millisecs, '1969-12-31 19:00:00', ''start date' 00:00:00')
AND "datecol" <= DATEDIFF(millisecs, '1969-12-31 19:00:00', ''end date' 23:59:59')
The application would update the start and end dates as described by the user. This code is adjusted for the time difference.
How can I use GETDATE() and DATEADD() on a Unix timestamp, using the constraints of Redshift SQL?
Thanks.
extract('epoch' from ts) gives you unix timestamps and you just add 5 hours to query UTC as if it is EST (if EST is UTC-5 then UTC is EST+5)
between extract('epoch' from ('<<date1>>' + interval '5 hour'))
and extract('epoch' from ('<<date2>>' + interval '29 hour' - interval '1 second'))
also, from_unixtime can be expressed in Redshift as the following:
select timestamp 'epoch' + unix_ts_column * interval '1 second'
a bit ugly but works just like that
I think you want to write a User Defined Function (UDF) for your Redshift database using Python and the python standard datetime module. See http://docs.aws.amazon.com/redshift/latest/dg/user-defined-functions.html
Follow the section titled Creating a Scalar Python UDF.
I don't quite understand your query or the context, but I think you can figure out how to get what you want using UDLs.
For example to get the milliseconds between two datetimes (one in UTC, one in EST) you would write is like the following (not tested):
CREATE FUNCTION datediff_py(a datetime, b datetime)
returns float
stable
as $$
#python code goes here between the $$
from datetime import datetime
FMT = '%Y-%m-%d %H:%M:%S' #dates like '2016-12-24 23:59:59'
tdelta = datetime.strptime(a + " UTC", FMT + " %Z") - datetime.strptime(b + " EST", FMT + " %Z")
return tdelta.total_seconds()*1000
$$ language plpythonu;
This computes the milliseconds between an SQL datetime a that is in UTC and b that is in EST. The %Z format is used for timezones. A usage would be:
"datecol" >= datediff_py('1969-12-31 19:00:00', user_date)
Of course Unix epoch is actually '1970-01-01 00:00:00'.
There are plenty of other date functions in the Python standard library datetime module, so you can write other UDLs if you need things like GETDATE() or DATEADD(), for exampling using timedelta
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')
I am looking for a solution to find with mySQL every result that was created since a(or many) entire week(s) (7 days).
I tried this but it seems sometimes the result is false because is there more than one result per week.
SELECT *
FROM `datatable`
WHERE MOD(TIMESTAMPDIFF(DAY,UNIX_TIMESTAMP(created),NOW()),7)=0;
created is a timestamp.
Thanks for any response!
Try this:
SELECT
*
FROM
`datatable`
WHERE 1
AND `created` >= DATE_FORMAT(NOW() - INTERVAL 7 DAY,'%Y-%m-%d 00:00:00')
AND `created` <= DATE_FORMAT(NOW() - INTERVAL 7 DAY,'%Y-%m-%d 23:59:59')
UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date)
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. The server interprets date as a value in the current time
zone and converts it to an internal value in UTC. Clients can set their
time zone as described in
http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html.
So, do not use UNIX_TIMESTAMP() function, but simple DATE().
As far as I can understand the question, you want the records that are are newer than a certainDate, AND not older than 7 days, or 14 days,..., 140 days (whichever of them is closest to the certainDate) from current date.
IF I understand you correctly, following might work for you with certainDate = '2013-04-01 00:00:00'
SELECT *
FROM `datatable`
WHERE `created` >= ADDDATE(
'2013-04-01 00:00:00',
INTERVAL MOD(ABS(TIMESTAMPDIFF(DAY, NOW(), '2013-04-01 00:00:00')),7) DAY
)
This query will fetch all results having created in last 14 days.
UPDATE blogs SET start_date = '11/27/2012 00:00',end_date = '11/27/2012 00:00' WHERE id='9'
This query won't store start_date or end_date values for blog id 9 unless I set them to varchar type.
Tried with timestamp and date time but the query allways will return that 0 rows where affected
the thing is I need to be able to check for rows in a interval of time, and with varchar it makes very complicated.
What am i missing?
As stated in Date and Time Literals:
MySQL recognizes DATETIME and TIMESTAMP values in these formats:
As a string in either 'YYYY-MM-DD HH:MM:SS' or 'YY-MM-DD HH:MM:SS' format. A “relaxed” syntax is permitted here, too: Any punctuation character may be used as the delimiter between date parts or time parts. For example, '2012-12-31 11:30:45', '2012^12^31 11+30+45', '2012/12/31 11*30*45', and '2012#12#31 11^30^45' are equivalent.
As a string with no delimiters in either 'YYYYMMDDHHMMSS' or 'YYMMDDHHMMSS' format, provided that the string makes sense as a date. For example, '20070523091528' and '070523091528' are interpreted as '2007-05-23 09:15:28', but '071122129015' is illegal (it has a nonsensical minute part) and becomes '0000-00-00 00:00:00'.
As a number in either YYYYMMDDHHMMSS or YYMMDDHHMMSS format, provided that the number makes sense as a date. For example, 19830905132800 and 830905132800 are interpreted as '1983-09-05 13:28:00'.
Therefore, the strings '11/27/2012 00:00' and '11/27/2012 00:00' are not valid MySQL datetime literals. You have two options (in some vague order of preference, without any further information of your requirements):
Provide your literals in a recognised format:
UPDATE blogs SET
start_date = '2012-11-27 00:00:00',
end_date = '2012-11-27 00:00:00'
WHERE id = 9
Use MySQL's STR_TO_DATE() function to convert the string:
UPDATE blogs SET
start_date = STR_TO_DATE('11/27/2012 00:00', '%m/%d/%Y %H:%i'),
end_date = STR_TO_DATE('11/27/2012 00:00', '%m/%d/%Y %H:%i')
WHERE id = 9
Try this:
UPDATE blogs
SET start_date = STR_TO_DATE('11/27/2012 00:00', '%m/%d/%Y %h:%i:%s') --cast string to date in correct date format
,end_date = STR_TO_DATE('11/27/2012 00:00', '%m/%d/%Y %h:%i:%s')--cast string to date in correct date format
WHERE id = 9 --removed quotes as this field's probably numeric
If using a timestamp column instead of datetime I think you need to do something like this:
UPDATE blogs
SET start_date = UNIX_TIMESTAMP(STR_TO_DATE('11/27/2012 00:00', '%m/%d/%Y %h:%i:%s'))
,end_date = UNIX_TIMESTAMP(STR_TO_DATE('11/27/2012 00:00', '%m/%d/%Y %h:%i:%s'))
WHERE id = 9
I'm trying to create a query using mysql.
select ID,NCOde,ifnull(EndTime,now())-starttime from xxx where starttime between
'2012-05-09 00:00:00' and '2012-05-09 23:59:59'
the problem is ifnull(EndTime,now()) return datetime in 24 hours format, while the starttime using am/pm format.
I've tried using DATE_FORMAT(starttime, '%m-%d-%Y %T'), but it seems that the operation changed the datetime type to other type.
Any advice?
Use STR_TO_DATE() to convert your starttime string to a MySQL DATETIME:
STR_TO_DATE(starttime, '%m-%d-%Y %r')
and then use TIMEDIFF() to subtract two times:
select ID,NCOde,
TIMEDIFF(ifnull(EndTime,now()), STR_TO_DATE(starttime, '%m-%d-%Y %r'))
from xxx
where STR_TO_DATE(starttime,'%m-%d-%Y %r')
between '2012-05-09 00:00:00' and '2012-05-09 23:59:59'
You should probably consider changing the data type of the starttime column to DATETIME or TIMESTAMP. Note also that this assumes EndTime is already of such a data type, or else you will also have to perform a similar conversion with it too.
Use the DATE_SUB() function.
Plus what eggyal said.