Showing posts with label empty. Show all posts
Showing posts with label empty. Show all posts

Wednesday, March 28, 2012

Replacing blank records with text

Hi there,

I'm in a bit of a jam here and will appreciate any help.

I need the SQL code to replace a record if the record is empty.

For instance, I have about 7 columns containing over 40K records. In the firstname field, some records are blank. I need to replace all the blank firstname fields with this: 'now invalid' (without the quotes)

What would be the best way to achieve this?

Thanks

newbie:oUpdate [YourTable]
set FirstName = isnull(FirstName, 'now invalid'),
LastName = isnull(LastName, 'now invalid'),
etc...|||Thanks alot for your response blindman.

I didnt add that for the blank firstname fields I want to replace, all their lastnames contain valid data which I do not want to replace. all the lastnames are 'Design Professional'

So i modified the sql like this:

Update [My Table]
set FirstName = isnull(FirstName, 'Now Invalid')
where lastname = 'Design Professional'

I think the above query has a problem because it generated an error saying:
saying 'wrong number of arguements used with function in query expression isnull(FirstName, 'now invalid')

can you help with this?

Thank you very much|||Thanks alot for your response blindman.

I didnt add that for the blank firstname fields I want to replace, all their lastnames contain valid data which I do not want to replace. all the lastnames are 'Design Professional'

So i modified the sql like this:

Update [My Table]
set FirstName = isnull(FirstName, 'Now Invalid')
where lastname = 'Design Professional'

I think the above query has a problem because it generated an error saying:
saying 'wrong number of arguements used with function in query expression isnull(FirstName, 'now invalid')

can you help with this?

Thank you very much|||Are you using SQL Server or MS Access? The isnull() function is different in TSQL than in Access VB.|||If you are in Access look up NZ in help. Same thing as the T-SQL ISNull basically. IsNull in Access means something totally different to IsNull in T-SQL.|||Thanks a lot for the suggestions. I'm using MS Access.

Monday, March 26, 2012

replace value of empty element?

Hi,
In SQL 2005, I have a XML with an one element with an empty value like
so:
<root>
<test></test>
</root>
When I try and replace the value of (/root/test) it does not seem to
work:
declare @.xml xml
set @.xml = '<root><test></test></root>'
set @.xml.modify('replace value of (/root/test/text())[1] with "test
new value"')
select @.xml
returns this:
<root>
<test></test>
</root>
If however the element has a value already like so:
<root>
<test>some old value here already</test>
</root>
and I re-rerun the statement above:
declare @.xml xml
set @.xml = '<root><test></test></root>'
set @.xml.modify('replace value of (/root/test/text())[1] with "test
new value"')
select @.xml
returns this:
<root>
<test>test new value</test>
</root>
Is something wrong there?In the first case, when you use expression (/root/test/text())[1] you refer
to
something that doesn't exist (there is no text node under element test) and
therefore nothing gets updated.
What you need to do is insert a new text node inder element "test" like this
declare @.xml xml
set @.xml = '<root><test></test></root>'
set @.xml.modify('insert text{"test new value"} as first into (/root/test)[1]
')
select @.xml
I hope this helps.
Denis Ruckebusch
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
<doctorphan@.gmail.com> wrote in message
news:1177013246.440921.259810@.d57g2000hsg.googlegroups.com...
> Hi,
> In SQL 2005, I have a XML with an one element with an empty value like
> so:
> <root>
> <test></test>
> </root>
> When I try and replace the value of (/root/test) it does not seem to
> work:
> declare @.xml xml
> set @.xml = '<root><test></test></root>'
> set @.xml.modify('replace value of (/root/test/text())[1] with "test
> new value"')
> select @.xml
> returns this:
> <root>
> <test></test>
> </root>
>
> If however the element has a value already like so:
> <root>
> <test>some old value here already</test>
> </root>
> and I re-rerun the statement above:
> declare @.xml xml
> set @.xml = '<root><test></test></root>'
> set @.xml.modify('replace value of (/root/test/text())[1] with "test
> new value"')
> select @.xml
> returns this:
> <root>
> <test>test new value</test>
> </root>
> Is something wrong there?
>|||On Apr 19, 6:07 pm, "Denis Ruckebusch [MSFT]"
<denis...@.online.microsoft.com> wrote:
> In the first case, when you use expression (/root/test/text())[1] you refe
r to
> something that doesn't exist (there is no text node under element test) an
d
> therefore nothing gets updated.
> What you need to do is insert a new text node inder element "test" like th
is
> declare @.xml xml
> set @.xml = '<root><test></test></root>'
> set @.xml.modify('insert text{"test new value"} as first into (/root/test)[
1] ')
> select @.xml
> I hope this helps.
> Denis Ruckebusch
> --
> This posting is provided "AS IS" with no warranties, and confers no rights
.
> Use of included script samples are subject to the terms specified athttp:/
/www.microsoft.com/info/cpyright.htm
> <doctorp...@.gmail.com> wrote in message
> news:1177013246.440921.259810@.d57g2000hsg.googlegroups.com...
>
>
>
>
>
>
>
>
>
>
>
>
>
> - Show quoted text -
So I guess there is no one built-in statement that will work in both
scenarios? ie. One that will insert the text if one doesn't exist, and
one that will replace the text if it already exists. I guess I will
have to write a function?|||You should probably use the XML datatype's exist() method to determin if the
re
is already a text node or not and then perform the proper action depending
on
what case you're in.
Denis Ruckebusch
http://blogs.msdn.com/denisruc
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
<doctorphan@.gmail.com> wrote in message
news:1177078051.463036.85330@.n76g2000hsh.googlegroups.com...
> On Apr 19, 6:07 pm, "Denis Ruckebusch [MSFT]"
> <denis...@.online.microsoft.com> wrote:
>
> So I guess there is no one built-in statement that will work in both
> scenarios? ie. One that will insert the text if one doesn't exist, and
> one that will replace the text if it already exists. I guess I will
> have to write a function?
>|||Try the following:
declare @.xml xml
set @.xml = '<root><test></test></root>'
set @.xml.modify('replace value of (/root/test/text())[1] with "test
new value"')
set @.xml.modify('insert text{ "test
new value"} into (/root/test[not(text())])[1]')
select @.xml
Best regards
Michael
<doctorphan@.gmail.com> wrote in message
news:1177078051.463036.85330@.n76g2000hsh.googlegroups.com...
> On Apr 19, 6:07 pm, "Denis Ruckebusch [MSFT]"
> <denis...@.online.microsoft.com> wrote:
>
> So I guess there is no one built-in statement that will work in both
> scenarios? ie. One that will insert the text if one doesn't exist, and
> one that will replace the text if it already exists. I guess I will
> have to write a function?
>

replace value of empty element?

Hi,
In SQL 2005, I have a XML with an one element with an empty value like
so:
<root>
<test></test>
</root>
When I try and replace the value of (/root/test) it does not seem to
work:
declare @.xml xml
set @.xml = '<root><test></test></root>'
set @.xml.modify('replace value of (/root/test/text())[1] with "test
new value"')
select @.xml
returns this:
<root>
<test></test>
</root>
If however the element has a value already like so:
<root>
<test>some old value here already</test>
</root>
and I re-rerun the statement above:
declare @.xml xml
set @.xml = '<root><test></test></root>'
set @.xml.modify('replace value of (/root/test/text())[1] with "test
new value"')
select @.xml
returns this:
<root>
<test>test new value</test>
</root>
Is something wrong there?
In the first case, when you use expression (/root/test/text())[1] you refer to
something that doesn't exist (there is no text node under element test) and
therefore nothing gets updated.
What you need to do is insert a new text node inder element "test" like this
declare @.xml xml
set @.xml = '<root><test></test></root>'
set @.xml.modify('insert text{"test new value"} as first into (/root/test)[1] ')
select @.xml
I hope this helps.
Denis Ruckebusch
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
<doctorphan@.gmail.com> wrote in message
news:1177013246.440921.259810@.d57g2000hsg.googlegr oups.com...
> Hi,
> In SQL 2005, I have a XML with an one element with an empty value like
> so:
> <root>
> <test></test>
> </root>
> When I try and replace the value of (/root/test) it does not seem to
> work:
> declare @.xml xml
> set @.xml = '<root><test></test></root>'
> set @.xml.modify('replace value of (/root/test/text())[1] with "test
> new value"')
> select @.xml
> returns this:
> <root>
> <test></test>
> </root>
>
> If however the element has a value already like so:
> <root>
> <test>some old value here already</test>
> </root>
> and I re-rerun the statement above:
> declare @.xml xml
> set @.xml = '<root><test></test></root>'
> set @.xml.modify('replace value of (/root/test/text())[1] with "test
> new value"')
> select @.xml
> returns this:
> <root>
> <test>test new value</test>
> </root>
> Is something wrong there?
>
|||On Apr 19, 6:07 pm, "Denis Ruckebusch [MSFT]"
<denis...@.online.microsoft.com> wrote:
> In the first case, when you use expression (/root/test/text())[1] you refer to
> something that doesn't exist (there is no text node under element test) and
> therefore nothing gets updated.
> What you need to do is insert a new text node inder element "test" like this
> declare @.xml xml
> set @.xml = '<root><test></test></root>'
> set @.xml.modify('insert text{"test new value"} as first into (/root/test)[1] ')
> select @.xml
> I hope this helps.
> Denis Ruckebusch
> --
> This posting is provided "AS IS" with no warranties, and confers no rights.
> Use of included script samples are subject to the terms specified athttp://www.microsoft.com/info/cpyright.htm
> <doctorp...@.gmail.com> wrote in message
> news:1177013246.440921.259810@.d57g2000hsg.googlegr oups.com...
>
>
>
>
>
>
>
> - Show quoted text -
So I guess there is no one built-in statement that will work in both
scenarios? ie. One that will insert the text if one doesn't exist, and
one that will replace the text if it already exists. I guess I will
have to write a function?
|||You should probably use the XML datatype's exist() method to determin if there
is already a text node or not and then perform the proper action depending on
what case you're in.
Denis Ruckebusch
http://blogs.msdn.com/denisruc
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
<doctorphan@.gmail.com> wrote in message
news:1177078051.463036.85330@.n76g2000hsh.googlegro ups.com...
> On Apr 19, 6:07 pm, "Denis Ruckebusch [MSFT]"
> <denis...@.online.microsoft.com> wrote:
>
> So I guess there is no one built-in statement that will work in both
> scenarios? ie. One that will insert the text if one doesn't exist, and
> one that will replace the text if it already exists. I guess I will
> have to write a function?
>
|||Try the following:
declare @.xml xml
set @.xml = '<root><test></test></root>'
set @.xml.modify('replace value of (/root/test/text())[1] with "test
new value"')
set @.xml.modify('insert text{ "test
new value"} into (/root/test[not(text())])[1]')
select @.xml
Best regards
Michael
<doctorphan@.gmail.com> wrote in message
news:1177078051.463036.85330@.n76g2000hsh.googlegro ups.com...
> On Apr 19, 6:07 pm, "Denis Ruckebusch [MSFT]"
> <denis...@.online.microsoft.com> wrote:
>
> So I guess there is no one built-in statement that will work in both
> scenarios? ie. One that will insert the text if one doesn't exist, and
> one that will replace the text if it already exists. I guess I will
> have to write a function?
>
sql