sql server programming:
Andy, [quoted text, click to view] > So does sql server convert string value to number if it can?
Yes, it does an implicit convertion. If SS can not convert the value, then you will get an error. [quoted text, click to view] > And last question, how with datetime field, how to set default value to null > instead of 1600 year I'm getting?
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] "Andy" wrote: > 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 > >
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] "Andy" wrote: > 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 > > > "Alejandro Mesa" <AlejandroMesa@discussions.microsoft.com> wrote in message > news:44DFF4F2-8174-4BA8-89EB-EEEB0FC437BB@microsoft.com... > > Andy, > > > >> So does sql server convert string value to number if it can? > > > > Yes, it does an implicit convertion. If SS can not convert the value, then > > you will get an error. > > > >> And last question, how with datetime field, how to set default value to > >> null > >> instead of 1600 year I'm getting? > > > > 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 > > > > "Andy" wrote: > > > >> 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 > >> > >> > >> > >
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
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] "Andy" <kc2ine@yahoo.com> wrote in message news:eYJPPwpFIHA.3716@TK2MSFTNGP03.phx.gbl...
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
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] "Alejandro Mesa" <AlejandroMesa@discussions.microsoft.com> wrote in message news:44DFF4F2-8174-4BA8-89EB-EEEB0FC437BB@microsoft.com... > Andy, > >> So does sql server convert string value to number if it can? > > Yes, it does an implicit convertion. If SS can not convert the value, then > you will get an error. > >> And last question, how with datetime field, how to set default value to >> null >> instead of 1600 year I'm getting? > > 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 > > "Andy" wrote: > >> 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 >> >> >>
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] "Alejandro Mesa" <AlejandroMesa@discussions.microsoft.com> wrote in message news:0833891F-B891-4185-A52C-2C51EBA2990F@microsoft.com... > 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 > > "Andy" wrote: > >> 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 >> >> >> "Alejandro Mesa" <AlejandroMesa@discussions.microsoft.com> wrote in >> message >> news:44DFF4F2-8174-4BA8-89EB-EEEB0FC437BB@microsoft.com... >> > Andy, >> > >> >> So does sql server convert string value to number if it can? >> > >> > Yes, it does an implicit convertion. If SS can not convert the value, >> > then >> > you will get an error. >> > >> >> And last question, how with datetime field, how to set default value >> >> to >> >> null >> >> instead of 1600 year I'm getting? >> > >> > 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 >> > >> > "Andy" wrote: >> > >> >> 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 >> >> >> >> >> >> >> >> >>
[quoted text, click to view] > 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.
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] "Andy" <kc2ine@yahoo.com> wrote in message news:edrNQVqFIHA.536@TK2MSFTNGP06.phx.gbl... > 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 > > > "Alejandro Mesa" <AlejandroMesa@discussions.microsoft.com> wrote in > message news:44DFF4F2-8174-4BA8-89EB-EEEB0FC437BB@microsoft.com... >> Andy, >> >>> So does sql server convert string value to number if it can? >> >> Yes, it does an implicit convertion. If SS can not convert the value, >> then >> you will get an error. >> >>> And last question, how with datetime field, how to set default value to >>> null >>> instead of 1600 year I'm getting? >> >> 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 >> >> "Andy" wrote: >> >>> 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 >>> >>> >>> > >
Don't see what you're looking for? Try a search.
|