Showing posts with label spaces. Show all posts
Showing posts with label spaces. Show all posts

Friday, March 23, 2012

Replace Multiple Spaces with One Space?

I need to map several columns of data from one database to another
where the data contains multiple spaces (once occurance of a variable
number or spaces) that I need to replace with a single space. What
would be the most efficient way to do this? I am using SQL2K. I was
thinking a function since I know of no single Transact-SQL command
that can accomplish this task."Joe Cool" <joecool@.home.netwrote in message
news:5pb7t2dkt37mgh0cqnm3f9luuu1rvhglkm@.4ax.com...

Quote:

Originally Posted by

>I need to map several columns of data from one database to another
where the data contains multiple spaces (once occurance of a variable
number or spaces) that I need to replace with a single space. What
would be the most efficient way to do this? I am using SQL2K. I was
thinking a function since I know of no single Transact-SQL command
that can accomplish this task.


DECLARE @.FieldName varchar(5000)
SET @.FieldName = ' Reduce any number of' + SPACE(512) +
'spaces up to 512 in
a row to a single space
'

--You can reduce layers of REPLACE depending how many extra spaces you
expect.

SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RTRIM(LTRI M(@.FieldName)),
SPACE(16), ' '), SPACE(8), ' '), SPACE(4), ' '), SPACE(2), ' '), SPACE(2), '
')|||On Wed, 14 Feb 2007 20:47:37 -0600, "Russ Rose" <russrose@.hotmail.com>
wrote:

Quote:

Originally Posted by

>
>"Joe Cool" <joecool@.home.netwrote in message
>news:5pb7t2dkt37mgh0cqnm3f9luuu1rvhglkm@.4ax.com...

Quote:

Originally Posted by

>>I need to map several columns of data from one database to another
>where the data contains multiple spaces (once occurance of a variable
>number or spaces) that I need to replace with a single space. What
>would be the most efficient way to do this? I am using SQL2K. I was
>thinking a function since I know of no single Transact-SQL command
>that can accomplish this task.


>
>DECLARE @.FieldName varchar(5000)
>SET @.FieldName = ' Reduce any number of' + SPACE(512) +
>'spaces up to 512 in
>a row to a single space
>'
>
>--You can reduce layers of REPLACE depending how many extra spaces you
>expect.
>
>SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RTRIM(LTRI M(@.FieldName)),
>SPACE(16), ' '), SPACE(8), ' '), SPACE(4), ' '), SPACE(2), ' '), SPACE(2), '
>')
>


I believe you misunderstood my question. Here are some sample values:

"SMITH JR" (5 spaces)
"JONES JR" (7 spaces)

Desired result"

"SMITH JR"
"JONES JR"|||Joe Cool wrote:

Quote:

Originally Posted by

On Wed, 14 Feb 2007 20:47:37 -0600, "Russ Rose" <russrose@.hotmail.com>
wrote:
>

Quote:

Originally Posted by

>"Joe Cool" <joecool@.home.netwrote in message
>news:5pb7t2dkt37mgh0cqnm3f9luuu1rvhglkm@.4ax.com...

Quote:

Originally Posted by

>>I need to map several columns of data from one database to another
>>where the data contains multiple spaces (once occurance of a variable
>>number or spaces) that I need to replace with a single space. What
>>would be the most efficient way to do this? I am using SQL2K. I was
>>thinking a function since I know of no single Transact-SQL command
>>that can accomplish this task.


>DECLARE @.FieldName varchar(5000)
>SET @.FieldName = ' Reduce any number of' + SPACE(512) +
>'spaces up to 512 in
>a row to a single space
>'
>>
>--You can reduce layers of REPLACE depending how many extra spaces you
>expect.
>>
>SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RTRIM(LTRI M(@.FieldName)),
>SPACE(16), ' '), SPACE(8), ' '), SPACE(4), ' '), SPACE(2), ' '), SPACE(2), '
>')
>>


>
I believe you misunderstood my question. Here are some sample values:
>
"SMITH JR" (5 spaces)
"JONES JR" (7 spaces)
>
Desired result"
>
"SMITH JR"
"JONES JR"


Looks to me like his answer produces the desired result. What problem
do you see with it?|||"Ed Murphy" <emurphy42@.socal.rr.comwrote in message
news:45d51798$0$28101$4c368faf@.roadrunner.com...

Quote:

Originally Posted by

Joe Cool wrote:
>

Quote:

Originally Posted by

>On Wed, 14 Feb 2007 20:47:37 -0600, "Russ Rose" <russrose@.hotmail.com>
>wrote:
>>

Quote:

Originally Posted by

>>"Joe Cool" <joecool@.home.netwrote in message
>>news:5pb7t2dkt37mgh0cqnm3f9luuu1rvhglkm@.4ax.com...
>>>I need to map several columns of data from one database to another
>>>where the data contains multiple spaces (once occurance of a variable
>>>number or spaces) that I need to replace with a single space. What
>>>would be the most efficient way to do this? I am using SQL2K. I was
>>>thinking a function since I know of no single Transact-SQL command
>>>that can accomplish this task.
>>DECLARE @.FieldName varchar(5000)
>>SET @.FieldName = ' Reduce any number of' + SPACE(512) +
>>'spaces up to 512 in a
>>row to a single space '
>>>
>>--You can reduce layers of REPLACE depending how many extra spaces you
>>expect.
>>>
>>SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RTRIM(LTRI M(@.FieldName)),
>>SPACE(16), ' '), SPACE(8), ' '), SPACE(4), ' '), SPACE(2), ' '),
>>SPACE(2), ' ')


>>
>I believe you misunderstood my question. Here are some sample values:
>>
>"SMITH JR" (5 spaces)
>"JONES JR" (7 spaces)
>>
>Desired result"
>>
>"SMITH JR"
>"JONES JR"


>
Looks to me like his answer produces the desired result. What problem
do you see with it?


It was a bit of overkill... maybe this will be closer to what you want.

CREATE FUNCTION dbo.SingleSpace(@.str varchar(8000))

RETURNS varchar(8000)

AS
--Usage
--SELECT dbo.SingleSpace('Function replaces any and all spaces up
to 16 in a row with a single space ')

BEGIN
DECLARE @.s varchar(8000)
SELECT @.s = REPLACE(REPLACE(REPLACE(RTRIM(LTRIM(@.str)), SPACE(4), ' '),
SPACE(2), ' '), SPACE(2), ' ')
RETURN @.s
END|||Russ Rose wrote:

Quote:

Originally Posted by

"Ed Murphy" <emurphy42@.socal.rr.comwrote in message
news:45d51798$0$28101$4c368faf@.roadrunner.com...

Quote:

Originally Posted by

>Joe Cool wrote:
>>

Quote:

Originally Posted by

>>On Wed, 14 Feb 2007 20:47:37 -0600, "Russ Rose" <russrose@.hotmail.com>
>>wrote:
>>>
>>>"Joe Cool" <joecool@.home.netwrote in message
>>>news:5pb7t2dkt37mgh0cqnm3f9luuu1rvhglkm@.4ax.com...
>>>>I need to map several columns of data from one database to another
>>>>where the data contains multiple spaces (once occurance of a variable
>>>>number or spaces) that I need to replace with a single space. What
>>>>would be the most efficient way to do this? I am using SQL2K. I was
>>>>thinking a function since I know of no single Transact-SQL command
>>>>that can accomplish this task.
>>>DECLARE @.FieldName varchar(5000)
>>>SET @.FieldName = ' Reduce any number of' + SPACE(512) +
>>>'spaces up to 512 in a
>>>row to a single space '
>>>>
>>>--You can reduce layers of REPLACE depending how many extra spaces you
>>>expect.
>>>>
>>>SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RTRIM(LTRI M(@.FieldName)),
>>>SPACE(16), ' '), SPACE(8), ' '), SPACE(4), ' '), SPACE(2), ' '),
>>>SPACE(2), ' ')
>>I believe you misunderstood my question. Here are some sample values:
>>>
>>"SMITH JR" (5 spaces)
>>"JONES JR" (7 spaces)
>>>
>>Desired result"
>>>
>>"SMITH JR"
>>"JONES JR"


>Looks to me like his answer produces the desired result. What problem
>do you see with it?


>
It was a bit of overkill... maybe this will be closer to what you want.
>
>
CREATE FUNCTION dbo.SingleSpace(@.str varchar(8000))
>
RETURNS varchar(8000)
>
AS
--Usage
--SELECT dbo.SingleSpace('Function replaces any and all spaces up
to 16 in a row with a single space ')
>
BEGIN
DECLARE @.s varchar(8000)
SELECT @.s = REPLACE(REPLACE(REPLACE(RTRIM(LTRIM(@.str)), SPACE(4), ' '),
SPACE(2), ' '), SPACE(2), ' ')
RETURN @.s
END


Doesn't work on 11, 14, or 15 spaces.

Adding an extra REPLACE(..., SPACE(2), ' ') wrapper will fix it, and
extend it to runs up to 26.

Adding an inner 8->1 on top of that extends to 167; 16->1 on top of that
extends to 2447; 32->1 on top of that extends to 77343, more than enough
unless you're messing with TEXT (for which REPLACE doesn't work at all,
IIRC) or VARCHAR(MAX).|||Do an UPDATE statement with nested REPLACE() functions to change (n)
spaces to 1 space:

UPDATE Foobar
SET mystring = REPLACE (SPACE(2), SPACE(1) ' ,
...
REPLACE (SPACE(<<fib(n)>>), SPACE(1),
mystring)
.. )));

The optimal pattern for the substitutions is a Fibbonnaci series with
the longest string of spaces in the innermost invocation. You have to
pick the right number based on the length of the column. Working out
the math is fun, so enjoy.|||"Russ Rose" <russrose@.hotmail.comwrote:

Quote:

Originally Posted by

>


How did you guys miss the recursion boat?

Create function dbo.udf_CondenseSpaces (@.str varchar(8000))

Returns varchar(8000)
AS

BEGIN
Declare @.s varchar(8000)

Set @.s = replace(@.str, ' ', ' ')

if charindex(' ', @.s) 0

set @.s = dbo.udf_CondenseSpaces(@.s)

return @.s
END|||also instead of calling a function many times
create a function or a proct
Sample incomming string

declare @.somestring varchar(100)
set @.somestring = 'aaa bbb cccc eee fff f gggg h i aa '
--sample guts of function / proc
while charindex(' ',@.somestring)>0 begin
set @.Somestring = replace(@.somestring,' ',' ')
end
print @.somestring
--output aaa bbb cccc eee fff f gggg h i aa

"Blackburn" <blackburn@.2centsediting.mypants.comwrote in message
news:1192727141_459@.sp12lax.superfeed.net...

Quote:

Originally Posted by

"Russ Rose" <russrose@.hotmail.comwrote:

Quote:

Originally Posted by

>>


>
>
How did you guys miss the recursion boat?
>
Create function dbo.udf_CondenseSpaces (@.str varchar(8000))
>
Returns varchar(8000)
AS
>
BEGIN
Declare @.s varchar(8000)
>
Set @.s = replace(@.str, ' ', ' ')
>
if charindex(' ', @.s) 0
>
set @.s = dbo.udf_CondenseSpaces(@.s)
>
return @.s
END

Replace multiple spaces in varchar

How can I replace multiple spaces in a varchar string?
Given 'ABC DEF GHI' I want to return 'ABC DEF GHI'
I want to find all occurrences of 2 or more spaces and replace with 1 space.
My reporting application can't handle this.
Thankstry this
replace( 'ABC DEF GHI', ' ',' ')
"Terri" <terri@.cybernets.com> wrote in message
news:d3ot2k$p63$1@.reader2.nmix.net...
> How can I replace multiple spaces in a varchar string?
> Given 'ABC DEF GHI' I want to return 'ABC DEF GHI'
> I want to find all occurrences of 2 or more spaces and replace with 1
> space.
> My reporting application can't handle this.
> Thanks
>|||--One possibility:
SELECT REPLACE(REPLACE('ABC DEF GHI JKL', ' ', ' '), ' ', ' ')
Handles up to 4 spaces
SELECT REPLACE(REPLACE(REPLACE('ABC DEF GHI JKL', ' ', '
'), ' ', ' '), ' ', ' ')
Handles up to 8 spaces
I know this works on my setup but maybe an expert will see a problem with
this.
Another (probably slower but will handle any number of spaces)
would be to build a function to handle this and integrate this logic:
DECLARE @.str VARCHAR(50)
SET @.str = 'ABC DEF GHI JKL'
WHILE LEN(@.str) > LEN(REPLACE(@.str, ' ', ' '))
SET @.str = REPLACE(@.str, ' ', ' ')
SELECT @.str
"Terri" <terri@.cybernets.com> wrote in message
news:d3ot2k$p63$1@.reader2.nmix.net...
> How can I replace multiple spaces in a varchar string?
> Given 'ABC DEF GHI' I want to return 'ABC DEF GHI'
> I want to find all occurrences of 2 or more spaces and replace with 1
> space.
> My reporting application can't handle this.
> Thanks
>|||"Terri" <terri@.cybernets.com> wrote in message
news:d3ot2k$p63$1@.reader2.nmix.net...
> How can I replace multiple spaces in a varchar string?
> Given 'ABC DEF GHI' I want to return 'ABC DEF GHI'
> I want to find all occurrences of 2 or more spaces and replace with 1
> space.
> My reporting application can't handle this.
> Thanks
>
SELECT REPLACE(REPLACE(REPLACE('ABC DEF GHI',' ','[ ]'),'][ ',''),'[ ]','
')|||The short answer is to nest REPLACE() functions inside each other.
This problem came up years ago in a Newsgroup, but we then asked the
question; What is the best series of replacement sizes to use on a
string of length(n)? For example, I could use powers of 2; reduce 8
spaces to 1, then 4 spaces to 1, and finally 2 spaces to 1, which does
not work unless you run it twice). I guessed that a Fibbonacci series
might be best but could not prove it. There were some other guesses,
but one guy worked out a list by brute force.
Anyone know where to find this? Since newbies constantly make
everything in the universe VARCHAR(50), that sounds like a good upper
value to use for experiments.|||Never mind; I found it! It is over in comp.database.theory as
"Squeezing spaces out of a string" and there is some code posted for
the solutions.|||http://tinyurl.com/cx7gt
HTH,
Gert-Jan
Terri wrote:
> How can I replace multiple spaces in a varchar string?
> Given 'ABC DEF GHI' I want to return 'ABC DEF GHI'
> I want to find all occurrences of 2 or more spaces and replace with 1 spac
e.
> My reporting application can't handle this.
> Thanks|||"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1113589959.961601.150610@.f14g2000cwb.googlegroups.com...
> Never mind; I found it! It is over in comp.database.theory as
> "Squeezing spaces out of a string" and there is some code posted for
> the solutions.
>
Interesting thread. Here's a rewrite of my earlier response to take into
account overflow:
DECLARE @.s VARCHAR(8000)
SET @.s = 'a b c d' + SPACE(7989) + 'e'
SELECT
REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(SUBSTRING(@.s,000
1,2000),' ','[ ]'),'][ ',''),'[ ]','
') +
REPLACE(REPLACE(REPLACE(SUBSTRING(@.s,200
1,2000),' ','[ ]'),'][ ',''),'[ ]','
') +
REPLACE(REPLACE(REPLACE(SUBSTRING(@.s,400
1,2000),' ','[ ]'),'][ ',''),'[ ]','
') +
REPLACE(REPLACE(REPLACE(SUBSTRING(@.s,600
1,2000),' ','[ ]'),'][ ',''),'[ ]','
'),
SPACE(4),' '),SPACE(3),' '),SPACE(2),' ')|||Hmm, why use 15 replaces when you only need 6?
See http://tinyurl.com/cx7gt
Gert-Jan
Chris Hohmann wrote:
> "--CELKO--" <jcelko212@.earthlink.net> wrote in message
> news:1113589959.961601.150610@.f14g2000cwb.googlegroups.com...
> Interesting thread. Here's a rewrite of my earlier response to take into
> account overflow:
> DECLARE @.s VARCHAR(8000)
> SET @.s = 'a b c d' + SPACE(7989) + 'e'
> SELECT
> REPLACE(REPLACE(REPLACE(
> REPLACE(REPLACE(REPLACE(SUBSTRING(@.s,000
1,2000),' ','[ ]'),'][ ',''),'[ ]'
,'
> ') +
> REPLACE(REPLACE(REPLACE(SUBSTRING(@.s,200
1,2000),' ','[ ]'),'][ ',''),'[ ]'
,'
> ') +
> REPLACE(REPLACE(REPLACE(SUBSTRING(@.s,400
1,2000),' ','[ ]'),'][ ',''),'[ ]'
,'
> ') +
> REPLACE(REPLACE(REPLACE(SUBSTRING(@.s,600
1,2000),' ','[ ]'),'][ ',''),'[ ]'
,'
> '),
> SPACE(4),' '),SPACE(3),' '),SPACE(2),' ')|||"Gert-Jan Strik" <sorry@.toomuchspamalready.nl> wrote in message
news:426033FB.8CBC2FC0@.toomuchspamalready.nl...
> Hmm, why use 15 replaces when you only need 6?
> See http://tinyurl.com/cx7gt
> Gert-Jan
>
> Chris Hohmann wrote:
The encoding from ' ' to '[ ]' has the potential to increase the size of the
string threefold. The maximum allowable length for stings is 8000. Since
8000 is not evenly divisible by 3, the string needs to be broken into at
least four parts. Once multiple spaces have been collapsed in each of the
four parts, the resulting strings are concatenated together. However, it is
possible that one part ends with a space and the following part begins with
one. The limit case for this scenario would be a string of 8000 spaces. In
which case four spaces would be concatenated together at the end. To account
for this case, I perform the three additional replace calls after the parts
have been concatenated. You could actually get away with two replace calls
at the end instead of three and eliminate the calls to SPACE. Something like
REPLACE(REPLACE
.
.
.
' ',' '),' ',' ')
In which case there would only be 14 replace calls.
HTH
-Chris Hohmann

Wednesday, March 21, 2012

replace instance of multiple char(32)

I have several fileds in a sql table which holds some text and a hell of a lot of white spaces (char(32)) which is causing some problems when I show the data on the front-end

What I want to do is use sql replace function to replace the char(32). However, because there is text I can't do a simple replace as the text will become just one word !

Can anyone tell me how to loop through the field to find the char(32), where the char(32) is greater than say 5 sapces and then replace this with just one single space

E.g. a field in a table contains the following :

"my text field and loads of spaces then some more text with loads of spaces "

I want to replace the char(32) with just one space so it looks like this :
"my text filed and loads of spaces then some more text with loads of spaces"

Thanks for your helpI'd try using:SELECT Replace('Target string with spaces', ' ', ' ')-PatP|||I had to run the replace twice...

declare @.Targetstring varchar(50)
set @.Targetstring = '1 2 3 4 5 6'

select @.TargetString
select Replace(@.TargetString, ' ', ' ') --Not quite there yet...
select Replace(Replace(@.TargetString, ' ', ' '), ' ', ' ') --Lookin' good!|||Replace is kinda like that... It only makes one pass through the string on its own. It can be coerced into doing the right thing, if you have a big enough stick!

-PatP

Replace Apostrophe in SQL

replace(lastname," ' '",'"x"') (spaces for clarity only)

Result:

Invalid column name ' ' '.

How do I get around the invalid column name?

Thanks in advance for your assistance!

Just double the apostrophy. Example, replace(lastname," ' ' '",'"x"') (spaces for clarity only)|||tried that also...

Invalid column name '''''

|||using SQL in vb6|||replace(lastname, ' ' ' ', 'x' ) (spaces for clarity only)|||

Seems like the error is most likely because you are using double quotes for a string. SQL Server has a special usage for double quotes. That is to identify object names -not enclose string values.

So if you are attempting to replace two single embedded quotes with a character [x], try this:

Code Snippet


DECLARE @.LastName varchar(20)
SET @.LastName = 'O''''Reilly'
SELECT replace( @.LastName, '''''','x')

If I have misread your intentions, please clarify what you are attempting to replace, and with what value...

|||

You can use either escape sequence (as Arnie suggested)

or

The QUOTED_IDENTIFIER settings, as follow as

Code Snippet

SETQUOTED_IDENTIFIEROFF

Selectreplace("O'Reilly","'",'"x"')

|||

If you're using VB snytax string.replace...double quotes around the "x" is sufficient. You don't want to replace the apstrophe with 'x' just x

If you can, you want to trim using vb on the form before it gets to SQL and I think that's what you want to do here.

Adamus

Replace Apostrophe in SQL

replace(lastname," ' '",'"x"') (spaces for clarity only)

Result:

Invalid column name ' ' '.

How do I get around the invalid column name?

Thanks in advance for your assistance!

Just double the apostrophy. Example, replace(lastname," ' ' '",'"x"') (spaces for clarity only)|||tried that also...

Invalid column name '''''

|||using SQL in vb6|||replace(lastname, ' ' ' ', 'x' ) (spaces for clarity only)|||

Seems like the error is most likely because you are using double quotes for a string. SQL Server has a special usage for double quotes. That is to identify object names -not enclose string values.

So if you are attempting to replace two single embedded quotes with a character [x], try this:

Code Snippet


DECLARE @.LastName varchar(20)
SET @.LastName = 'O''''Reilly'
SELECT replace( @.LastName, '''''','x')

If I have misread your intentions, please clarify what you are attempting to replace, and with what value...

|||

You can use either escape sequence (as Arnie suggested)

or

The QUOTED_IDENTIFIER settings, as follow as

Code Snippet

SETQUOTED_IDENTIFIEROFF

Selectreplace("O'Reilly","'",'"x"')

|||

If you're using VB snytax string.replace...double quotes around the "x" is sufficient. You don't want to replace the apstrophe with 'x' just x

If you can, you want to trim using vb on the form before it gets to SQL and I think that's what you want to do here.

Adamus

Tuesday, March 20, 2012

Replace '' with a 0 ......... replace nothing with a 0

Hi guys
Im sure this is easy but I have a query to calculate spaces on a
booking system and need to replace a blank ie no bookings yet with a 0
so that i can add the numbers up to get a total.
Trouble is its not a NULL value and when i try
REPLACE([fieldname],'','0') it just doesnt work.
I can get round it but rather do it properly
MattieHi
When you say "it does not work" , what does that mean? Is an error message,
is it not given you a right result ?
SELECT REPLACE(column ,' ' ,'0')
"MattieG" <mattiegriffin@.hotmail.co.uk> wrote in message
news:1156689093.555748.280040@.75g2000cwc.googlegroups.com...
> Hi guys
> Im sure this is easy but I have a query to calculate spaces on a
> booking system and need to replace a blank ie no bookings yet with a 0
> so that i can add the numbers up to get a total.
> Trouble is its not a NULL value and when i try
> REPLACE([fieldname],'','0') it just doesnt work.
> I can get round it but rather do it properly
> Mattie
>|||Hi
Basically the query still have blanks in that particular field, no
error message.
SELECT REPLACE(column ,' ' ,'0') ->only works if there is actually a
space in the field.
Im thinkin i might have to use ISNUM or somet like that.
Uri Dimant wrote:[vbcol=seagreen]
> Hi
> When you say "it does not work" , what does that mean? Is an error messag
e,
> is it not given you a right result ?
> SELECT REPLACE(column ,' ' ,'0')
>
> "MattieG" <mattiegriffin@.hotmail.co.uk> wrote in message
> news:1156689093.555748.280040@.75g2000cwc.googlegroups.com...|||> SELECT REPLACE(column ,' ' ,'0') ->only works if there is actually a
> space in the field.
One method to use COALESCE to return ' ' or '0' instead of NULL:
SELECT REPLACE(COALESCE(column, '0'), ' ', '0')

It seems to me that this is a flaw in your table design. The column should
be defined with the appropriate data type so that spaces can't be inserted
into a numeric column in the first place. You can still use SELECT
COALESCE(column, 0) to treat NULLs as 0 or treat NULLs as zero in your
application code.
Hope this helps.
Dan Guzman
SQL Server MVP
"MattieG" <mattiegriffin@.hotmail.co.uk> wrote in message
news:1156694244.396948.101820@.p79g2000cwp.googlegroups.com...[vbcol=seagreen]
> Hi
> Basically the query still have blanks in that particular field, no
> error message.
> SELECT REPLACE(column ,' ' ,'0') ->only works if there is actually a
> space in the field.
> Im thinkin i might have to use ISNUM or somet like that.
>
> Uri Dimant wrote:
>|||Hi Dan, thanks for that will look at it now.
To describe in more detail, i have a qurey to calculate sales sold in a
particular session and a maximum for that session. If there are no
sales then "maximum - nothing = nothing as apposed to 0 which is what i
need.
Ill go have a play about now and let you know how it goes.
Mattie
Dan Guzman wrote:[vbcol=seagreen]
> One method to use COALESCE to return ' ' or '0' instead of NULL:
> SELECT REPLACE(COALESCE(column, '0'), ' ', '0')
>
> It seems to me that this is a flaw in your table design. The column shoul
d
> be defined with the appropriate data type so that spaces can't be inserted
> into a numeric column in the first place. You can still use SELECT
> COALESCE(column, 0) to treat NULLs as 0 or treat NULLs as zero in your
> application code.
>
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "MattieG" <mattiegriffin@.hotmail.co.uk> wrote in message
> news:1156694244.396948.101820@.p79g2000cwp.googlegroups.com...|||On 27 Aug 2006 07:31:33 -0700, MattieG wrote:

>Hi guys
>Im sure this is easy but I have a query to calculate spaces on a
>booking system and need to replace a blank ie no bookings yet with a 0
>so that i can add the numbers up to get a total.
>Trouble is its not a NULL value and when i try
>REPLACE([fieldname],'','0') it just doesnt work.
>I can get round it but rather do it properly
Hi Mattie,
Two ways:
SELECT CASE WHEN fieldname = '' THEN '0' ELSE fieldname END
FROM ...
SELECT COALESCE(NULLIF(fieldname, ''), '0')
FROM ...
Hugo Kornelis, SQL Server MVP|||On 27 Aug 2006 15:12:24 -0700, MattieG wrote:

>Hi Dan, thanks for that will look at it now.
>To describe in more detail, i have a qurey to calculate sales sold in a
>particular session and a maximum for that session. If there are no
>sales then "maximum - nothing = nothing as apposed to 0 which is what i
>need.
Hi Mattie,
I don't understand. How can maximum - nothing be equal to nothing
instead of maximum?
But more important: how can the result of a substraction ever be an
empty string? Can yoou please post some repro code? (I.e. table
definitions as CREATE TABLE statements, sample data as INSERT statements
and the statement that produces the incorrect output)
Hugo Kornelis, SQL Server MVP|||I'm guessing here, but...
The problem isn't that maximum minus nothing = nothing.
The problem is that (implicit conversion of conforming string) minus (empty
string) isn't math.
You can fight this forever, or you can alter your table structure so that
you have a nullable integer column. Then you can use select
max(column)-ISNULL(min(column),0).
good luck!
"MattieG" wrote:

> Hi Dan, thanks for that will look at it now.
> To describe in more detail, i have a qurey to calculate sales sold in a
> particular session and a maximum for that session. If there are no
> sales then "maximum - nothing = nothing as apposed to 0 which is what i
> need.
> Ill go have a play about now and let you know how it goes.
> Mattie
>
> Dan Guzman wrote:
>

Replace '' with a 0 ......... replace nothing with a 0

Hi guys
Im sure this is easy but I have a query to calculate spaces on a
booking system and need to replace a blank ie no bookings yet with a 0
so that i can add the numbers up to get a total.
Trouble is its not a NULL value and when i try
REPLACE([fieldname],'','0') it just doesnt work.
I can get round it but rather do it properly :)
MattieHi
When you say "it does not work" , what does that mean? Is an error message,
is it not given you a right result ?
SELECT REPLACE(column ,' ' ,'0')
"MattieG" <mattiegriffin@.hotmail.co.uk> wrote in message
news:1156689093.555748.280040@.75g2000cwc.googlegroups.com...
> Hi guys
> Im sure this is easy but I have a query to calculate spaces on a
> booking system and need to replace a blank ie no bookings yet with a 0
> so that i can add the numbers up to get a total.
> Trouble is its not a NULL value and when i try
> REPLACE([fieldname],'','0') it just doesnt work.
> I can get round it but rather do it properly :)
> Mattie
>|||Hi
Basically the query still have blanks in that particular field, no
error message.
SELECT REPLACE(column ,' ' ,'0') ->only works if there is actually a
space in the field.
Im thinkin i might have to use ISNUM or somet like that.
Uri Dimant wrote:
> Hi
> When you say "it does not work" , what does that mean? Is an error message,
> is it not given you a right result ?
> SELECT REPLACE(column ,' ' ,'0')
>
> "MattieG" <mattiegriffin@.hotmail.co.uk> wrote in message
> news:1156689093.555748.280040@.75g2000cwc.googlegroups.com...
> > Hi guys
> >
> > Im sure this is easy but I have a query to calculate spaces on a
> > booking system and need to replace a blank ie no bookings yet with a 0
> > so that i can add the numbers up to get a total.
> >
> > Trouble is its not a NULL value and when i try
> > REPLACE([fieldname],'','0') it just doesnt work.
> >
> > I can get round it but rather do it properly :)
> >
> > Mattie
> >|||> SELECT REPLACE(column ,' ' ,'0') ->only works if there is actually a
> space in the field.
One method to use COALESCE to return ' ' or '0' instead of NULL:
SELECT REPLACE(COALESCE(column, '0'), ' ', '0')
>> > need to replace a blank ie no bookings yet with a 0
>> > so that i can add the numbers up to get a total.
It seems to me that this is a flaw in your table design. The column should
be defined with the appropriate data type so that spaces can't be inserted
into a numeric column in the first place. You can still use SELECT
COALESCE(column, 0) to treat NULLs as 0 or treat NULLs as zero in your
application code.
Hope this helps.
Dan Guzman
SQL Server MVP
"MattieG" <mattiegriffin@.hotmail.co.uk> wrote in message
news:1156694244.396948.101820@.p79g2000cwp.googlegroups.com...
> Hi
> Basically the query still have blanks in that particular field, no
> error message.
> SELECT REPLACE(column ,' ' ,'0') ->only works if there is actually a
> space in the field.
> Im thinkin i might have to use ISNUM or somet like that.
>
> Uri Dimant wrote:
>> Hi
>> When you say "it does not work" , what does that mean? Is an error
>> message,
>> is it not given you a right result ?
>> SELECT REPLACE(column ,' ' ,'0')
>>
>> "MattieG" <mattiegriffin@.hotmail.co.uk> wrote in message
>> news:1156689093.555748.280040@.75g2000cwc.googlegroups.com...
>> > Hi guys
>> >
>> > Im sure this is easy but I have a query to calculate spaces on a
>> > booking system and need to replace a blank ie no bookings yet with a 0
>> > so that i can add the numbers up to get a total.
>> >
>> > Trouble is its not a NULL value and when i try
>> > REPLACE([fieldname],'','0') it just doesnt work.
>> >
>> > I can get round it but rather do it properly :)
>> >
>> > Mattie
>> >
>|||Hi Dan, thanks for that will look at it now.
To describe in more detail, i have a qurey to calculate sales sold in a
particular session and a maximum for that session. If there are no
sales then "maximum - nothing = nothing as apposed to 0 which is what i
need.
Ill go have a play about now and let you know how it goes.
Mattie
Dan Guzman wrote:
> > SELECT REPLACE(column ,' ' ,'0') ->only works if there is actually a
> > space in the field.
> One method to use COALESCE to return ' ' or '0' instead of NULL:
> SELECT REPLACE(COALESCE(column, '0'), ' ', '0')
> >> > need to replace a blank ie no bookings yet with a 0
> >> > so that i can add the numbers up to get a total.
> It seems to me that this is a flaw in your table design. The column should
> be defined with the appropriate data type so that spaces can't be inserted
> into a numeric column in the first place. You can still use SELECT
> COALESCE(column, 0) to treat NULLs as 0 or treat NULLs as zero in your
> application code.
>
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "MattieG" <mattiegriffin@.hotmail.co.uk> wrote in message
> news:1156694244.396948.101820@.p79g2000cwp.googlegroups.com...
> > Hi
> >
> > Basically the query still have blanks in that particular field, no
> > error message.
> > SELECT REPLACE(column ,' ' ,'0') ->only works if there is actually a
> > space in the field.
> >
> > Im thinkin i might have to use ISNUM or somet like that.
> >
> >
> > Uri Dimant wrote:
> >> Hi
> >> When you say "it does not work" , what does that mean? Is an error
> >> message,
> >> is it not given you a right result ?
> >>
> >> SELECT REPLACE(column ,' ' ,'0')
> >>
> >>
> >>
> >> "MattieG" <mattiegriffin@.hotmail.co.uk> wrote in message
> >> news:1156689093.555748.280040@.75g2000cwc.googlegroups.com...
> >> > Hi guys
> >> >
> >> > Im sure this is easy but I have a query to calculate spaces on a
> >> > booking system and need to replace a blank ie no bookings yet with a 0
> >> > so that i can add the numbers up to get a total.
> >> >
> >> > Trouble is its not a NULL value and when i try
> >> > REPLACE([fieldname],'','0') it just doesnt work.
> >> >
> >> > I can get round it but rather do it properly :)
> >> >
> >> > Mattie
> >> >
> >|||On 27 Aug 2006 07:31:33 -0700, MattieG wrote:
>Hi guys
>Im sure this is easy but I have a query to calculate spaces on a
>booking system and need to replace a blank ie no bookings yet with a 0
>so that i can add the numbers up to get a total.
>Trouble is its not a NULL value and when i try
>REPLACE([fieldname],'','0') it just doesnt work.
>I can get round it but rather do it properly :)
Hi Mattie,
Two ways:
SELECT CASE WHEN fieldname = '' THEN '0' ELSE fieldname END
FROM ...
SELECT COALESCE(NULLIF(fieldname, ''), '0')
FROM ...
--
Hugo Kornelis, SQL Server MVP|||On 27 Aug 2006 15:12:24 -0700, MattieG wrote:
>Hi Dan, thanks for that will look at it now.
>To describe in more detail, i have a qurey to calculate sales sold in a
>particular session and a maximum for that session. If there are no
>sales then "maximum - nothing = nothing as apposed to 0 which is what i
>need.
Hi Mattie,
I don't understand. How can maximum - nothing be equal to nothing
instead of maximum?
But more important: how can the result of a substraction ever be an
empty string? Can yoou please post some repro code? (I.e. table
definitions as CREATE TABLE statements, sample data as INSERT statements
and the statement that produces the incorrect output)
--
Hugo Kornelis, SQL Server MVP|||I'm guessing here, but...
The problem isn't that maximum minus nothing = nothing.
The problem is that (implicit conversion of conforming string) minus (empty
string) isn't math.
You can fight this forever, or you can alter your table structure so that
you have a nullable integer column. Then you can use select
max(column)-ISNULL(min(column),0).
good luck!
"MattieG" wrote:
> Hi Dan, thanks for that will look at it now.
> To describe in more detail, i have a qurey to calculate sales sold in a
> particular session and a maximum for that session. If there are no
> sales then "maximum - nothing = nothing as apposed to 0 which is what i
> need.
> Ill go have a play about now and let you know how it goes.
> Mattie
>
> Dan Guzman wrote:
> > > SELECT REPLACE(column ,' ' ,'0') ->only works if there is actually a
> > > space in the field.
> >
> > One method to use COALESCE to return ' ' or '0' instead of NULL:
> >
> > SELECT REPLACE(COALESCE(column, '0'), ' ', '0')
> >
> > >> > need to replace a blank ie no bookings yet with a 0
> > >> > so that i can add the numbers up to get a total.
> >
> > It seems to me that this is a flaw in your table design. The column should
> > be defined with the appropriate data type so that spaces can't be inserted
> > into a numeric column in the first place. You can still use SELECT
> > COALESCE(column, 0) to treat NULLs as 0 or treat NULLs as zero in your
> > application code.
> >
> >
> > --
> > Hope this helps.
> >
> > Dan Guzman
> > SQL Server MVP
> >
> > "MattieG" <mattiegriffin@.hotmail.co.uk> wrote in message
> > news:1156694244.396948.101820@.p79g2000cwp.googlegroups.com...
> > > Hi
> > >
> > > Basically the query still have blanks in that particular field, no
> > > error message.
> > > SELECT REPLACE(column ,' ' ,'0') ->only works if there is actually a
> > > space in the field.
> > >
> > > Im thinkin i might have to use ISNUM or somet like that.
> > >
> > >
> > > Uri Dimant wrote:
> > >> Hi
> > >> When you say "it does not work" , what does that mean? Is an error
> > >> message,
> > >> is it not given you a right result ?
> > >>
> > >> SELECT REPLACE(column ,' ' ,'0')
> > >>
> > >>
> > >>
> > >> "MattieG" <mattiegriffin@.hotmail.co.uk> wrote in message
> > >> news:1156689093.555748.280040@.75g2000cwc.googlegroups.com...
> > >> > Hi guys
> > >> >
> > >> > Im sure this is easy but I have a query to calculate spaces on a
> > >> > booking system and need to replace a blank ie no bookings yet with a 0
> > >> > so that i can add the numbers up to get a total.
> > >> >
> > >> > Trouble is its not a NULL value and when i try
> > >> > REPLACE([fieldname],'','0') it just doesnt work.
> > >> >
> > >> > I can get round it but rather do it properly :)
> > >> >
> > >> > Mattie
> > >> >
> > >
>