question

SrinivasMaddula-4047 avatar image
0 Votes"
SrinivasMaddula-4047 asked SrinivasMaddula-4047 commented

how to insert default value(1753-01-01 00:00:00.000) in datetime field if select query returns null in sql server

Hi All,

Can someone provide the query to insert default value(1753-01-01 00:00:00.000) in datetime field if select query from other table returns null in sql server

Example:

INSERT INTO [dbo].[Test] ([time1])
SELECT c.[time2] from [dbo].[Test1] c

where [time2] allows Null values and [time1] does not allow Null values.


Thanks in advance,
Srinivas Maddula

sql-server-transact-sql
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MelissaMa-msft avatar image
0 Votes"
MelissaMa-msft answered SrinivasMaddula-4047 commented

Hi @SrinivasMaddula-4047,

Please refer below example:

 create table [Test]
 (id int identity(1,1),
 [time1] datetime not null)
    
 create table [Test1]
 (id int,
 [time2] datetime)
    
 insert into [Test1] values
 (1,NULL)
    
 INSERT INTO [dbo].[Test] ([time1])
 SELECT isnull(c.[time2],'1753-01-01 00:00:00.000') from [dbo].[Test1] c
    
 select * from [Test]

Output:

 id    time1
 1    1753-01-01 00:00:00.000

Best regards,
Melissa


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What if we have 100s of Fileds and multiple tables.

0 Votes 0 ·