Runtime Exception :
Cannot insert the value NULL into column 'CreationTime', table
'MyTables'; column does not allow nulls. INSERT
fails.
Code:
INSERT INTO [MyTables] (LegacyId, CreationTime)
SELECT DISTINCT
a.[IPLID], a.[inputdate]
FROM
[Legacy].[dbo].[MyTables2] AS a
Can you tell me how to insert custom date like 01/01/2000 when a.[inputdate] is Null ?
just wrap in an ISNULL:
INSERT INTO [MyTables] (LegacyId,CreationTime)
SELECT DISTINCT a.[IPLID],ISNULL(a.[inputdate], '01/01/2000')
FROM [Legacy].[dbo].[MyTables2] as a
Use the ISNULL function.
INSERT INTO [MyTables] (LegacyId,CreationTime)
SELECT DISTINCT a.[IPLID],ISNULL(a.[inputdate], '01/01/2000') FROM [Legacy].[dbo].[MyTables2] as a
Related
When I am using isnull it does not return the '' please see below I have original DOB, isnull used, cast as date.
You would need to convert dob to a char/nchar/varchar/nvarchar type to use isnull() or coalesce() like that.
select isnull(convert(varchar(10),dob,120),'')
if you really would like to return an empty string for the date value, you could try this in a new query window. It creates a table to repoduce your requirement of a null date value and then selects the value before dropping the table.
CREATE TABLE dbo.Test
(
Id INT IDENTITY(1,1) NOT NULL
,Date1 DATE NULL
)
INSERT INTO dbo.Test(Date1) VALUES ('01/01/2017')
INSERT INTO dbo.Test(Date1) VALUES ('01/02/2017')
INSERT INTO dbo.Test(Date1) VALUES (NULL)
INSERT INTO dbo.Test(Date1) VALUES ('01/04/2017')
SELECT * FROM dbo.Test
SELECT Date1 = CASE WHEN date1 IS NULL THEN '' ELSE CAST(DATE1 AS VARCHAR(10)) END from dbo.Test
DROP TABLE dbo.Test
go
I want to insert value from my sub query
my query is like that..
insert into T_Scanned(F_Asset_Code) values (select F_Barcode from T_Assets where F_Barcode
in( select Barcode as barcoade from [T_NEWASSET]))
but this is not working...
You don't need to use VALUES when doing an INSERT INTO ... SELECT:
insert into T_Scanned(F_Asset_Code)
select F_Barcode from T_Assets where F_Barcode
in( select Barcode as barcoade from [T_NEWASSET])
I already have a table built in oracle.
Im trying to insert some data like this:
INSERT INTO movies_actor('name','id')
VALUES ('Nuno','2'), ('Pedro','3'), ('Jose','1');
select * from movies_actor;
I always get this error
ORA-00928: missing SELECT keyword
What am I doing wrong?
I don't think you need the single quote around your field names.
You need to do:
INSERT INTO TableName(Column1, Column2)
VALUES('Nuno', '2');
In your example, it would be:
INSERT INTO movies_actor(name, id)
VALUES ('Nuno','2');
INSERT INTO movies_actor(name, id)
VALUES ('Pedro','3');
INSERT INTO movies_actor(name, id)
VALUES ('Jose','1');
select * from movies_actor;
Another way.
insert into table
(field1, field2)
select value1, value2
from dual
union
select value3, value4
from dual
etc
You cannot insert multiple records in one statement using VALUES. You can either use Tenzin's solution or use INSERT ALL :
INSERT ALL
INTO movies_actor(name, id) VALUES ('Nuno', '2')
INTO movies_actor(name, id) VALUES ('Pedro', '3')
INTO movies_actor(name, id) VALUES ('Jose', '1')
SELECT * FROM dual;
I have an insert statement where in i am inserting the data from table_abc to table_job. Now there is mandatory column in effec_start_date in table_job,
but this effec_start_date is not mandatory in table_Abc. Hence there may be null values too in effec_start_date of table_abc.
While inserting from table_abc to table_job it is going into exception
stating
-1400 - ORA-01400: cannot insert NULL into ("HR"."table_job"."EFF_START_DATE")
and obviously when querying table_job it is not returning any value
Is there a possibility to insert the not null values for effective_Start_date in table_job and for only null values it goes into exception
insert into table_job
(job_code,
eff_start_date,
eff_end_date,
config_id
)
select * from table_Abc;
you need to filter your data in the select statement
insert into table_job
(job_code,
eff_start_date,
eff_end_date,
config_id
)
select * from table_Abc
where eff_start_date is not null;
I'm trying to insert some data to a table contains two things : "a string" and "maximum number in Order column + 1".
This is my query:
INSERT INTO MyTable ([Text],[Order])
SELECT 'MyText' , (Max([Order]) + 1)
FROM MyTable
What is going wrong with my query?
I'm using Microsoft SQL Server 2005 SP3.
You can test this query like this:
I don't receive error:
create table #MyTable
(
[Text] varchar(40),
[Order] int NOT NULL
)
INSERT INTO #MyTable([Text],[Order])
SELECT 'MyText' [Text], isnull(max([order]) + 1, 0) [Order]
FROM #MyTable
drop table #MyTable
Original:
INSERT INTO MyTable ([Text],[Order])
SELECT 'MyText' [Text], max([Order]) + 1 [Order]
FROM MyTable
or
INSERT INTO MyTable ([Text],[Order])
SELECT top 1 'MyText' [Text], max([Order]) + 1 [Order]
FROM MyTable
limit is not valid in SQL Server as far as I know.
Cannot insert the value NULL into column 'Order', table 'master.dbo.MyTable'; column does not allow nulls. INSERT fails. The statement has been terminated.
This means that the Order column isn't allowed to be null, and that the Max([Order]) + 1 part of your column returns NULL.
This is because your table is empty, as you already noticed by yourself.
You can work around this by replacing NULL by a real number in the query, using ISNULL():
INSERT INTO MyTable ([Text],[Order])
SELECT 'MyText' , (isnull(Max([Order]),0) + 1)
FROM MyTable
Unless he has a column named OrderBy
then he would have to add / assign all values within that Insert especially if the column does not allow for nulls
sounds like fully qualifying the Insert with the dbo.MyTable.Field may make more sense.
also why are you naming fields with SQL Key words...???
INSERT INTO MyTable ([Text],[Order] Values('MyTextTest',1)
try a test insert first..