Use Military-time value as Time in PostgreSQL? - sql

I have created a table
CREATE TABLE mytable
(food VARCHAR(20) references myfood(BIN)
,name NUMERIC(2)
,servingsize VARCHAR(15)
,time TIMESTAMP
,PRIMARY KEY(food, time)
);
and what I want is to insert data in to this table which I use:
INSERT INTO dinnertime (food, name, servingsize, time) VALUES
('earl', 3, 'one cup', '1300'),
('phebeo', 2, 'two cup', '1100'),
('apollo', 1, 'one Cup', '0700'),
('oscar', 4, 'one cup', '2200');
But PostgrSQL does not let me do this. The problem is with the time. What changes do I need to make to my table for it to take this format of time

Use time instead of timestamp (timestamp is date + time, you only have time), then convert to one of the recognized formats for time:
http://www.postgresql.org/docs/8.0/static/datatype-datetime.html

Related

Is there any way to display time in format ot hh:mm

i declare table like this
create table CHUYENBAY(
MACB char(4),
SBDI char(3),
SBDEN char(3),
GIODI time(0),
GIODEN time(0),
primary key (MACB)
)
I insert into table
insert into CHUYENBAY
values ('100', 'SLC', 'BOS', '8:00', '17:50')
go
i check for its display and it was like this
is there any way to transform the hh:mm:ss format into hh:mm only, many thanks
You can do this when you retrieve the data, say by using format():
select format(GIODI, 'HH:mm')
You can also add computed columns to facilitate this for anyone querying the table:
alter table CHUYENBAY add column giodi_hhmm as (format(GIODI, 'HH:mm'));
Instead of format(), you can also use convert(varchar(5), GIODI, 108), so:
alter table CHUYENBAY add column giodi_hhmm as (convert(varchar(5), GIODI, 108));
Since you said format() was not available, you can use convert() instead:
select MACB,
SBDI,
SBDEN,
GIODI = convert(char(5), GIODI , 108),
GIODEN = convert(char(5), GIODEN, 108)
from CHUYENBAY
But be aware that if you don't have format() then your version of SQL is no longer supported. Time for an upgrade :)

Inserting a timestamp into a table in MS SQL Management Studio

I'm trying to insert a timestamp within a data entry and I'm unsure what I'm doing wrong.
INSERT INTO dbo.SALES (
Sales_No
,Customer_ID
,Shop_No
,Staff_No
,DATE
,Sum_total
)
VALUES (
9876
,11223344556
,1000
,9000
,CURRENT_TIMESTAMP
,50900
);
I'm still learning how to do this so any help would be helpful.
i just create one field and insert current timestamp
create table t(date smalldatetime);
insert into t values(CURRENT_TIMESTAMP);
select * from t
http://sqlfiddle.com/#!18/bf41ce/2
date
2018-08-31T06:22:00Z
So i think you need to change your datatype of your table column
Another way if you design your table in default clause no need insertion explicitly
CREATE TABLE test ( aa int,
dd DATETIME DEFAULT GETDATE()
);
insert into test (aa) values(1)
insert into test (aa) values(2)
insert into test (aa) values(3)
aa dd
1 2018-08-31T08:08:14.49Z
2 2018-08-31T08:08:14.49Z
3 2018-08-31T08:08:14.493Z
sqlfiddle.com/#!18/b5fdd/1
You can also use getutcdate() to get the current timestamp if your datatype is datetime

ORA-01858 Error in an Oracle SQL Table

I've been fighting with this problem for a few days.
I created the next table in Oracle's SQL Developer:
CREATE TABLE EMPLEADOSMM (
ID_EMPLEADO VARCHAR2 (10) PRIMARY KEY,
NOMBREEM VARCHAR2 (15) NOT NULL,
AP_PATEM VARCHAR2 (20) NOT NULL,
AP_MATEM VARCHAR2 (20) NOT NULL,
FECHA_NAC DATE NOT NULL,
FECHA_ING DATE NOT NULL,
ID_CARGO VARCHAR2 (10));
Then, I proceeded to add some values to the table, the thing is that it added some like this:
INSERT INTO EMPLEADOSMM VALUES ('100006','OSCAR','MARIN','PEREZ','12-jul-85','15-nov-17','C0002');
But this others send me the 'ORA-01858: a non-numeric character was found where a numeric was expected' error.
INSERT INTO EMPLEADOSMM VALUES ('100004','FABIAN','RODRIGUEZ','VELEZ','31-aug-87','13-jul-17','C0003');
INSERT INTO EMPLEADOSMM VALUES ('100005','LUZ MARIA','TORINO','YAÑEZ','11-dec-90','13-jul-17','C0003');
I tried changing the year format by "yyyy", rewriting all the zeros, but nothings seems to work. Some ideas?
First, I want to emphasize that the code does work. Here is a SQL Fiddle demonstrating the working code.
Second, that means that something about your system causes it to break. Barbaras Ozhan seems to have the right explanation -- internationalization settings recognize some month abbreviations as being the same as English, but not all of them.
You should be writing the insert as:
INSERT INTO EMPLEADOSMM (ID_EMPLEADO, NOMBREEM, AP_PATEM, AP_MATEM, FECHA_NAC, FECHA_ING, ID_CARGO)
VALUES ('100006', 'OSCAR', 'MARIN', 'PEREZ', DATE '1985-07-12', DATE '2017-11-15', 'C0002');
INSERT INTO EMPLEADOSMM (ID_EMPLEADO, NOMBREEM, AP_PATEM, AP_MATEM, FECHA_NAC, FECHA_ING, ID_CARGO)
VALUES ('100004', 'FABIAN', 'RODRIGUEZ', 'VELEZ', DATE '1987-08-31', DATE '2017-07-13', 'C0003');
INSERT INTO EMPLEADOSMM (ID_EMPLEADO, NOMBREEM, AP_PATEM, AP_MATEM, FECHA_NAC, FECHA_ING, ID_CARGO)
VALUES ('100005', 'LUZ MARIA', 'TORINO', 'YAÑEZ', DATE '1990-12-11', DATE '2017-07-13', 'C0003');
Oracle supports the ANSI standard keyword DATE for introducing date constants in the ISO/ANSI standard format, YYYY-MM-DD. I strongly recommend that you use this format in all your code. Use TIMESTAMP when there is a time component.
Including the column names is a best practice.
I would question why the employee id is a string, if you are only going to include numbers in it.

How to insert date values into table

How can I insert into table with different input using / ,with date datatype?
insert into run(id,name,dob)values(&id,'&name',[what should I write here?]);
I'm using oracle 10g.
Since dob is DATE data type, you need to convert the literal to DATE using TO_DATE and the proper format model. The syntax is:
TO_DATE('<date_literal>', '<format_model>')
For example,
SQL> CREATE TABLE t(dob DATE);
Table created.
SQL> INSERT INTO t(dob) VALUES(TO_DATE('17/12/2015', 'DD/MM/YYYY'));
1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT * FROM t;
DOB
----------
17/12/2015
A DATE data type contains both date and time elements. If you are not concerned about the time portion, then you could also use the ANSI Date literal which uses a fixed format 'YYYY-MM-DD' and is NLS independent.
For example,
SQL> INSERT INTO t(dob) VALUES(DATE '2015-12-17');
1 row created.
date must be insert with two apostrophes'
As example if the date is 2018/10/20. It can insert from these query
Query -
insert into run(id,name,dob)values(&id,'&name','2018-10-20')
let suppose we create a table Transactions using SQl server management studio
txn_id int,
txn_type_id varchar(200),
Account_id int,
Amount int,
tDate date
);
with date datatype we can insert values in simple format: 'yyyy-mm-dd'
INSERT INTO transactions (txn_id,txn_type_id,Account_id,Amount,tDate)
VALUES (978, 'DBT', 103, 100, '2004-01-22');
Moreover we can have differet time formats like
DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MI:SS
SMALLDATETIME - format: YYYY-MM-DD HH:MI:SS
insert into run(id,name,dob)values(&id,'&name',[what should I write
here?]);
insert into run(id,name,dob)values(&id,'&name',TO_DATE('&dob','YYYY-MM-DD'));
You can also use the "timestamp" data type where it just needs "dd-mm-yyyy"
Like:
insert into emp values('12-12-2012');
considering there is just one column in the table...
You can adjust the insertion values according to your table.
I simply wrote an embedded SQL program to write a new record with date fields.
It was by far best and shortest without any errors I was able to reach my requirement.
w_dob = %char(%date(*date));
exec sql insert into Tablename (ID_Number ,
AmendmentNo ,
OverrideDate ,
Operator ,
Text_ID ,
Policy_Company,
Policy_Number ,
Override ,
CREATE_USER )
values ( '801010',
1,
:w_dob,
'MYUSER',
' ',
'01',
'6535435023150',
'1',
'myuser');
To insert the current date you can just use this GETDATE() function.
insert into run(id,name,dob) values(&id,'&name',GETDATE());
you can also use CURRENT_TIMESTAMP() function to insert current date and time.

Advice on a complex SQL query for a BIRT dataset

I have the following (simplified) PostgreSQL database table containing info about maintenance done on a certain device:
id bigint NOT NULL,
"time" timestamp(0) with time zone,
action_name text NOT NULL,
action_info text NOT NULL DEFAULT ''::text,
The action_name field can have four values of interest:
MAINTENANCE_START
DEVICE_DEFECT
DEVICE_REPAIRED
MAINTENANCE_STOP
<other (irrelevant) values>
I have to do a BIRT report using the information from this table. I should have an entry in a table each time a MAINTENANCE_STOP action is encountered. If between this MAINTENANCE_STOP action and the its corresponding MAINTENANCE_START action (should be the MAINTENANCE_START action with the max "time" value smaller than that of the MAINTENANCE_STOP action) I encounter a DEVICE_DEFECT or DEVICE_REPAIRED action I should write in a table cell the string "Device not available", else I should write "Device available".
Also, I should compute the duration of the maintenance as the time difference between the MAINTENANCE_STOP action and the MAINTENANCE_START action.
I first attempted to do this in the SQL query, but now I'm not sure it's possible. What approach do you recommend?
My working snippet:
CREATE TABLE "log"
(
id bigint NOT NULL,
time timestamp(0) with time zone,
action_name text NOT NULL,
action_info text NOT NULL DEFAULT ''::text
);
insert into log(id,time,action_name,action_info) values ( 1, '2011-01-01', 'MAINTENANCE_START', 'maintenance01start');
insert into log(id,time,action_name,action_info) values ( 2, '2011-02-01', 'MAINTENANCE_START', 'maintenance02start');
insert into log(id,time,action_name,action_info) values ( 3, '2011-03-01', 'MAINTENANCE_START', 'maintenance03start');
insert into log(id,time,action_name,action_info) values ( 4, '2011-04-01', 'MAINTENANCE_START', 'maintenance04start');
insert into log(id,time,action_name,action_info) values ( 5, '2011-01-10', 'MAINTENANCE_STOP', 'maintenance01stop');
insert into log(id,time,action_name,action_info) values ( 6, '2011-02-10', 'MAINTENANCE_STOP', 'maintenance02stop');
insert into log(id,time,action_name,action_info) values ( 7, '2011-03-10', 'MAINTENANCE_STOP', 'maintenance03stop');
--insert into log(id,time,action_name,action_info) values ( 8, '2011-04-10', 'MAINTENANCE_STOP', 'maintenance04stop');
insert into log(id,time,action_name,action_info) values ( 9, '2011-02-05', 'DEVICE_DEFECT', 'maintenance02defect');
insert into log(id,time,action_name,action_info) values ( 10, '2011-03-05', 'DEVICE_REPAIRED', 'maintenance03repaired');
select
maintenance.start as start
, maintenance.stop as stop
, count (device_action.*) as device_actions
from (select
l_start.time as start
, (select time
from log l_stop
where l_stop.time > l_start.time
and l_stop.action_name = 'MAINTENANCE_STOP'
order by time asc limit 1) as stop
from log l_start
where l_start.action_name='MAINTENANCE_START' order by l_start.time asc) maintenance
left join log device_action
on device_action.time > maintenance.start
and device_action.time < maintenance.stop
and device_action.action_name like 'DEVICE_%'
group by maintenance.start
, maintenance.stop
order by maintenance.start asc
;
Be carefull with performance. If Postgres didn't optimize nested query, it would take O(n^2) time.
If you may:
Change structure. E.g. one table DEVICE_MAINTENANCES with maintenance ID and second table DEVICE_MAINTENANCE_ACTIONS with foreign key DEVICE_MAINTENANCES.ID. Queries will be simpler and faster.
If not, treat time as primary key (implict index)
If not, create index on time column.