RTrim is not working when sentence is Arabic - sql-server

RTrim is not working when sentence is Arabic.
Below is the example
declare #name nvarchar(50);
set #name = N' لللييب بي  ';
select rtrim(ltrim(#name));
English one is working
declare #name nvarchar(50);
set #name = ' This is test message ';
select rtrim(ltrim(#name));

Please try the following.
Casting as VARBINARY data type allows to see what exact characters represent white spaces in the string. It could be SPACE character (0x2000), but could be something else. If it is something else, the LTRIM() and RTRIM() functions won't work.
SQL
DECLARE #name NVARCHAR(50) ;
SET #name = N' لللييب بي ' COLLATE Arabic_CI_AI_KS_WS;
SELECT #name AS [Before], DATALENGTH(#name) AS [Before_Len]
, TRY_CAST(#name AS VARBINARY(30)) AS [Before_Binary]
, RTRIM(LTRIM(#name)) AS [After], DATALENGTH(RTRIM(LTRIM(#name))) AS [After_Len];
-- check available Arabic collations
SELECT * FROM sys.fn_helpcollations()
WHERE name LIKE 'Arabic%';
Before
Before_Len
Before_Binary
After
After_Len
لللييب بي
24
0x20004406440644064A064A062806200028064A0620002000
لللييب بي
18

Related

How can I replace two characters in a single word with wildcard if the two characters are included in wild card definition in MSQL

declare #SearchString varchar(150) = 'Banana'
set #SearchString = replace(#SearchString,'a','[abc]');
set #SearchString = replace(#SearchString,'b','[abc]');
select #SearchString
Result will be
#SearchString = [abc][a[abc]c]n[a[abc]c]n[a[abc]c]
what I want is
[abc][[abc]c
How can I do that ?
I found another way to do that be comparing the the replacements on both sides like below
Declare #Keyword varchar(50)='أحمد'
Select name from names where Replace(name,'أ','ا') = replace(#keyword,'ا','أ')
that will get me 'أحمد' and 'احمد'

Blank Spaces are Not Including IN Variable

Actually I'm validating the User_Name With the Column user_Name which Present in a table.
User vipul_1 Present in table, But if pass The data, 'vipul_1 ', it Should return 0 rows affected but instead of this return me the Result.
DECLARE #User_Name NVARCHAR(25)
SET #User_Name= 'vipul_1 '
Returns the number of characters of the specified string expression, excluding trailing blanks. This is clearly mentioned here.
Trailing blanks won't be included in the length count. But it will include the blanks in the beginning of the string.
For example.
DECLARE #User_Name as NVARCHAR(25)
SET #User_Name= 'vipul_1 '
SELECT LEN(#User_Name)[User_Name]
This will return 7.
But,
DECLARE #User_Name as NVARCHAR(25)
SET #User_Name= ' vipul_1 '
SELECT LEN(#User_Name)[User_Name];
This will return 12 including the blanks in the beginning.
And if you want to count the trailing spaces also, then just add any character to the end and find the length and subtract 1.
Something like this.
Query
declare #username as varchar(1000);
set #username = 'some_name_here '; -- you are unknown about the content of string.
set #username = #username + '_';
select LEN(#username) - 1 as [name_length];
Find a fiddle demo here
he trailing spaces are there. They are just ignored by the LEN function. Try using DATALENGTH function instead.
DECLARE #User_Name NVARCHAR(25);
SET #User_Name = 'vipul_1 ';
SELECT
LenVal = LEN(#User_Name),
DataLenVal = DATALENGTH(#User_Name);
Results...
LenVal DataLenVal
----------- -----------
7 28
In Sqlserver RTRIM and LTRIM function exist, use it
select LTRIM( ' vipul_1 ') , RTRIM( ' vipul_1 ') , RTRIM( LTRIM( ' vipul_1 ') )
As the latest Sqlserver introduce the Trim function
Updated
SQL Server follows the ANSI/ISO SQL-92 specification, see this below links. For this you compare the string with LIKE operator as suggested below.
Len function is also not work, use DATALENGTH instead of this (As last suggested).
Why the SQL Server ignore the empty space at the end automatically?
https://dba.stackexchange.com/questions/10510/behavior-of-varchar-with-spaces-at-the-end
Declare #table table (name varchar(50))
insert into #table values ('vipul_1'), ('ajay_1') , ('eeee_1') , ('vipul')
DECLARE #User_Name NVARCHAR(25)
SET #User_Name= 'vipul_1 '
if Exists (select * from #table where name = #User_Name and DATALENGTH(name) = DATALENGTH(#User_Name) )
Select 0
Else
Select 1
select * from #table where name like #User_Name
select * from #table where name = #User_Name
select * from #table where name = #User_Name and DATALENGTH(name) = DATALENGTH(#User_Name) --use DataLength for this

Not able to replace special character in nvarchar string

I am trying to split a string which is nvarchar(max)
if I pass the string like
#String_xyz = 'abc#def#ghi$jkl'
Then I am able to replace the special character but my character comes in a unreadable format like ?????
If I send my string like this way
#String_xyz = N'abc#def#ghi$jkl'
Then I am not able to replace any special character
Let say
DECLARE #string nvarchar(max)
SET #string = 'என்$பெயர்#PIN#ஆகிறது##என்$பெயர்#KUL#ஆகிறது'
SET #string = replace(#string,'##',' ') -- This work perfect
if
DECLARE #string nvarchar(max)
SET #string = N'என்$பெயர்#PIN#ஆகிறது##என்$பெயர்#KUL#ஆகிறது'
SET #string = replace(#string,'##',' ') -- This will not work
Please let me know any possible solution
where abc-def-ghi-jkl are multilanguage character
Collation : SQL_Latin1_General_CP1_CI_AS
The point is UNICODE
You have to make sure, that you use unicode in all places
Literal strings must be started with an N everywhere
Try this
--This will come out with question marks
SELECT 'என்$பெயர்#PIN#ஆகிறது##என்$பெயர்#KUL#ஆகிறது';
--And this is the correct output
SELECT N'என்$பெயர்#PIN#ஆகிறது##என்$பெயர்#KUL#ஆகிறது';
--Here I replace one of the characters with a "%"
SELECT REPLACE(N'என்$பெயர்#PIN#ஆகிறது##என்$பெயர்#KUL#ஆகிறது',N'கி',N'%')
This works fine here...
UPDATE
Cannot verify this anymore, but it might be, that the output was wrong at the first try. I tried around with several collations. With this I got the wanted
SELECT N'YourString' COLLATE Indic_General_90_BIN;
After having used this, it was OK. So - but this is just guessing - it might be, that SQL Server had to learn this first...
If your code is like
DECLARE #string nvarchar(max) = N'என்$பெயர்#PIN#ஆகிறது##என்
SET #string = replace(#string,'##',' ')
results as
என்$பெயர்#PIN#ஆகிறது என்$பெயர்#KUL#ஆகிறது
if
DECLARE #string1 nvarchar(max) = 'என்$பெயர்#PIN#ஆகிறது##என்$பெயர்#KUL#ஆகிறது'
SET #string1 = replace(#string1,'##',' ')
then
???$?????#PIN#?????? ???$?????#KUL#??????
if
DECLARE #string2 varchar(max) = 'என்$பெயர்#PIN#ஆகிறது##என்$பெயர்#KUL#ஆகிறது'
SET #string2 = replace(#string2,'##',' ')
then
???$?????#PIN#?????? ???$?????#KUL#??????
if
DECLARE #string3 varchar(max) = N'என்$பெயர்#PIN#ஆகிறது##என்$பெயர்#KUL#ஆகிறது'
SET #string3 = replace(#string3,'##',' ')
then
???$?????#PIN#?????? ???$?????#KUL#??????
if you declares string as nvarchar you have to give N' before the string otherwise unicodes will not work.

Saving sql query that contains arabic into a cell

declare #val nvarchar(max),#H_ARABIC nvarchar(max)
select #val = 'select [settings_key] as N''اسم'' from [settings]'
set #H_ARABIC= #val;
print #H_ARABIC
It is showing results as
select [settings_key] as N'???' from [application_setting] but how can I get this result
select [settings_key] as N'اسم' from [application_setting] I have tried many ways by changing the quotation mark but no use. Pls help
Prefix your string literal with N.
declare #val nvarchar(max),#H_ARABIC nvarchar(max)
select #val = N'select [settings_key] as N''اسم'' from [settings]'
set #H_ARABIC= #val;
print #H_ARABIC

substring or rtrim

i have a field called LastWebpage, in the Field it stores the webpage .asp pagename
I have to write a script where i just need to take the jl_AssessmentDirect.asp name instead of 4000_1_jl_AssessmentDirect.asp. How do i get rid of 4000_1_. Please help.
Thanks.
Here are a couple of ways to do this, I assume the data is not always the same, this is just to show you how the functions work
right or replace is my preferred method
DECLARE #v VARCHAR(100)
SELECT #v = '4000_1_jl_AssessmentDirect.asp'
SELECT REPLACE(#v,'4000_1_','')
or
DECLARE #v VARCHAR(100)
SELECT #v = '4000_1_jl_AssessmentDirect.asp'
DECLARE #r VARCHAR(100)
SELECT #r ='4000_1_'
--if you have spaces RTRIM and LTRIM everything
SELECT RIGHT(#v,LEN(#v) -LEN(#r))
If stuff changes, you can dump it in variables
DECLARE #v VARCHAR(100)
SELECT #v = '4000_1_jl_AssessmentDirect.asp'
DECLARE #r VARCHAR(100)
SELECT #r ='4000_1_'
SELECT REPLACE(#v,#r,'')
Ideally you want to update such columns to only have the URL and nothing else
"It depends"
If always "4000_1_", use REPLACE as per SQLMenace's answer.
If a fixed length of any string, use SUBSTRING. No need to work out LENGTH first
SUBSTRING(MyValue, 8, 8000)
If the right hand side is fixed length and the unwanted part is variable length, use RIGHT
If both sides are variable, you'll need something like
SUBSTRING(MyValue, PATINDEX('%[_]1[_]%', MyValue) + 3, 8000)
Edit:
If you always want "jl"...
SUBSTRING(MyValue, PATINDEX('%jl%', MyValue), 8000)
Besides all the suggestions here already you can use stuff.
declare #s varchar(100)
set #s = '4000_1_jl_AssessmentDirect.asp'
select stuff(#s, 1, 7, '')
Remove characters before jl
declare #s varchar(100)
set #s = '4000_1_jl_AssessmentDirect.asp'
select stuff(#s, 1, charindex('jl', #s)-1, '')

Resources