all groups > sql server programming > october 2007 >
You're in the

sql server programming

group:

insert convertion question


RE: insert convertion question Alejandro Mesa
10/24/2007 6:05:00 PM
sql server programming:
Andy,

[quoted text, click to view]

Yes, it does an implicit convertion. If SS can not convert the value, then
you will get an error.

[quoted text, click to view]

If the column is nullable, you can exclude that column from the insert
statement, or insert NULL value directly.

create table dbo.t1 (
[id] int not null unique,
dt datetime NULL
)

insert into dbo.t1([id]) values (1)
insert into dbo.t1([id], dt) values (2, NULL)

select * from dbo.t1

drop table dbo.t1
go


AMB

[quoted text, click to view]
Re: insert convertion question Alejandro Mesa
10/24/2007 6:36:00 PM
Andy,

SQL Server convert empty string to '1900-01-01T00:00:00.000'.

How are you doing the insert?

create table dbo.t1 (
[id] int not null unique,
dt datetime NULL
)
go

declare @d varchar(25)

set @d = ''

insert into dbo.t1([id], dt)
select 1, nullif(isdate(@d), 0)

select * from dbo.t1
go

drop table dbo.t1
go

AMB

[quoted text, click to view]
insert convertion question Andy
10/24/2007 8:14:49 PM
Hi,
I noticed that if I do insert into table (num, name) values ('1', 'john')
the result is the same as (1,'name')

where num is int field and name is varchar(50)
So does sql server convert string value to number if it can?

And last question, how with datetime field, how to set default value to null
instead of 1600 year I'm getting?
thanks

Re: insert convertion question Tom Moreau
10/24/2007 8:55:49 PM
SQL Server will do implicit char to int conversion, as long as the string is
a number.

Not sure what you mean in your last question. Have you got some sample
code?

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


[quoted text, click to view]
Hi,
I noticed that if I do insert into table (num, name) values ('1', 'john')
the result is the same as (1,'name')

where num is int field and name is varchar(50)
So does sql server convert string value to number if it can?

And last question, how with datetime field, how to set default value to null
instead of 1600 year I'm getting?
thanks

Re: insert convertion question Andy
10/24/2007 9:20:51 PM
Hi,
the problem is I have to have string in insert statement for every field. So
in that datetime case if no date is set I have just empty string in the
insert like (''). But when I do this I'm getting some default date with 1600
year in itinstead of null.

thanks guys for help


[quoted text, click to view]

Re: insert convertion question Andy
10/24/2007 11:33:17 PM
I have this: insert into dbo.t1 values (string)
and string is generated with values for every field in that way
'value1','value2' etc. I have like 200 fields.
For some reasons I don't use stored procedure for that.
I am almost sure I saw 1600 but I'll veryfi that.
thanks


[quoted text, click to view]

Re: insert convertion question Dan Guzman
10/25/2007 6:19:20 AM
[quoted text, click to view]

An empty string is not a NULL. If you need to insert a NULL, you'll need to
either specify the keyword NULL or omit the column from the INSERT
statement.

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
AddThis Social Bookmark Button