January 31, 2010 at 12:28 am
Hi,
can i convert a column that is int type to nvarchar where value is null in it?
THX
January 31, 2010 at 1:25 am
Hi,
Please find below a small repro of this.
use test
go
create table hello (I int)
go
insert into hello values(1)
insert into hello values(2)
insert into hello values(3)
insert into hello values(null)
insert into hello values(5)
insert into hello values(null)
insert into hello values(null)
insert into hello values(null)
go
select * from hello
OUTPUT
*******
I
-----------
1
2
3
NULL
5
NULL
NULL
NULL
select cast(i as nvarchar(20)) from hello
or
select convert(nvarchar(20),i) from hello
OUTPUT
*******
--------------------
1
2
3
NULL
5
NULL
NULL
NULL
I Hope this is what you were trying to know if not please mail me.
Regards
GURSETHI
(sethigurpreet@yahoo.com)
January 31, 2010 at 1:44 am
now i want to update the null value to varchar like 'No Data'
January 31, 2010 at 2:20 am
If you want to change the columns data type have a look to BOL for ALTER TABLE and ALTER COLUMN.
If you want to change the output, not the stored data, surround GURSETHI's CAST/CONVERT with ISNULL or COALESCE.
Greets
Flo
January 31, 2010 at 4:58 am
use a case statement as well as the cast/convert.
case when <column> is null then 'No data' else cast(<column> as varchar(x)) End
January 31, 2010 at 6:14 am
how to use the case in the update statment for a value that is NULL in the column?
THX
January 31, 2010 at 7:53 am
Mad-Dog (1/31/2010)
how to use the case in the update statment for a value that is NULL in the column?THX
update <tableA>
set <column> = case when B.<column> is null then 'No Data' else cast(<column> as nvarchar(x)) End
from <tableA>
join <tableB> on A.x = B.y
January 31, 2010 at 11:44 pm
select isnull(i,'NoData') from hello
February 1, 2010 at 12:39 am
Animal Magic (1/31/2010)
Mad-Dog (1/31/2010)
how to use the case in the update statment for a value that is NULL in the column?THX
update <tableA>
set <column> = case when B.<column> is null then 'No Data' else cast(<column> as nvarchar(x)) End
from <tableA>
join <tableB> on A.x = B.y
it's not working.
i get this error - Conversion failed when converting the nvarchar value 'No Data' to data type int.
February 1, 2010 at 2:19 am
is the field your updating an int field? if so you wont be able to put 'No data' into it. Post the create table scripts for both your tables and tell me what your trying to update.
February 1, 2010 at 4:00 am
Mad-Dog (2/1/2010)
it's not working.i get this error - Conversion failed when converting the nvarchar value 'No Data' to data type int.
It won't work, your column is defined as an integer, and your trying to put character data in it!!! :w00t:
If you want to do this you'll need to change the column type (which was mentioned in an earlier post). But I don't think that's really what you want to do.
If you could explain the reasoning behind your question in more detail then someone may be able to help.
February 1, 2010 at 4:49 am
yes I'm trying to update an int column to put varchar character.
i know that i can't update this column,but i think maybe there is something to do.
by the way how NULL can be insert into INT column?
February 1, 2010 at 5:01 am
Good, glad you understand that you can't update an integer column with a character value.
In general, any column can contain a NULL unless specifically prevented in the table definition (by specifying 'NOT NULL').
If you want to see the text 'No data' in place of the NULL in the results of a SELECT then you need to use, ISNULL or COALESCE, or even CASE (I think this was mentioned earlier).
Could you give further details on what you are going to do with this data. Display in a web page or in a report or what?
February 1, 2010 at 6:08 am
Mad-Dog (2/1/2010)
yes I'm trying to update an int column to put varchar character.
By definition you cant. No amount of convert/case/coalesce will ever get this work.
Mad-Dog (2/1/2010)
i know that i can't update this column,but i think maybe there is something to do.
Like what? Why dont you put a -1 in if the value from the other table is null?
February 1, 2010 at 8:26 am
thanks all for your answers.
I'll try different approach.
p.s - i'm workig on a html report that sent from sql.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply