How do I insert datetime as just hh:mm:ss? - sql

I'm trying to insert a time into at table.
"playTime" is the datetime value. I don't need the actual days for it.
Insert playTime values ('00:05:15:00') but it's giving me an error.
How can I solve this?
Thanks!

Use a time field instead of a datetime field. Or insert it with a date then ignore the date when you select it back out.

You cannot just insert time value into a datetime field. You definitely need to have datetime appended. If you are sure the entries to the field have to be of time, change the data type. If not append a default date (say 1900-01-01) and then insert it.
insert playTime values('1900-01-01 05:15:00')

Related

Dates inserting incorrectly - SQL

Dates are not inserting correctly to table, any explanation / solution?
create table test
(
ID bigint,
MarketOpen datetime
);
insert into test (ID, MarketOpen)
values (1, 2019-01-19-11-40-00);
select * from test;
Fiddle
Thats totally the wrong way to enter a date. SQL Server is treating your current syntax as a calculation e.g. 2019-01-19-11-40-00=1948 and then converting the number 1948 to a datetime. You need to use a formatted string e.g.
insert into #test (ID, EventId, MarketId, RaceDate, MarketOpen, HorseID)
values
(1, 123, 153722767, '2019-01-19 11:40:00', '2019-01-18 11:40:00', 34434);
Note: As mentioned by seanb its best practice to use a non-ambiguous format when specifying dates and the ISO format (yyyymmdd) is probably the best of these.

How to stop the insertion of default date format 1900-01-01 in SQL Server when user input is null

I have an input field in my application,which accepts date through date picker. Sometimes users do not select any date and they may submit blank (client requirement) as it's not a mandatory field.
Once after blank submission the data which is stored into the SQL Server 2008 database is '1900-01-01'. Instead I need the data to be stored as 'NULL' or 'empty' or '0000-00-00' but not any other valid date format.
If the request.getparameter() is empty, I tried to insert '0000-00-00'. But it is stopping the insertion.
My aim is to insert either 'NULL' or '-' or '0000-00-00' when the user is submitting the blank field. Instead of '1900-01-01'.
Unfortunately, SQL Server interprets a "blank" (i.e. empty string) as a date with the value of 1900-01-01. So when you write:
insert into t (datecol)
values ('');
This is basically compiled into:
insert into t (datecol)
values ('1900-01-01');
What can you do? The most obvious is to not insert blank values. You want to insert NULL, so insert NULL. In this case, you can use NULLIF():
insert into t (datecol)
values (nullif('', ''));
That will turn the input into an appropriate value.
A second option is to reject such values using a check constraint:
alter table t add constraint chk_t_datecol
check (date >= '1950-01-01');
That is, dates are within some acceptable range.
A third option would be to add a trigger to convert '1900-01-01' to a NULL value when seen on input.
And a fourth option would be to allow such values but to use a computed column for reference purposes:
alter table t add datecol_good as ( nullif(datecol, '1900-01-01'));
All of these options assume that '1900-01-01' is not a valid date that you would want in your data.

Create a Trigger to change values before insert in Informix

We have a table with a date field. Also there are some old apps we can't modify whose inserts that date field with string ("yyyy-mm-dd") (Yeah, SQL Injectión Party!!). Now it's needed to change that field to a Datetime year to second. This makes that old apps fail.
create table test(
testDate DATETIME YEAR TO SECOND
);
insert into test values("2018-12-12")
Error: A field in a datetime or interval value is incorrect or an illegal operation specified on datetime field.
We are trying to make a trigger to concatenate " 00:00:00" to the string if its length is 10. That should correct the problem.
It's that even posible?
Seems to me that if I use a before insert trigger, I can't modify the insert values. But if I use for each row instead, the trigger is not launched because the Error is throwed first.
Any Idea?

How to Create a ORACLE Table to store Appointment Time

I am trying to add to my current appointment table on ORACLE to store time of the appointment
ALTER TABLE APPOINTMENT
add (Timeofappointment to_date NOT NULL)
Here the code seems to be fine but the function to_date, or the way I am writing it don't seem to be working.
Any suggestion as I have not found one to be able to work online.
I want it to store time like this: 13:00
You'll have to use the DATE datatype to store the appointment time. Here's a quick sample to get you started.
create table mydate(i NUMBER, d date)
insert into mydate values(1, TO_DATE('2014-03-14 16:24','YYYY-MM-DD HH24:MI'));
SELECT * FROM mydate
You'll need to add the column to your table and use similar format. When you make appointments you definitely need to consider the date as well as time. Just time by itself will have little value.

Milliseconds from GETUTCDATE not stored in datetime field

I have stored procedure that inserts data into table. One column in the table is datetime and is used for storing the time stamp of row insert:
INSERT INTO myTable (Field1, Field2, Field3) VALUES (1, 2, GETUTCDATE());
Field3 is datetime column. When I select data from that table with simple SELECT * FROM myTable query, all datetime values are shown with .000 value for milliseconds.
If I execute SELECT GETUTCDATE(), the milliseconds are displayed: 2013-10-16 18:02:55.793
Why milliseconds are not stored/displayed in the date time column on SELECT?
You have to be doing something somewhere to change this to smalldatetime or something because it works fine. I just created a new table, inserted data like you showed, queried the table and I have the milliseconds.
I have been unable to find anything where you can set the precision at the server level so it must be in your code.
Which datetime type are you using? To store the date with precision up to a millisecond, you need to use DATETIME2 if you're using SQL Server 2008 or higher.
'DATETIME' gives a precision of about 1/300th of a second.
'SMALLDATETIME' has accuracy of 1 minute.
Source: http://msdn.microsoft.com/en-us/library/ff848733.aspx
As Steve suggested, issue was not related with server. There is a trigger on this table, that trigger does milliseconds rounding on insert.