Dates inserting incorrectly - SQL - 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.

Related

How to validate the date in a varchar column in SQL Server

I have a staging table which contains all varchar columns. I want to validate a date stored in the data column. Since my staging table contains all varchar columns, then all csv records are inserted into table.
After inserted into the staging table, I need a validation for specific date column to validate date are properly present or not. If any string value comes then I need to eliminate from staging table
Building on #Larnu's comment, you can use TRY_CONVERT to select only the records that contain proper dates, then use those records to do some further action. Consider the following example:
-- Using a table variable as an example of the source data
DECLARE #SampleTable TABLE
(
Id int,
SomePossibleDateField varchar(20)
)
-- Now insert some sample data into the table variable, just for illustration
INSERT INTO #SampleTable
VALUES (1, '2021-05-04'),
(2, '2021-05-05'),
(3, 'not a date'),
(4, NULL),
(5, ''),
(6, '2021-05-06')
-- Now select all the records that contain proper dates:
SELECT * FROM #SampleTable WHERE TRY_CONVERT(DATE, [SomePossibleDateField], 120) > '1900-01-01'
The results of the final select statement above are
Id SomePossibleDateField
1 2021-05-04
2 2021-05-05
6 2021-05-06
Some things to note:
First, in this sample, for simplicity, all the dates are expressed as format 120 (ODBC Canonical). So you may need to try different formats depending on your data. See the date formats listed on the CAST page for the different format values.
Second, that select statement tests for dates greater than the year 1900, but you can change that to any other date that makes sense for your data.
Finally, in case you are looking specifically for records that only contain bad data, you can do that by changing the select statement to something like:
SELECT * FROM #SampleTable
WHERE TRY_CONVERT(DATE, [SomePossibleDateField], 120) = ''
OR TRY_CONVERT(DATE, [SomePossibleDateField], 120) IS NULL
Which results with:
Id SomePossibleDateField
3 not a date
4 NULL
5
Unfortunately, an empty string does not result in NULL like bad data does, it simply gets passed through as empty string. So, if you are specifically looking for bad records, you will need to check both for IS NULL and for '' as shown in the example above.

Snowflake - insert date

I have a value of 12/31/18 and created table in snowflake:
create table my_date (a date);
insert into my_date values ('12/31/18');
select * from my_date;
Result: 0018-12-31
I want to get: 2018-12-31
I saw about 2 number format:
https://docs.snowflake.net/manuals/sql-reference/parameters.html#label-two-digit-century-start
but not sure if this is specification of a column type or data needs to be transformed before the insert?
The parameter two_digit_century_start seems not to be used when parameter date_input_format is set to AUTO. You can get your example working correctly by setting the date format with a parameter ("alter session..." statement on line 2 below). Your complete working example would look like this:
create table my_date (a date);
alter session set DATE_INPUT_FORMAT = 'MM/DD/YY';
insert into my_date values ('12/31/18');
select * from my_date;
This results in 2018-12-31.
Snowflake best-practices recommend to specify the format explicitly with to_date(value, 'format') or by setting the format in parameters. You can find the best practices for date/time functions from Snowflake documentation here: https://docs.snowflake.net/manuals/user-guide/date-time-input-output.html#date-time-function-format-best-practices

How to insert data in a 'DATE' type sql [duplicate]

This question already has answers here:
Date casting in oracle
(1 answer)
SQL oracle beginner questions
(3 answers)
SQL Date Format Conversion
(3 answers)
Comparing Dates in Oracle SQL
(5 answers)
Closed 4 years ago.
I have created tables like this,
create table customer
(
customer_id number(3),
customer_name varchar(20),
phone varchar(15)
);
create table manager
(
customer_id number(3),
ordered_products number(4),
transaction_on DATE
);
Now how will i insert data in manager table? How to insert into the DATE type?
The answer to this really depends on the type of sql you are using.
In general if the date time is present in the ISO format (yyyymmdd), any sql engine should not face any issues in parsing it. A custom date format would be tricky and specific to individual SQL clients, but here are the broad types to help you get started:
Oracle/PostgreSQL
INSERT INTO manager
(
transaction_on
)
SELECT TO_DATE('23-01-2018', 'dd-mm-yyyy')
FROM DUAL;
This will allow you to parse any date format, as long as you can specify the date format in the following string. The values I have provided are only examples, you can play around with them as per your specific use case. Oracle Reference, Postgres Reference
SQL Server
INSERT INTO manager
(
transaction_on
)
SELECT CONVERT(DATE, '02-25-2018', 101);
Here 101 is style indicator from a pre-specified list, which supports a wide range of date formats. You can select the style indicator based on your specific input string format
MySQL
INSERT INTO manager
(
transaction_on
)
SELECT STR_TO_DATE('May 1, 2013','%M %d,%Y');
This is pretty similar to usage in Postgres and Oracle, except that the date format is represented slightly differently. My SQL Reference
You can insert via SQL Statement:
INSERT INTO manager (customer_id, ordered_products, transaction_on)
VALUES (123, 2, '2018-03-07 05:52:30');
We can provide more accurate answer if we know the database type. (etc. MySQL, MSSQL, PosgreSQL, ...)
As a generic answer: This would probably work:
INSERT INTO manager (
customer_id,
ordered_products,
transaction_on)
VALUES (
001,
5,
'2000-02-28'
);
In MySQL, you can do it in two ways if you want to insert the current date
INSERT INTO manager (customer_id, ordered_products, transaction_on)
VALUES (NULL, '110', NOW());
or for any date you can use
INSERT INTO manager (customer_id, ordered_products, transaction_on) VALUES (NULL,'10', '2018-03-07');
In SQL Server if you want the date to be inserted automatically when the transaction occurs.
ALTER TABLE manager ADD CONSTRAINT DF_DefaultTranDate DEFAULT GETDATE() FOR transaction_on
Whenever a row is inserted in the manager table the transaction_on column will have the server's date and time.

Covert a column of string dates

I have 2 columns in access that are saved as string dates yyyymmdd. I am linking the table to a oracle database and need to coveret the columns on insert to look like yyyy/mm/dd.
I am trying:
INSERT INTO TEST
(DATE) Values (20110818, To_DATE("YYYY/MM/DD"))
FROM TEST_DATE
I want to convert the entire column on insert from access into oracle
Try like this
INSERT INTO TEST
(DATE)
SELECT TO_DATE('20110818','YYYYMMDD')
FROM TEST_DATE

Insert formatted date function using Oracle SQL*PLus?

Am a Oracle PL/SQL newbie. Basically, I have a table created as follows:
CREATE TABLE MYTABLE (
ID VARCHAR2(3 CHAR) NOT NULL PRIMARY KEY,
LAST_UPDATE DATE
);
Am trying to write a script which will run through SQL*Plus command:
insert into MYTABLE (
id,
last_update)
values (
sys_guid,
--- Date call - what is placed here?
);
What can I do to insure that the date inserted is the correct date (at time of insertion) with this format (what function to call):
27-Oct-11
Will need this script to be executable from within SQL*Plus (read that there's numerous amounts of incompatibilities between ANSI SQL & PL/SQL running from SQL Developer vs. SQL*Plus).
Thanks for taking the time to read this!
Date fields don't have a format. You apply a format when you select them by using the to_char function. So what you want to do is
insert into mytable
(id, last_update)
values
(sys_guid, SYSDATE);
And you select from it using
select id, to_char(last_update, 'DD-Mon-YY')
from mytable;