Spaces in WHERE clause for SQL Server
Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
I want to find out the records in which a certain column contains exactly one space and nothing else. So I wrote the first of the following queries:
select COUNT(*)
from mytable
where col = ' ' -- One space
select COUNT(*)
from mytable
where col = ' ' -- Two spaces
select COUNT(*)
from mytable
where col = ' ' -- Three spaces
However, all three queries return the exact same records. Does Microsoft SQL Server not distinguish between the amount of spaces? How can I query exactly for one, two or more spaces?
sql sql-server
add a comment |Â
up vote
8
down vote
favorite
I want to find out the records in which a certain column contains exactly one space and nothing else. So I wrote the first of the following queries:
select COUNT(*)
from mytable
where col = ' ' -- One space
select COUNT(*)
from mytable
where col = ' ' -- Two spaces
select COUNT(*)
from mytable
where col = ' ' -- Three spaces
However, all three queries return the exact same records. Does Microsoft SQL Server not distinguish between the amount of spaces? How can I query exactly for one, two or more spaces?
sql sql-server
dba.stackexchange.com/questions/10510/â¦
â Ivan Starostin
4 hours ago
Its simple: looks into empty string. doesnt matter if its one or two spaces. try it this way 'value ' and check result set
â Hadrian
4 hours ago
If SQL Server would adhere to Standard SQL it would be really simple:where col like ' '
â dnoeth
3 hours ago
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I want to find out the records in which a certain column contains exactly one space and nothing else. So I wrote the first of the following queries:
select COUNT(*)
from mytable
where col = ' ' -- One space
select COUNT(*)
from mytable
where col = ' ' -- Two spaces
select COUNT(*)
from mytable
where col = ' ' -- Three spaces
However, all three queries return the exact same records. Does Microsoft SQL Server not distinguish between the amount of spaces? How can I query exactly for one, two or more spaces?
sql sql-server
I want to find out the records in which a certain column contains exactly one space and nothing else. So I wrote the first of the following queries:
select COUNT(*)
from mytable
where col = ' ' -- One space
select COUNT(*)
from mytable
where col = ' ' -- Two spaces
select COUNT(*)
from mytable
where col = ' ' -- Three spaces
However, all three queries return the exact same records. Does Microsoft SQL Server not distinguish between the amount of spaces? How can I query exactly for one, two or more spaces?
sql sql-server
sql sql-server
edited 11 mins ago
Peter Mortensen
13k1983111
13k1983111
asked 4 hours ago
Koruba
735
735
dba.stackexchange.com/questions/10510/â¦
â Ivan Starostin
4 hours ago
Its simple: looks into empty string. doesnt matter if its one or two spaces. try it this way 'value ' and check result set
â Hadrian
4 hours ago
If SQL Server would adhere to Standard SQL it would be really simple:where col like ' '
â dnoeth
3 hours ago
add a comment |Â
dba.stackexchange.com/questions/10510/â¦
â Ivan Starostin
4 hours ago
Its simple: looks into empty string. doesnt matter if its one or two spaces. try it this way 'value ' and check result set
â Hadrian
4 hours ago
If SQL Server would adhere to Standard SQL it would be really simple:where col like ' '
â dnoeth
3 hours ago
dba.stackexchange.com/questions/10510/â¦
â Ivan Starostin
4 hours ago
dba.stackexchange.com/questions/10510/â¦
â Ivan Starostin
4 hours ago
Its simple: looks into empty string. doesnt matter if its one or two spaces. try it this way 'value ' and check result set
â Hadrian
4 hours ago
Its simple: looks into empty string. doesnt matter if its one or two spaces. try it this way 'value ' and check result set
â Hadrian
4 hours ago
If SQL Server would adhere to Standard SQL it would be really simple:
where col like ' '
â dnoeth
3 hours ago
If SQL Server would adhere to Standard SQL it would be really simple:
where col like ' '
â dnoeth
3 hours ago
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
6
down vote
Yes, it ignores trailing spaces in comparisons.
You can try to append a delimiting character.
SELECT count(*)
FROM mytable
WHERE col + 'X' = ' X';
Technically the standard requires the shorter string be padded with spaces until it has the same length as the longer string. In practice though, I've never found a situation where that would produce a different outcome than trimming/ignoring the trailing spaces.
â Damien_The_Unbeliever
4 hours ago
2
Unfortunately, this precludes the use of indexes.
â Gordon Linoff
3 hours ago
1
@GordonLinoff Could that be solved withWHERE col = ' ' AND col+'X' = ' X'
? The first part is indexed, then it applies the refinement.
â Barmar
1 hour ago
add a comment |Â
up vote
2
down vote
The link posted by Ivan Starostin in the comments of the OP provides a good explanation and I think it deserves a full answer instead of just a comment.
To summarize, try using LIKE instead of equality:
select COUNT(*)
from mytable
where col LIKE ' ' -- one space
And you can also use DATALENGTH to calculate how many bytes are in the field to double-check field length:
select col, DATALENGTH(col)
from mytable;
Please note that DATALENGTH will return a different value if col is a VARCHAR vs NVARCHAR. VARCHAR stores each character as 1 byte where NVARCHAR stores each character as 2 bytes since NVARCHAR is stored in Unicode.
add a comment |Â
up vote
0
down vote
You can combine DATALENGTH
clause with your query:
select COUNT(*)
from mytable
where col = ' '
and DATALENGTH(col) = 1
add a comment |Â
up vote
0
down vote
You can replace the single space with a single character (for exampe ç
) and then put this character in your where
condition:
declare @tmp table(col varchar(50))
insert into @tmp values
(' '),
(' '),
(' ')
select COUNT(*) as one_space_count
from @tmp
where replace(col,' ','ç')='ç'
select COUNT(*) as two_space_count
from @tmp
where replace(col,' ','ç')='çç'
select COUNT(*) as three_space_count
from @tmp
where replace(col,' ','ç')='ççç'
Results:
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
Yes, it ignores trailing spaces in comparisons.
You can try to append a delimiting character.
SELECT count(*)
FROM mytable
WHERE col + 'X' = ' X';
Technically the standard requires the shorter string be padded with spaces until it has the same length as the longer string. In practice though, I've never found a situation where that would produce a different outcome than trimming/ignoring the trailing spaces.
â Damien_The_Unbeliever
4 hours ago
2
Unfortunately, this precludes the use of indexes.
â Gordon Linoff
3 hours ago
1
@GordonLinoff Could that be solved withWHERE col = ' ' AND col+'X' = ' X'
? The first part is indexed, then it applies the refinement.
â Barmar
1 hour ago
add a comment |Â
up vote
6
down vote
Yes, it ignores trailing spaces in comparisons.
You can try to append a delimiting character.
SELECT count(*)
FROM mytable
WHERE col + 'X' = ' X';
Technically the standard requires the shorter string be padded with spaces until it has the same length as the longer string. In practice though, I've never found a situation where that would produce a different outcome than trimming/ignoring the trailing spaces.
â Damien_The_Unbeliever
4 hours ago
2
Unfortunately, this precludes the use of indexes.
â Gordon Linoff
3 hours ago
1
@GordonLinoff Could that be solved withWHERE col = ' ' AND col+'X' = ' X'
? The first part is indexed, then it applies the refinement.
â Barmar
1 hour ago
add a comment |Â
up vote
6
down vote
up vote
6
down vote
Yes, it ignores trailing spaces in comparisons.
You can try to append a delimiting character.
SELECT count(*)
FROM mytable
WHERE col + 'X' = ' X';
Yes, it ignores trailing spaces in comparisons.
You can try to append a delimiting character.
SELECT count(*)
FROM mytable
WHERE col + 'X' = ' X';
answered 4 hours ago
sticky bit
10.3k51629
10.3k51629
Technically the standard requires the shorter string be padded with spaces until it has the same length as the longer string. In practice though, I've never found a situation where that would produce a different outcome than trimming/ignoring the trailing spaces.
â Damien_The_Unbeliever
4 hours ago
2
Unfortunately, this precludes the use of indexes.
â Gordon Linoff
3 hours ago
1
@GordonLinoff Could that be solved withWHERE col = ' ' AND col+'X' = ' X'
? The first part is indexed, then it applies the refinement.
â Barmar
1 hour ago
add a comment |Â
Technically the standard requires the shorter string be padded with spaces until it has the same length as the longer string. In practice though, I've never found a situation where that would produce a different outcome than trimming/ignoring the trailing spaces.
â Damien_The_Unbeliever
4 hours ago
2
Unfortunately, this precludes the use of indexes.
â Gordon Linoff
3 hours ago
1
@GordonLinoff Could that be solved withWHERE col = ' ' AND col+'X' = ' X'
? The first part is indexed, then it applies the refinement.
â Barmar
1 hour ago
Technically the standard requires the shorter string be padded with spaces until it has the same length as the longer string. In practice though, I've never found a situation where that would produce a different outcome than trimming/ignoring the trailing spaces.
â Damien_The_Unbeliever
4 hours ago
Technically the standard requires the shorter string be padded with spaces until it has the same length as the longer string. In practice though, I've never found a situation where that would produce a different outcome than trimming/ignoring the trailing spaces.
â Damien_The_Unbeliever
4 hours ago
2
2
Unfortunately, this precludes the use of indexes.
â Gordon Linoff
3 hours ago
Unfortunately, this precludes the use of indexes.
â Gordon Linoff
3 hours ago
1
1
@GordonLinoff Could that be solved with
WHERE col = ' ' AND col+'X' = ' X'
? The first part is indexed, then it applies the refinement.â Barmar
1 hour ago
@GordonLinoff Could that be solved with
WHERE col = ' ' AND col+'X' = ' X'
? The first part is indexed, then it applies the refinement.â Barmar
1 hour ago
add a comment |Â
up vote
2
down vote
The link posted by Ivan Starostin in the comments of the OP provides a good explanation and I think it deserves a full answer instead of just a comment.
To summarize, try using LIKE instead of equality:
select COUNT(*)
from mytable
where col LIKE ' ' -- one space
And you can also use DATALENGTH to calculate how many bytes are in the field to double-check field length:
select col, DATALENGTH(col)
from mytable;
Please note that DATALENGTH will return a different value if col is a VARCHAR vs NVARCHAR. VARCHAR stores each character as 1 byte where NVARCHAR stores each character as 2 bytes since NVARCHAR is stored in Unicode.
add a comment |Â
up vote
2
down vote
The link posted by Ivan Starostin in the comments of the OP provides a good explanation and I think it deserves a full answer instead of just a comment.
To summarize, try using LIKE instead of equality:
select COUNT(*)
from mytable
where col LIKE ' ' -- one space
And you can also use DATALENGTH to calculate how many bytes are in the field to double-check field length:
select col, DATALENGTH(col)
from mytable;
Please note that DATALENGTH will return a different value if col is a VARCHAR vs NVARCHAR. VARCHAR stores each character as 1 byte where NVARCHAR stores each character as 2 bytes since NVARCHAR is stored in Unicode.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
The link posted by Ivan Starostin in the comments of the OP provides a good explanation and I think it deserves a full answer instead of just a comment.
To summarize, try using LIKE instead of equality:
select COUNT(*)
from mytable
where col LIKE ' ' -- one space
And you can also use DATALENGTH to calculate how many bytes are in the field to double-check field length:
select col, DATALENGTH(col)
from mytable;
Please note that DATALENGTH will return a different value if col is a VARCHAR vs NVARCHAR. VARCHAR stores each character as 1 byte where NVARCHAR stores each character as 2 bytes since NVARCHAR is stored in Unicode.
The link posted by Ivan Starostin in the comments of the OP provides a good explanation and I think it deserves a full answer instead of just a comment.
To summarize, try using LIKE instead of equality:
select COUNT(*)
from mytable
where col LIKE ' ' -- one space
And you can also use DATALENGTH to calculate how many bytes are in the field to double-check field length:
select col, DATALENGTH(col)
from mytable;
Please note that DATALENGTH will return a different value if col is a VARCHAR vs NVARCHAR. VARCHAR stores each character as 1 byte where NVARCHAR stores each character as 2 bytes since NVARCHAR is stored in Unicode.
answered 3 hours ago
Mike Bruesch
3517
3517
add a comment |Â
add a comment |Â
up vote
0
down vote
You can combine DATALENGTH
clause with your query:
select COUNT(*)
from mytable
where col = ' '
and DATALENGTH(col) = 1
add a comment |Â
up vote
0
down vote
You can combine DATALENGTH
clause with your query:
select COUNT(*)
from mytable
where col = ' '
and DATALENGTH(col) = 1
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can combine DATALENGTH
clause with your query:
select COUNT(*)
from mytable
where col = ' '
and DATALENGTH(col) = 1
You can combine DATALENGTH
clause with your query:
select COUNT(*)
from mytable
where col = ' '
and DATALENGTH(col) = 1
answered 4 hours ago
S.K.
1,824717
1,824717
add a comment |Â
add a comment |Â
up vote
0
down vote
You can replace the single space with a single character (for exampe ç
) and then put this character in your where
condition:
declare @tmp table(col varchar(50))
insert into @tmp values
(' '),
(' '),
(' ')
select COUNT(*) as one_space_count
from @tmp
where replace(col,' ','ç')='ç'
select COUNT(*) as two_space_count
from @tmp
where replace(col,' ','ç')='çç'
select COUNT(*) as three_space_count
from @tmp
where replace(col,' ','ç')='ççç'
Results:
add a comment |Â
up vote
0
down vote
You can replace the single space with a single character (for exampe ç
) and then put this character in your where
condition:
declare @tmp table(col varchar(50))
insert into @tmp values
(' '),
(' '),
(' ')
select COUNT(*) as one_space_count
from @tmp
where replace(col,' ','ç')='ç'
select COUNT(*) as two_space_count
from @tmp
where replace(col,' ','ç')='çç'
select COUNT(*) as three_space_count
from @tmp
where replace(col,' ','ç')='ççç'
Results:
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can replace the single space with a single character (for exampe ç
) and then put this character in your where
condition:
declare @tmp table(col varchar(50))
insert into @tmp values
(' '),
(' '),
(' ')
select COUNT(*) as one_space_count
from @tmp
where replace(col,' ','ç')='ç'
select COUNT(*) as two_space_count
from @tmp
where replace(col,' ','ç')='çç'
select COUNT(*) as three_space_count
from @tmp
where replace(col,' ','ç')='ççç'
Results:
You can replace the single space with a single character (for exampe ç
) and then put this character in your where
condition:
declare @tmp table(col varchar(50))
insert into @tmp values
(' '),
(' '),
(' ')
select COUNT(*) as one_space_count
from @tmp
where replace(col,' ','ç')='ç'
select COUNT(*) as two_space_count
from @tmp
where replace(col,' ','ç')='çç'
select COUNT(*) as three_space_count
from @tmp
where replace(col,' ','ç')='ççç'
Results:
answered 3 hours ago
Andrea
6,922144248
6,922144248
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52592109%2fspaces-in-where-clause-for-sql-server%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
dba.stackexchange.com/questions/10510/â¦
â Ivan Starostin
4 hours ago
Its simple: looks into empty string. doesnt matter if its one or two spaces. try it this way 'value ' and check result set
â Hadrian
4 hours ago
If SQL Server would adhere to Standard SQL it would be really simple:
where col like ' '
â dnoeth
3 hours ago