I want to create this table but I want to save datetime in 13 digit timestamp format e.g 1424109603234
create table data (
datetime timestamp,
id text,
type text,
body text
primary key (id, type, datetime)
)
is the datatype of datetime correct? how should I insert data, in any specific function like toTimestamp(now())??
datatype for datetime is correct.
Values for the timestamp type are encoded as 64-bit signed integers representing a number of milliseconds since the standard base time known as the epoch: January 1 1970 at 00:00:00 GMT. (For more detail refer to https://docs.datastax.com/en/cql/3.3/cql/cql_reference/timestamp_type_r.html
)
Now from your function you need to get the epoch timestamp and then convert it to milliseconds( There are lot of different codes to do that) and then push the converted time stamp (signed long long) to cassandra.
Related
I'm looking to cast/convert a decimal data into date data. I've looked online and am still struggling. Can you help? "value" is a double precision, and here I convert it to varchar. From that point on, I've tried using convert but I'm still unable to get a date value.
Thanks in advance!
select cast(value as varchar(8) )date_value, value,
For example: The value 43099 should read 12/30/2017.
Guessing from your meager example, you might want this:
SELECT date '1899-12-30' + 43099; -- returns date '2017-12-30'
You need to cast your value column to integer for this:
SELECT date '1899-12-30' + value::int;
Meaning, the number might represent the count of days since 1900 (with an off-by-2 error I can't explain).
Adding an integer to a date means adding the number of days.
If your value can have fractional digits, you can compute a timestamp in similar fashion:
SELECT timestamp '1899-12-30' + interval '1 day' * value;
When inserting now() in a table, the written value only contains the date (e.g. 2017-12-20), but not the date and time as specified in the documentation.
See this SQLfiddle.
create table timetest (
id serial primary key,
mydate date
);
insert into timetest (mydate) values (
now());
Is there some specific command that should be passed to either write or retrieve also the time information?
You created mydate date as a date type column. date only represents the date fraction (unsurprisingly).
If you need both date and time use timestamp type instead.
References:
https://www.postgresql.org/docs/current/static/datatype-datetime.html
what will be data type to store date time in cassandra
CREATE TABLE testTable (
dateValue date,
time timestamp
)
n my insert staements would be like this ,
insert into caliper.log_per_day ( timeStampValue,dateValue ) values ('2015-12-30 16:10:31','2015-12-30');
i wanted to store date & time both in one column like this '2015-12-30 16:10:31'.
but if i used timestamp it would be store like this '2015-12-30 04:10:31+0530'
Notes : primary key n other things are skip here... ignore it.
Cqlsh will display timestamps in the following format by default:
yyyy-mm-dd HH:mm:ssZ
The Z in these formats refers to an RFC-822 4-digit time zone,
If no time zone is supplied, the current time zone for the Cassandra
server node will be used.
so if you don't want to store in this way you can store it as varchar.
Can unix timestamps contain the milliseconds value of the time?
If so, using SQL Server 2008 R2:
CREATE TABLE [dbo].[my_table_calls_log]
(
[id] [bigint] IDENTITY(1,1) NOT NULL,
[requestdate] [bigint] NULL,
[partycode] [bigint] NULL,
CONSTRAINT [PK_my_table_calls_log]
PRIMARY KEY CLUSTERED ([id] ASC)
)
The following select gives me the current date and time with milliseconds:
SELECT CONVERT(VARCHAR,SYSDATETIME(),121);
Example:
2015-09-11 13:29:02.8239061
How do I convert that long YYYY-MM-DD HH:MM:SS.MS date/time value into a unix timestamp so that I can insert it using something like:
DECLARE #UnixDate AS bigint;
SET #UnixDate = "the unix timestamp equivalent of SELECT CONVERT(VARCHAR,SYSDATETIME(),121);"
INSERT INTO my_table_calls_log (requestdate,partycode)
VALUES (#UnixDate,123);
Standard unix timestamps are 32-bit signed integers with a precision of 1 second (see here), that said there is certainly nothing invalid about using a 64-bit integer and using a precision of milliseconds if you so choose (so long as you are certain that all code that will use/consume that timestamp value is aware of that constraint). Most standard unix timestamp libraries assume it is a 1 second precision, and if you pass a 64-bit signed integer instead of a 32-bit integer that still holds and the assumption is the user is just planning for a longer life of the timestamp (i.e. to track date/time values past January 19, 2038, which is the date a 32-bit based unix timestamp will stop working at some point from the default epoch due to an overflow).
Unix timestamps also generally assume a base epoch of '1970-01-01 00:00:00', so using that assumption you can convert a standard SQL Server datetime value to a unixtimestamp using something like this:
select datediff(second, '1970-01-01T00:00:00.000', sysdatetime());
If you want that to be millisecond based, it becomes a bit more complex as the datediff function in SQL Server is 32-bit integer based (i.e. an int data type) and the difference between the default epoch and the current date/time in milliseconds exceeds the upper-bound of that type, so we have to get a little creative, here is one option (which I tend to use wrapped in a function):
declare #start datetime2 = '1970-01-01T00:00:00.000',
#end datetime2 = sysdatetime(),
#ms_in_day bigint = 60 * 60 * 24 * 1000;
select (#ms_in_day * datediff(day, #start, #end)) - datediff(millisecond, #end, cast(#end as date));
In each of these examples, if you have a varchar/character based date/time value, simply insert directly into the script in place of the sysdatetime() function I used.
I have created a table with a column date_time type (varchar2 (40) ) but when i try to insert the current system date and time the doesnt work it gives error (too many values). please tell me what's wrong with the insert statement.
create table HR (type varchar2 (20), raised_by number (6), complaint varchar2 (500), date_time varchar2(40))
insert into HR values ('request',6785,'good morning',sysdate,'YYYY/MM/DD:HH:MI:SSAM')
The immediate cause of the error is that you have too many values, as the message says; that is, more elements in your values clause than there are columns. It is better to explicitly list the column names to avoid future problems and confusion, so you're really doing this:
insert into HR (type, raised_by, complaint, date_time)
values ('request',6785,'good morning',sysdate,'YYYY/MM/DD:HH:MI:SSAM')
... sp you have four columns, but five values. You're trying to insert the current date/time as a string so you would need to use the to_char() function:
insert into HR (type, raised_by, complaint, date_time)
values ('request',6785,'good morning',
to_char(sysdate,'YYYY/MM/DD:HH:MI:SSAM'))
But it is bad practice to store a date (or any other structured data, such as a number) as a string. As the documentation notes:
Each value manipulated by Oracle Database has a data type. The data
type of a value associates a fixed set of properties with the value.
These properties cause Oracle to treat values of one data type
differently from values of another. For example, you can add values of
NUMBER data type, but not values of RAW data type.
If you use a string then you can put invalid values in. If you use a proper DATE data type then you cannot accidentally put an invalid or confusing value in. Oracle will also be able to optimise the use of the column, and will be able to compare values safely and efficiently. Although the format you're using is better than some, using string comparison you still can't easily compare two values to see which is earlier, so you can't properly order by the date_time column for example.
Say you inserted two rows with values 2013/11/15:09:00:00AM and 2013/11/15:08:00:00PM - which is earlier? You need to look at the AM/PM marker to realise the first one is earlier; with a string comparison you'd get it wrong because 8 would be sorted before 9. Using HH24 instead of HH and AM avoids that, but would still be less efficient than a true date.
If you need to store a date with a time component you can use the DATE data type, which has precision down to the second; or if you need fractional seconds too then you can use TIMESTAMP. Then your table and insert would be:
create table HR (type varchar2 (20), raised_by number (6),
complaint varchar2 (500), date_time date);
insert into HR (type, raised_by, complaint, date_time)
values ('request',6785,'good morning',sysdate);
You can still get the value in the format you wanted for display purposes as part of a query:
select type, raised_by, complaint,
to_char(date_time, 'YYYY/MM/DD:HH:MI:SSAM') as date_time
from HR
order by date_time;
TYPE RAISED_BY COMPLAINT DATE_TIME
-------------------- ---------- -------------------- ---------------------
request 6785 good morning 2013/11/15:08:44:35AM
Only treat a date as a string for display.
You can use TO_DATE() or TO_TIMESTAMP or To_char() function,
insert into HR values ('request',6785,'good morning',TO_DATE(sysdate, 'yyyy/mm/dd hh24:mi:ss'))
insert into HR values ('request',6785,'good morning',TO_TIMESTAMP(systimestamp, 'yyyy/mm/dd hh24:mi:ss'))
sysdate - It will give date with time.
systimestamp - It will give datetime with milliseconds.
To_date() - Used to convert string to date.
To_char() - Used to convert date to string.
Probably here you have to use To_char() because your table definition have varchar type for date_time column.
Use TIMESTAMP datatype for date_time. And while inserting use the current timestamp.
create table HR (type varchar2(20), raised_by number(6), complaint varchar2(500), date_time timestamp);
insert into HR values ('request',6785,'good morning', systimestamp);
For other options: http://psoug.org/reference/timestamp.html