Inserting a timestamp into a table in MS SQL Management Studio - sql

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

Related

Updating timestamp with custom format (dd/MM/yyyy HH:MM:ss.FFFFFFF)

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'

Bulk insert issue with date from excel into SQL Table

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)

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.

What means DEFAULT VALUES specification in an insert query?

I am pretty new in Microsoft SQL Server and I am not so into DB in general.
I have the following doubt about an insert query that begin in this way:
insert into MyTable DEFAULT VALUES
What exactly mean the DEFAULT VALUES specification?
Tnx
Andrea
Reading the fine manual yields:
DEFAULT VALUES
Forces the new row to contain the default values defined for each column.
Well it uses the default values specified in your table.
So for example if you have a column CreationDate datetime default(getdate()) it will use it.
If each of the required columns in MyTable has specified DEFAULT VALUE then this statement insert such a row.
For example you could have column Date with default 01/01/2014 and position with DEFAULT 'Developer' and this statement would insert such a record.
You can read more here: http://msdn.microsoft.com/en-us/library/aa933206%28SQL.80%29.aspx
You can watch default specifications in work by checking that code:
DECLARE #tmp as table
(
id int null,
num int null default(777),
txt varchar(10) null default('abc'),
date datetime null
)
insert into #tmp DEFAULT VALUES
select * from #tmp
Output is
id num txt date
NULL 777 abc NULL

SQL Server 2012 sequence

I create a table and sequence in order to replace identity in the table I use SQL Server 2012 Express but I get this error while I tried to insert data to the table
Msg 11719, Level 15, State 1, Line 2
NEXT VALUE FOR function is not allowed in check constraints, default objects, computed columns,
views, user-defined functions, user-defined aggregates, user-defined
table types, sub-queries, common table expressions, or derived
tables.
T-SQL code:
insert into Job_Update_Log(log_id, update_reason, jobid)
values((select next value for Job_Log_Update_SEQ),'grammer fixing',39);
This is my table:
create table Job_Update_Log
(
log_id int primary key ,
update_reason nvarchar(100) ,
update_date date default getdate(),
jobid bigint not null,
foreign key(jobid) references jobslist(jobid)
);
and this is my sequence:
CREATE SEQUENCE [dbo].[Job_Log_Update_SEQ]
AS [int]
START WITH 1
INCREMENT BY 1
NO CACHE
GO
Just get rid of the subselect in the VALUES section, like this:
insert into Job_Update_Log(log_id,update_reason,jobid)
values (next value for Job_Log_Update_SEQ,'grammer fixing',39);
Reference: http://msdn.microsoft.com/en-us/library/hh272694%28v=vs.103%29.aspx
Your insert syntax appears to be wrong. You are attempting to use a SELECT statement inside of the VALUES section of your query. If you want to use SELECT then you will use:
insert into Job_Update_Log(log_id,update_reason,jobid)
select next value for Job_Log_Update_SEQ,'grammer fixing',39;
See SQL Fiddle with Demo
I changed the syntax from INSERT INTO VALUES to INSERT INTO ... SELECT. I used this because you are selecting the next value of the sequence.
However, if you want to use the INSERT INTO.. VALUES, you will have to remove the SELECT from the query:
insert into Job_Update_Log(log_id,update_reason,jobid)
values(next value for Job_Log_Update_SEQ,'grammer fixing',39);
See SQL Fiddle with Demo
Both of these will INSERT the record into the table.
Try this one:
–With a table
create sequence idsequence
start with 1 increment by 3
create table Products_ext
(
id int,
Name varchar(50)
);
INSERT dbo.Products_ext (Id, Name)
VALUES (NEXT VALUE FOR dbo.idsequence, ‘ProductItem’);
select * from Products_ext;
/* If you run the above statement two types, you will get the following:-
1 ProductItem
4 ProductItem
*/
drop table Products_ext;
drop sequence idsequence;
------------------------------