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.
Related
How can I convert String to Date type in SQL?
I have a column called timestamp (type varchar) with the following data:
timestamp
06.07.2021 00:00:00
05.07.2021 00:00:00
04.07.2021 00:00:00
I would like to run the following query which is unfortunately not working:
select timestamp from table where
convert(datetime,timestamp,112) = convert(datetime, '20210706')
How should I replace "convert(datetime,timestamp,112)" so that it works?
try use PARSE t-sql function
SELECT PARSE(timestamp AS date USING 'Ru-RU') from table
SQL Server is actually pretty flexible on converting date/times without a format. So, if you want to convert to a date/time, this works:
select convert(datetime, '06.07.2021 00:00:00')
If you are only looking at the date, though, you might want to limit to the first 10 characters. If you are certain that all the strings have valid dates, you can use 104 to convert the value:
select cast(date, left(datetime, 10), 104)
You are comparing against date value, you can convert to date datatype and apply WHERE clause.
declare #TABLE TABLE(timestampvalue varchar(20))
INSERT into #table
values
('06.07.2021 00:00:00'),
('05.07.2021 00:00:00'),
('04.07.2021 00:00:00');
SELECT convert(date,timestampvalue,104) FROM #table where convert(date,timestampvalue,104) = '20210706'
2021-07-06
Note: This type of applying CONVERT function in the WHERE clause, will make the query Non-Sargeable. It is better to change datatype to DATE and then do comparison.
UPDATE TABLE <TABLENAME>
SET TimeStampValue = CONVERT(DATE,TimeStampValue,104)
Now, you can ALTER table.
ALTER TABLE <TABLENAME> ALTER COLUMN TimeStampValue DATE
Now, you can apply the WHERE clause without conversion.
SELECT * FROM tablename where timestampvalue = '20210706'
I have a CSV file containing about 30m rows with a column that has a date type. But the problem is with its format. PSQL supports '-' delimiters for timestamp but my dates are using '/'. For example, the date should be '2021-02-01 00:00:00' but my date format is '2021/02/01 00:00:00'. Also, I can not open the CSV file and change it manually due to its large size. I am trying to import my data into a temporary table to replace the '/' with '-' and then inserting them to a new table and I am using the following command(it is not the real table and it is just an example):
CREATE TABLE TMP(
dt VARCHAR
)
CREATE TABLE other_tmp(
dt TIMESTAMP
)
INSERT INTO TMP VALUES('2020/01/02 22:33:11');
INSERT INTO other_tmp(dt)
SELECT dt,
REPLACE(dt, '/', '-')
FROM TMP
I get an error with replace function when I want to run it.
Does anybody know that how can I solve this problem? Or even is it possible to manipulate the column in the original table?
Try this,
In this case, your column "dt" is of type timestamp without time zone but the expression is of type text. So the text should cast to timestamp as below.
CREATE TABLE IF NOT EXISTS TMP
(
dt VARCHAR
);
CREATE TABLE IF NOT EXISTS other_tmp
(
dt TIMESTAMP
);
INSERT INTO TMP
VALUES ('2020/01/02 22:33:11'::TIMESTAMP);
INSERT INTO other_tmp(dt)
SELECT REPLACE(dt, '/', '-')::TIMESTAMP AS dt
FROM TMP;
Your first error is that you have two columns in the SELECT list, but only one column in the INSERT target.
To convert a string to a timestamp, use to_timstamp()
insert into other_tmp(dt)
select to_timestamp(dt, 'yyyy/mm/dd hh24:mi:ss')
from tmp;
Online example
So I have a couple date fields in a table that are formatted as (DD-MON-YYYY) and I need to convert them to (DD-MM-YYYY) The field itself is already a VARCHAR. In Snowflake, how would I make this change. Here is what I have done so far.
select to_date(end_date, 'dd-mm-yyyy') from
error: Can't parse '31-JAN-2020' as date with format 'dd-mm-yyyy'
An easy way for you to approach this is to convert your string to a date, and then back to a string in the new format, an example follows:
--create an example table to test
CREATE TEMPORARY TABLE xyz (my_dt_string VARCHAR(100));
--insert a couple records
INSERT INTO xyz VALUES ('31-JAN-2020'), ('13-APR-2020');
--test a conversion, from string to date back to string
SELECT my_dt_string,
TO_CHAR(TO_DATE(my_dt_string, 'dd-mon-yyyy'), 'dd-mm-yyyy') converted
FROM xyz;
--looks good? run the following
UPDATE xyz
SET my_dt_string = TO_CHAR(TO_DATE(my_dt_string, 'dd-mon-yyyy'), 'dd-mm-yyyy');
--take a look
select * from xyz;
I hope this helps...Rich
The to_date() function provides you the ability to convert a VARCHAR field to a DATE field, not specify the format of the date. So, you'd want to run this:
SELECT TO_DATE(end_date, 'DD-MON-YYYY');
This will output the date as a date. If you want to specify how you see a date, you have 2 choices:
First, convert it back to a VARCHAR in your preferred format:
SELECT TO_VARCHAR(TO_DATE(end_date, 'DD-MON-YYYY'),'DD-MM-YYYY');
Second, change your session parameter DATE_OUTPUT_FORMAT to be the format you'd like to see dates displayed:
ALTER SESSION SET DATE_OUTPUT_FORMAT = 'DD-MM-YYYY';
This could also be done for a user, rather than a session using:
ALTER USER SET DATE_OUTPUT_FORMAT = 'DD-MM-YYYY';
How to write a statement that will update the date field in database with dd/MM/yyyy HH:MM:ss.FFFFFFF format.
I have select query which return the require string
SELECT FORMAT(CURRENT_TIMESTAMP, 'dd/MM/yyyy HH:MM:ss.FFFFFFF')
I tried with
update ORDER
set timedate=FORMAT(CURRENT_TIMESTAMP, 'dd/MM/yyyy HH:MM:ss.FFFFFFF')
WHERE ID='288'
But returning error :
SQL Error [8152] [22001]: String or binary data would be truncated.
com.microsoft.sqlserver.jdbc.SQLServerException: String or binary data would
be truncated.
My field datatype is varchar 27
CREATE TABLE AYAM.dbo.ORDER (
ID int NOT NULL IDENTITY(1,1),
TIMEDATE varchar(27) NOT NULL,
CONSTRAINT PK_ORDER_DATA PRIMARY KEY (ID,TIMEDATE)
) GO;
I am using MSSQL 2016
Why do you ask for FFFFFFF when CURRENT_TIMESTAMP function returns only 3 decimal numbers? Use SYSDATETIME() function to get better precision.
I was unable to re-produce the error. What SQL Server version do you use?
create table #test (timedate varchar(27))
insert into #test VALUES ('test');
update #test
set timedate=FORMAT(SYSDATETIME(), 'dd/MM/yyyy HH:MM:ss.FFFFFFF')
select * from #test
drop table #test
The output is: 18/09/2017 12:09:44.6914345
UPDATED:
the same test was done using your table structure. No errors ...
INSERT INTO dbo.[ORDER] (TIMEDATE) VALUES ('test')
GO 300
update [ORDER]
set timedate=FORMAT(CURRENT_TIMESTAMP, 'dd/MM/yyyy HH:MM:ss.FFFFFFF')
WHERE ID='288'
I am trying to bulk insert these two columns from excel into a temp table ##NBP_Table. However, when I do that I get the following error:
'Operand type clash: int is incompatible with date'
Does that mean the date aren't in the format it should be to be inserted into a table?
create table ##NBP_Table
(
Applicable_Date date,
NBP_Value numeric(4,4)
)
insert into ##NBP_Table
values (01/04/2014,1.7107),
(02/04/2014,1.6482),
(03/04/2014,1.686),
(04/04/2014,1.6681)
To get the date insert working, please try this
create table ##NBP_Table
(
Applicable_Date date
NBP_Value numeric(5,4)
)
insert into ##NBP_Table
values ('01/04/2014',1.7107)
The date needs to be in quotation marks
I have also corrected the numeric data type for you
this date in expression is considered as int so it will be performed / operations,
so please use 'before starting date and ' after ending date.
'01-04-2014'
Create table #NBP_Table
(
Applicable_Date date,
NBP_Value numeric(5,4)
)
insert into #NBP_Table
values ('01-04-2014',1.7107),
('02-04-2014',1.6482),
('03-04-2014',1.686),
('04-04-2014',1.6681)