SQL Server T-SQL N prefix on string literal [duplicate] - sql

This question already has answers here:
What is the meaning of the prefix N in T-SQL statements and when should I use it?
(4 answers)
Closed 9 years ago.
It's maybe a noob question but I found some T-SQL query example to verify database size with SELECT and WHERE clause here
Here is the code:
SELECT name, size, size*1.0/128 AS [Size in MBs]
FROM sys.master_files
WHERE name = N'mytest';
My question is what does the N prefix mean in the WHERE name = N'keyword' clause?
I always use WHERE name = 'keyword' before, and I don't find the differences (you can try it by yourself).
I've googled that but I don't know the keyword I supposed to search

It's declaring the string as nvarchar data type (Unicode), rather than varchar (8-bit encoding, including ASCII).
FYI, this is a duplicate of the question:
What is the meaning of the prefix N in T-SQL statements?

From the docs:-
You may have seen Transact-SQL code that passes strings around using
an N prefix. This denotes that the subsequent string is in Unicode
(the N actually stands for National language character set). Which
means that you are passing an NCHAR, NVARCHAR or NTEXT value, as
opposed to CHAR, VARCHAR or TEXT.

N means there can be some unicode characters in the given string.

It means unicode. See http://msdn.microsoft.com/en-us/library/bb399176.aspx To declare a character string literal as Unicode, prefix the literal with an uppercase "N"

Related

db2 REGEXP_INSTR issue with large input string

DB2 Regular expression REGEXP_INSTR works perfectly using host variables(pl1 program), but somehow it has issues when the input string has more than 360 characters, trailing spaces would not be a issue.
3 Info CHAR(378),
EXEC SQL
SELECT REGEXP_INSTR(:Info,
:RG_EXPR,
1,
1)
INTO :REGEXP_START
FROM SYSIBM.DUAL;
Error Message:
SQL0302N The value of a host variable in the EXECUTE or OPEN statement is out of range for its corresponding use. .SQL
STATE=22001.
Edit: Issue seems to be resovlved when I use a VAR CHAR variable instead. But this issue occurs with non-var char field with large inputs.
This is less to do with regexp_instr() than simple fundamentals.
Instead, it concerns Db2 database fundamentals, specifically the maximum length of fixed length character strings.
A fixed length character string (data type CHAR in SQL) in Db2 can occupy between 1 and 255 bytes.
A variable length character string (database type VARCHAR and others) can occupy between 1 and 32,672 bytes .
If you need longer length strings, then you need to use large objects (for example CLOB which allows up to 2gigabytes).
Please refer to the documentation for your Db2-version on your Db2 platform (z/os, i series, linux/unix/windows).
Your host variables need to reflect these rules, and your host variables must match (or be fully compatible with) the database columns or result-set columns to/from which they are assigned.

Check if string consists of letters or numbers only in Transact-SQL

How can I check if a string consists of letters or numbers ONLY in Transact-SQL?
Here the string has datatype nchar, 'letters' specifically refer to latin characters. Database environment is Microsoft SQL Server 2014.
Example for desired result:
C172E returns True
412?A returns False //'?' is neither a letter nor a number
I've done some searching but only found the built-in ISNUMERIC() function, which is for numbers only.
Is there a Transact-SQL solution for this problem?
See this similar question about obtaining only fields with alphanumeric data
You can do the following:
FIELD NOT LIKE '%[^a-zA-Z0-9]%'
You can use not like:
(case when str not like '%[^a-zA-Z0-9]%' then 1 else 0 end) as isalnum_flag
This is saying that the string has no non-alphanumberic characters.
You are looking at using Regex to do this. [a-zA-Z] is char, [0-9] is numeric, [a-zA-Z0-9] is both. You mention having to add in spaces? \s is white space characters (this could be more than just the fields.
This one is a bit more involved (more depth).

SQL server, check if varchar contains all zeros

I need to write a function to check if a varchar variable value is all zeros.
As the variable is a varchar and not an int I am a bit lost. What would you do?
Thanks a lot
You can use a double-negative with a LIKE test:
#variable NOT LIKE '%[^0]%'
Which says the variable isn't composed of some number of characters, then a character that isn't a 0, followed by some number of characters. The only strings that fail to match that LIKE expression are strings that only contain 0s, and so we use the NOT to invert the result.
(This does also accept an empty string - whether you consider an empty string to be composed of only 0s can be quite an interesting discussion - it certainly doesn't contain any other characters. A simple LEN(#Variable) test can be used if you do choose to reject this)

count number of characters in nvarchar column

Does anyone know a good way to count characters in a text (nvarchar) column in Sql Server?
The values there can be text, symbols and/or numbers.
So far I used sum(datalength(column))/2 but this only works for text. (it's a method based on datalength and this can vary from a type to another).
You can find the number of characters using system function LEN.
i.e.
SELECT LEN(Column) FROM TABLE
Use
SELECT length(yourfield) FROM table;
Use the LEN function:
Returns the number of characters of the specified string expression, excluding trailing blanks.
Doesn't SELECT LEN(column_name) work?
text doesn't work with len function.
ntext, text, and image data types will be removed in a future version
of Microsoft SQL Server. Avoid using these data types in new
development work, and plan to modify applications that currently use
them. Use nvarchar(max), varchar(max), and varbinary(max) instead. For
more information, see Using Large-Value Data Types.
Source
I had a similar problem recently, and here's what I did:
SELECT
columnname as 'Original_Value',
LEN(LTRIM(columnname)) as 'Orig_Val_Char_Count',
N'['+columnname+']' as 'UnicodeStr_Value',
LEN(N'['+columnname+']')-2 as 'True_Char_Count'
FROM mytable
The first two columns look at the original value and count the characters (minus leading/trailing spaces).
I needed to compare that with the true count of characters, which is why I used the second LEN function. It sets the column value to a string, forces that string to Unicode, and then counts the characters.
By using the brackets, you ensure that any leading or trailing spaces are also counted as characters; of course, you don't want to count the brackets themselves, so you subtract 2 at the end.

Sql Server Collation

The book I am reading says that
SQL Server supports two kinds of character data types—regular and Unicode. Regular data types include CHAR and VARCHAR, and Unicode data types include NCHAR and NVARCHAR. The difference is that regular characters use one byte of storage for each character, while Unicode characters require two bytes per character. With one byte of storage per character, a choice of a regular character type for a column restricts you to only one language in addition to English because only 256 (2^8) different characters can be represented by a single byte.
What I came to know by this is, if I use Varchar then I can use only one language(For ex. Hindi, an Indian Language) along with English.
But When I run this
Create Table NameTable
(
NameColumn varchar(MAX) COLLATE Indic_General_90_CI_AS_KS
)
It shows me error "Collation 'Indic_General_90_CI_AS_KS' is supported on Unicode data types only and cannot be applied to char, varchar or text data types."
So where have I misunderstood the author?
Thanks
You can find a list of collations here, along with the encoding type
Certain collations will apply only to 1-byte encodings -- 127 bits are used for normal ASCII and 128 are available for other characters -- hindi probably does not fit in 128 characters so a 1-byte collation does not apply to it.
You will have to use a nvarchar (or other 'n' prefixed character type).
-- edit --
French_CI_AS as a non-english example
One of the things collations enable is language and locale specific ordering of characters. Therefore French != latin.
Another example Arabic_CI_AS
This is a 1-byte encoding with the arabic alphabet.
Use this in your SQL Statement, considering "content" is a variable containing the Arabic string you want to insert:
update Table set contents = convert(text, N'" + content + "' collate Arabic_CI_AS)
It works fine.
you can use this
name = N'مرحبا كيف حالك'