Using Letter N in WHERE Cluase [duplicate] - sql

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

Related

Postgres SQL to remove only non-ascii characters from a string [duplicate]

This question already has an answer here:
how to replace non ascii characters with empty values
(1 answer)
Closed 2 months ago.
We have data in Postgres something as below , there is possibility of having mutiple non-ascii chars in a string
Name
Kate SolutionǸǸs
Etak Solutions
We are trying to identify if there are any NON-ASCII char set from the string and remove all of them if there are any(if there no non-ascii then keep the string as is) and expecting output below output
Name
Kate Solutions
Etak Solutions
Tried using below SQL but its just removing only one non-ascii char irrespective of the position where it is present
select REGEX_REPLACE(name,'[^ -~]', '') as new_name from table
Appreciate any help!
From the Postgresql documentation:
If the g flag is given, or if N is specified and is zero, then all matches at or after the start position are replaced. (The g flag is ignored when N is specified.)
So either add a 4th parameter as zero, 0, or add a 6th parameter as 'g'.

Why is the dot or period ('.') treated as numeric in SQL Server? [duplicate]

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.

Select truncated string from Postgres

I have some large varchar values in Postgres that I want to SELECT and move somewhere else. The place they are going to uses VARCHAR(4095) so I only need at most 4095 bytes (I think that's bytes) and some of these varchars are quite big, so a performance optimization would be to SELECT a truncated version of them.
How can I do that?
Something like:
SELECT TRUNCATED(my_val, 4095) ...
I don't think it's a character length though, it needs to be a byte length?
The n in varchar(n) is the number of characters, not bytes. The manual:
SQL defines two primary character types: character varying(n) and
character(n), where n is a positive integer. Both of these types can
store strings up to n characters (not bytes) in length.
Bold emphasis mine.
The simplest way to "truncate" a string would be with left():
SELECT left(my_val, 4095)
Or just cast:
SELECT my_val::varchar(4095)
The manual once more:
If one explicitly casts a value to character varying(n) or
character(n), then an over-length value will be truncated to n
characters without raising an error. (This too is required by the SQL standard.)

How does SQL Server 2008R2 handle Spaces [duplicate]

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))=''

Retrieving MS SQL database size in culture independent format

sp_helpdb returns strings like '50000.255 MB' in the db_size column.
These strings are culture-dependent; the above string will mean 2 different things in US and Germany (in the latter, the dot char is used as a group separator, similar to the comma in US).
Is there another method which returns a numeric value, culture-independent?
Use YourDB;
SELECT SUM(Size / 128.0) As FileSize from sys.database_files;
This returns the size in MB as a numeric, you should be able to do what you like with it from there.
Note: size returns the number of 8KB pages in a given database file.
http://msdn.microsoft.com/en-us/library/ms174397.aspx
I do not think it is possible. The SP sp_helpdb uses str to convert the numeric size to varchar and there is nothing in the documentation (that I can find) that can make str use , instead of . as decimal symbol. Using set language does not help.
Workaround as suggested by Martin in comment
select replace(str(sum(convert(dec(17,2),size)) / 128,10,2) +' MB', '.', ',')
from sys.database_files