How do you update an existing date to a random date in a range? - datetime

In one of my tables I have datetime field in which the data in the table column is populated with something like "2016-01-07 01:33:00".
What I want to do is change ONLY the date to a random date within a range (ie: 2016-02-01 thru 2016-02-28) without changing the time. The end result might be "2016-02-13 01:33:00".
What mysql command string would accomplish this task?

Something like
UPDATE someTable SET someDate = DATE_ADD(
someDate,
INTERVAL
DATEDIFF(rangeStart, someDate) +
ROUND(RAND()*DATEDIFF(rangeEnd, rangeStart))
DAY
);
where someTable.someDate is your existing data, and rangeStart and rangeEnd are the boundaries of your target date range.
Here you take the initial date, add enough days to it to reach the range start, and then further add a random number of days no greater than the number of days in your target range.

In MsSQL it could be:
select dateadd(day,cast((RAND() * 30) as int),getdate())
Substitute getdate() with your input date.
(RAND() * 30) is used to randomly generate a number of days up to 30.

Related

Negative dates in sqllite database

I am working locally with an sqllite DB. I have imported some records from teradata where there was a date field in the format of 'YYYY-MM-DD'. When i imported the records the date switched from a date to a number. I know this is a feature of sqllite and that one can access it via date(sqllite_date) when selecting it in a where clause.
My problem is that the dates now appear to be a bit odd. For example the year appears to be negative.
Is there anyway to recover this to the correct format?
Below is an example of converting a number in the database into a date
SELECT date(18386)
# -4662-03-28
SELECT datetime('now')
# 2021-02-11 10:41:52
SELECT date(sqllite_date) FROM mydb
# Returns -4662-03-28
# Should return 2020-05-04
I am very new to this area so apologies if this is a basic question. Thank you very much for your time
In SQLite you can store dates as TEXT, REAL or INTEGER.
It seems that you stored the dates in a column with INTEGER or REAL affinity.
In this case, if you use the function date(), it considers a value like 18386 as a Julian day, meaning the number of days since noon in Greenwich on November 24, 4714 B.C.
This is why date(18386) returns 4662-03-28B.C.
But I suspect that the date values that you have are the number of days since '1970-01-01'.
In this case, 18386 days after '1970-01-01' is '2020-05-04'.
So you can get the dates in the format YYYY-MM-DD if you add the value of your column as days to '1970-01-01':
SELECT date('1970-01-01', datecolumn || ' day') FROM tablename
Or by transforming your date values to seconds and treat them as UNIX time (the number of seconds since '1970-01-01 00:00:00 UTC'):
SELECT date(datecolumn * 24 * 3600, 'unixepoch') FROM tablename
Replace datecolumn with the name of your column.

How get values from database scadalts from last day

I need to get data from DB scadalts from last day.
I have data in table pointValues where is column pointValue and ts but is not timestamp.
Column ts is type BIGINT(20)
Checking ts is unixtime
SELECT
pointValue,
ts,
from_unixtime(ts),
YEAR(from_unixtime(ts)),
MONTH(from_unixtime(ts)),
DAY(from_unixtime(ts))
FROM
pointValues;
The result null is wrong is not unixtime.
I don't know how to create condition where because - I don't know how to interpret value in column ts.
Column ts should be interpreted with greater accuracy.
eg:
SELECT
pointValue,
ts,
from_unixtime(ts/1000),
YEAR(from_unixtime(ts/1000)),
MONTH(from_unixtime(ts/1000)),
DAY(from_unixtime(ts/1000))
FROM
pointValues;
And we may get values from last day eg:
SELECT
pointValue,
ts,
YEAR(from_unixtime(ts/1000)),
MONTH(from_unixtime(ts/1000)),
DAY(from_unixtime(ts/1000))
FROM
pointValues
WHERE
YEAR(from_unixtime(ts/1000)) = YEAR(NOW() - INTERVAL 1 day) and
MONTH(from_unixtime(ts/1000)) = MONTH(NOW() - INTERVAL 1 day) and
DAY(from_unixtime(ts/1000)) = DAY(NOW() - INTERVAL 1 day)
Thanks
Maybe it will be useful also

Apache Drill: Group by week

I tried to group my daily data by week (given a reference date) to generate a smaller panel data set.
I used postgres before and there it was quite easy:
CREATE TABLE videos_weekly AS SELECT channel_id,
CEIL(DATE_PART('day', observation_date - '2016-02-10')/7) AS week
FROM videos GROUP BY channel_id, week;
But it seems like it is not possible to subtract a timestamp with a date string in Drill. I found the AGE function, which returns an interval between two dates, but how to convert this into an integer (number of days or weeks)?
DATE_SUB may help you here. Following is an example:
SELECT extract(day from date_sub('2016-11-13', cast('2015-01-01' as timestamp)))/7 FROM (VALUES(1));
This will return number of weeks between 2015-01-01 and 2016-11-13.
Click here for documentation

Get RowCount With Date Comparasion in SSRS

I am new to SSRS.
I have a dataset, my dataset brings data from a stored procedure.
one of the parameters of my sp is StartDate and another one is EndDate. Their type is datetime
And the table has a dateTime Column called Date.
I have two gauges and I wanna bind integer values to my gauges.
First one is the count of rows where Date < DateAdd(DateInterval.Hour,24,StartDate)
and te second is count of rows where Date > DateAdd(DateInterval.Hour,24,StartDate)
How will I write the exact script. Whatever I wrote is not working.
I appreciate any help, thanks.
You need to set the gauge Pointer value as something like:
=Sum(IIf(DateDiff(DateInterval.Day, Parameters!StartDate.Value, Fields!Date.Value) >= 1
, 1
, 0))
This is counting rows where the time difference is less than a day compared to the parameter StartDate. Just change it slightly to get those where the difference is at least a day:
=Sum(IIf(DateDiff(DateInterval.Day, Parameters!StartDate.Value, Fields!Date.Value) >= 1
, 0
, 1))
Worked fine for me in a quick test:

How to add/subtract date/time components using a calculated interval?

I'd like to get this to work in Teradata:
Updated SQL for better example
select
case
when
current_date between
cast('03-10-2013' as date format 'mm-dd-yyyy') and
cast('11-03-2013' as date format 'mm-dd-yyyy')
then 4
else 5
end Offset,
(current_timestamp + interval Offset hour) GMT
However, I get an error of Expected something like a string or a Unicode character blah blah. It seems that you have to hardcode the interval like this:
select current_timestamp + interval '4' day
Yes, I know I hardcoded it in my first example, but that was only to demonstrate a calculated result.
If you must know, I am having to convert all dates and times in a few tables to GMT, but I have to account for daylight savings time. I am in Eastern, so I need to add 4 hours if the date is within the DST timeframe and add 5 hours otherwise.
I know I can just create separate update statements for each period and just change the value from a 4 to a 5 accordingly, but I want my query to be dynamic and smart.
Here's the solution:
select
case
when
current_date between
cast('03-10-2013' as date format 'mm-dd-yyyy') and
cast('11-03-2013' as date format 'mm-dd-yyyy')
then 4
else 5
end Offset,
(current_timestamp + cast(Offset as interval hour)) GMT
You have to actually cast the case statement's return value as an interval. I didn't even know interval types existed in Teradata. Thanks to this page for helping me along:
http://www.teradataforum.com/l081007a.htm
If I understand correctly, you want to multiply the interval by some number. Believe it or not, that's literally all you need to do:
select current_timestamp as right_now
, right_now + (interval '1' day) as same_time_tomorrow
, right_now + (2 * (interval '1' day)) as same_time_next_day
Intervals have always challenged me for some reason; I don't use them very often. But I've had this little example in my Teradata "cheat sheet" for quite a while.
Two remarks:
You could return an INTERVAL instead of an INT
The recommended way to write a date literal in Teradata is DATE 'YYYY-MM-DD' instead of CAST/FORMAT
select
case
when current_date between DATE '2013-03-10' and DATE '2013-11-03'
then interval '4' hour
else interval '5'hour
end AS Offset,
current_timestamp + Offset AS GMT

Resources