This question already has answers here:
SQL Server 2008 Empty String vs. Space
(9 answers)
Closed 8 years ago.
I just realised, the following behaviour of SQL Server
SELECT
1
WHERE
' ' = ' '
Seemingly the string with just 1 space equals the string with 8 spaces. Can anyone explain, why that is and how i can compare empty strings?
SQL Server follows the ANSI/ISO SQL-92 specification (Section 8.2, , General rules #3) on how to compare strings with spaces. The ANSI standard requires padding for the character strings used in comparisons so that their lengths match before comparing them. The padding directly affects the semantics of WHERE and HAVING clause predicates and other Transact-SQL string comparisons. For example, Transact-SQL considers the strings 'abc' and 'abc ' to be equivalent for most comparison operations
use DataLength to compare in where clause.
http://msdn.microsoft.com/en-in/library/ms173486.aspx
To compare for empty string
SELECT *
FROM tableName
WHERE LTRIM(RTRIM(columnName))=''
Related
This question already has answers here:
What is the use of the square brackets [] in sql statements?
(10 answers)
What does the colon sign ":" do in a SQL query?
(8 answers)
Closed 2 years ago.
I've came cross a SQL SELECT statement which is different than what I normally see:
Select Distinct [Employee: Department_key_historic],[Employee: Department_key_current]
From [shared].[vw_Table]
What does the colon do in the above code? I am used to see format like Select table.columnName but never a :
This appears to be a SQL Server query. If that is the case, [] are used to surround object names. Therefore the : that you see are simply part of the name of the column itself.
This question already has answers here:
Using RegEx in SQL Server
(6 answers)
Closed 3 years ago.
REGEX is not available in SQL and Cannot have access to Visual Studio .
There is one output here shorturl.at/ovACN but its for MYSQL.
What I need is for SQL.
REGEX is not working with SQL.
When we are trying to do
REGEXP '^[a-zA-Z][0-9][0-9][0-9][0-9][0-9][0-9]'
in SQL its giving non boolean expression error.
How can I get only these patterns (R123456,m123456,y729472) and not like these (R12AS56,mS23456,y7294D2)
Like operator is not working and not getting what exactly I should use to get this output (R123456,m123456,y729472)
MYSQL
where COlumn name REGEXP '^[a-zA-Z][0-9][0-9][0-9][0-9][0-9][0-9]';
ORACLE:
where REGEXP (COlumn nane,'^[a-zA-Z][0-9][0-9][0-9][0-9][0-9][0-9]');
Output = R123456,m123456,y729472
Actual error msg with REGEXP function
If you have this code in MySQL:
where name REGEXP '^[a-zA-Z][0-9][0-9][0-9][0-9][0-9][0-9]'
The equivalent in Oracle is:
where regexp_like(name, '^[a-zA-Z][0-9][0-9][0-9][0-9][0-9][0-9]')
And in SQL Server is:
where name like '[a-zA-Z][0-9][0-9][0-9][0-9][0-9][0-9]%'
This question already has answers here:
Why does ISNUMERIC('.') return 1?
(3 answers)
Closed 4 years ago.
I was working on a query and found that the ISNUMERIC function in SQL Server is returning 1 for the input '.'
Why would that happen? Why does SQL Server treat that as a numeric?
Am I missing something here?
DECLARE #Input VARCHAR = '.'
SELECT IsNumeric(#Input)
From the docs online:
ISNUMERIC returns 1 for some characters that are not numbers, such as plus (+), minus (-), and valid currency symbols such as the dollar sign ($). For a complete list of currency symbols...
For example:
SELECT ISNUMERIC(+)
Will return 1, So I suggest to use TRY_PARSE() instead.
You can read this useful link from MSDN blogs, and this one from SQLServerCentral too.
SQL implicit convert empty value to zero.
SELECT CAST('' as int)
SELECT CAST('.' as money)
and
SELECT ISNUMERIC(0) --> 1
SELECT ISNUMERIC(0.0) --> 1
But i don't know where is this in the documentation
char and varchar
Character expressions that are being converted to an exact numeric
data type must consist of digits, a decimal point, and an optional
plus (+) or minus (-). Leading blanks are ignored. Comma separators,
such as the thousands separator in 123,456.00, are not allowed in the
string.
This question already has answers here:
What does N' stands for in a SQL script ? (the one used before characters in insert script)
(7 answers)
What is the meaning of the prefix N in T-SQL statements and when should I use it?
(4 answers)
Closed 5 years ago.
I found something weird in this query
SELECT *
FROM t1
WHERE t1.country = N'USA';
why N Letter is used ?
The N stands for "National Character" and it means that the content of the string is Unicode.
You should be using Unicode (nchar/nvarchar) whenever you might come across proper names or other entities that can contain characters outside of the default ASCII character set. If you don't surround such strings with the N prefix, you will lose data. For example:
SELECT N'ук ферт хер', 'ук ферт хер';
Results:
----------- -----------
ук ферт хер ?? ???? ???
You should also be sure to use the N prefix in your WHERE or other clauses against n(var)char columns. When you don't use the N prefix, you could suffer serious performance issues due to implicit conversion.
N is for the compatibility of a character. Used for representing unicode characters.
It declares the string as nvarchar data type, instead of varchar, and the difference is that varchar column only stores an 8-bit codepage and a nvarchar column can store any Unicode data
This question already has answers here:
How to handle a single quote in Oracle SQL
(2 answers)
Closed 7 years ago.
I have a column containing certain expressions stored as a text string which include single quotatons, such as 'missed transaction' (INCLUDING the quotations)
How can I use a where clause with such an occurance?
select * from table where reason = ''missed transaction''
doesn't work, and I can't use the replace function because it also requires single quotations in its syntax. Obscure problem, i know. But thanks for any help.
You need to escape the ' by doubling them :
select * from table where reason = '''missed transaction''';
The q quote syntax makes this sort of thing easier:
select * from table where reason = q'['missed transaction']'
Everything between the '[ and the ]' is interpreted literally, so no need to double all the quotes, however many there may be.