Showing posts with label handle. Show all posts
Showing posts with label handle. Show all posts

Friday, March 30, 2012

Replacing NULL value in multiple columns in a table

Hi,

I have some tables where I import data in, lots of field have gotten a
NULL value which the application can not handle.

Now can I replace each NULL value with '' in a columns with:
update <tableset [<column>] = '' where [<column>] IS NULL

But because there are lots of columns this is pretty much work, also
there are multiple tables.

Is there an easy way to replace all NULL values in all columns in a
table?

Thanks in Advance
BobBF (bob@.faessen.net) writes:

Quote:

Originally Posted by

I have some tables where I import data in, lots of field have gotten a
NULL value which the application can not handle.
>
Now can I replace each NULL value with '' in a columns with:
update <tableset [<column>] = '' where [<column>] IS NULL
>
But because there are lots of columns this is pretty much work, also
there are multiple tables.
>
Is there an easy way to replace all NULL values in all columns in a
table?


First of all, that operation would only be possible with columns
that hold character data. For numeric and datetime columns there
is rarely any good replacement for NULL values. So, unless, your
database only has nullable character columns, you need to fix the
application to handle NULL values anyway.

No, there is no direct function for setting many columns to NULL. You
need to have an UPDATE statement for each table, and one that lists
all columns that should be set to NULL. The good news is that you
can generate the statements:

SELECT 'UPDATE ' + o.name + ' SET ' + c.name + ' = '''' WHERE ' +
c.name + ' IS NULL'
FROM sysobjects o
JOIN syscolumns c ON o.id = c.id
JOIN systypes t ON c.xtype = t.xtype
WHERE o.xtype = 'U'
AND (t.name like '%char' or t.name like '%text')

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Great,

Thanks a lot, with the query I can create the new script much and much
easier.

I replaced the o.name with the tables which I new they had the problem
and now I have have all columns to replace the values.

Thanks a lot.

Regards, Bob

Erland Sommarskog schreef:

Quote:

Originally Posted by

BF (bob@.faessen.net) writes:

Quote:

Originally Posted by

I have some tables where I import data in, lots of field have gotten a
NULL value which the application can not handle.

Now can I replace each NULL value with '' in a columns with:
update <tableset [<column>] = '' where [<column>] IS NULL

But because there are lots of columns this is pretty much work, also
there are multiple tables.

Is there an easy way to replace all NULL values in all columns in a
table?


>
First of all, that operation would only be possible with columns
that hold character data. For numeric and datetime columns there
is rarely any good replacement for NULL values. So, unless, your
database only has nullable character columns, you need to fix the
application to handle NULL values anyway.
>
No, there is no direct function for setting many columns to NULL. You
need to have an UPDATE statement for each table, and one that lists
all columns that should be set to NULL. The good news is that you
can generate the statements:
>
SELECT 'UPDATE ' + o.name + ' SET ' + c.name + ' = '''' WHERE ' +
c.name + ' IS NULL'
FROM sysobjects o
JOIN syscolumns c ON o.id = c.id
JOIN systypes t ON c.xtype = t.xtype
WHERE o.xtype = 'U'
AND (t.name like '%char' or t.name like '%text')
>
>
>
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
>
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Tuesday, March 20, 2012

Repetitive SQL Tasks

Thanks in advance.

I am new to sql script area.

Below is my ugly program.

Help, please)

There should be a better code to handle these repetitive tasks.

/* DECLARE Part Omitted */

SET @.Job_Code_A = '(Job_Type = ''A'')';

SET @.Job_Code_B = '(Job_Type = ''B'')';

.... And so on

SET @.SQL1 = 'Select Count(*) FROM tblWorkorders WHERE' + @.Job_Code_A;

SET @.SQL2 = 'Select Count(*) FROM tblWorkorders WHERE' + @.Job_Code_B;

..... And so on

EXEC (@.SQL1);

EXEC (@.SQL2);

..... And so on

Why do you need dynamic SQL? You can do:

select o.Job_Type, count(*)

from tblWorkOrders as o

where o.Job_Type in ('A', 'B')

group by o.Job_Type;

|||

Thank you for reply.

I need to use three-level depth loops from outer conditions.

Friday, March 9, 2012

Repari tools

Is there any repairtools in SQL Server 2000 that can handle unreadable files?
Cheers & TIA,
Niclas
Nick,
As per the other post:
http://www.officerecovery.com/mssql/
Above is the only one I know of...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"TheNick" <TheNick@.discussions.microsoft.com> wrote in message
news:9B82ADE4-BCCA-40AB-91E4-373A1CC5B52B@.microsoft.com...
> Is there any repairtools in SQL Server 2000 that can handle unreadable files?
> Cheers & TIA,
> Niclas
|||Thanx! Sure gonna download and try!
//Niclas
"Tibor Karaszi" wrote:

> Nick,
> As per the other post:
> http://www.officerecovery.com/mssql/
> Above is the only one I know of...
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "TheNick" <TheNick@.discussions.microsoft.com> wrote in message
> news:9B82ADE4-BCCA-40AB-91E4-373A1CC5B52B@.microsoft.com...
>
>
|||Niclas,
See if any are here:
http://www.sysinternals.com/ntw2k/utilities.shtml
Dinesh
SQL Server MVP
--
SQL Server FAQ at
http://www.tkdinesh.com
"TheNick" <TheNick@.discussions.microsoft.com> wrote in message
news:9B82ADE4-BCCA-40AB-91E4-373A1CC5B52B@.microsoft.com...
> Is there any repairtools in SQL Server 2000 that can handle unreadable
files?
> Cheers & TIA,
> Niclas

Repari tools

Is there any repairtools in SQL Server 2000 that can handle unreadable files
?
Cheers & TIA,
NiclasNick,
As per the other post:
http://www.officerecovery.com/mssql/
Above is the only one I know of...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"TheNick" <TheNick@.discussions.microsoft.com> wrote in message
news:9B82ADE4-BCCA-40AB-91E4-373A1CC5B52B@.microsoft.com...
> Is there any repairtools in SQL Server 2000 that can handle unreadable fil
es?
> Cheers & TIA,
> Niclas|||Thanx! Sure gonna download and try!
//Niclas
"Tibor Karaszi" wrote:

> Nick,
> As per the other post:
> http://www.officerecovery.com/mssql/
> Above is the only one I know of...
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "TheNick" <TheNick@.discussions.microsoft.com> wrote in message
> news:9B82ADE4-BCCA-40AB-91E4-373A1CC5B52B@.microsoft.com...
>
>|||Niclas,
See if any are here:
http://www.sysinternals.com/ntw2k/utilities.shtml
Dinesh
SQL Server MVP
--
--
SQL Server FAQ at
http://www.tkdinesh.com
"TheNick" <TheNick@.discussions.microsoft.com> wrote in message
news:9B82ADE4-BCCA-40AB-91E4-373A1CC5B52B@.microsoft.com...
> Is there any repairtools in SQL Server 2000 that can handle unreadable
files?
> Cheers & TIA,
> Niclas