I have quite a strange problem. I have a column with surnames with national characters
I would like to replace national characters, so I made something like that
select replace(surname,'Ê','E')
for surname ABCDÊ as result still is ABCDÊ
but when I make a test and replace the value that I copied
select replace('ABCDÊ','Ê','E')
It works correctly and as result I get ABCDE
Feel like something is missing here. Is it possible your column is using a case sensitive collation? If so, either will have to override it, or just replace each letter case individually (probably the better method to preserve original letter case).
Adjusting for Case-Sensitive Collation
/*Collation abbreviation "CS" = Case-sensitive*/
DECLARE #table AS TABLE (surname VARCHAR(100) COLLATE SQL_Latin1_General_CP1_CS_AS)
INSERT INTO #table
VALUES ('abcdê'),('ABCDÊ')
SELECT surname
,YourOriginalCode = REPLACE(surname, 'Ê', 'E')
,ForceCaseInsensitiveCollation = REPLACE(surname COLLATE SQL_Latin1_General_CP1_CI_AS , 'Ê', 'E')
,ReplaceForEachLetterCase = REPLACE(REPLACE(surname,'Ê','E'),'ê','e')
,SQLServer2017Version = TRANSLATE(surname,'êÊ','eE')
FROM #table
Results
surname
YourOriginalCode
ForceCaseInsensitiveCollation
ReplaceForEachLetterCase
SQLServer2017Version
abcdê
abcdê
abcdE
abcde
abcde
ABCDÊ
ABCDE
ABCDE
ABCDE
ABCDE
This sort of issue usually comes down to how SQL stores unicode characters.
Try running the following to see what sort of output you get. On my server both work fine, but it may well help you identify your issue.
DECLARE #table AS TABLE ( surname VARCHAR(MAX) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL )
INSERT INTO #table ( surname )
VALUES ( 'ABCDÊ' )
, ( N'ABCDÊ' )
SELECT surname
, REPLACE(surname, 'Ê', 'E')
FROM #table
Related
Database: Azure Dedicated SQL Pool
Collation: SQL_Latin1_General_CP1_CI_AS
create table testtable (timeenter nvarchar(30))
insert into testtable values('08:19:21''')
insert into testtable values('08:19:21$')
insert into testtable values('08:19:21%')
insert into testtable values('08:19:21(')
insert into testtable values('08:19:21"')
Here's my problem, when I sort the values, I get this:
select *
from (
select timeenter
, ascii(right(timeenter,1)) as ascvalue
, right(timeenter,1) as symbol
from testtable
) x
order by timeenter
Result set:
timeenter ascvalue symbol
------------------------------ ----------- ------
08:19:21' 39 '
08:19:21" 34 "
08:19:21$ 36 $
08:19:21% 37 %
08:19:21( 40 (
When I order by with this: order by timeenter collate Latin1_General_BIN or order by timeenter collate SQL_Latin1_General_CP850_BIN, it sorts correctly:
timeenter ascvalue symbol
------------------------------ ----------- ------
08:19:21" 34 "
08:19:21$ 36 $
08:19:21% 37 %
08:19:21' 39 '
08:19:21( 40 (
I looked up SQL_Latin1_General_CP1_CI_AS collation and CP1 shows that the character set is properly ordered but the sort order is still irregular in my instance for my query.
Is it due to the fact that the apostrophe (') is a delimiter to SQL?
Any insight is greatly appreciated.
The answer i'm looking for is how and why my instance is sorting the way it is based on the current collation. I'm able to make the data sort the way i want via tsql easily. I'm only looking for an explanation in regards to the collation and how it behaves with apostrophes per my example.
This is odd. " < ' in SQL_Latin1_General_CP1_CI_AS, as you can see
select case when '''' collate SQL_Latin1_General_CP1_CI_AS < '"' collate SQL_Latin1_General_CP1_CI_AS then 1 else 0 end
outputs
0
But if they are unicode characters, the order is reversed:
select case when N'''' collate SQL_Latin1_General_CP1_CI_AS < N'"' collate SQL_Latin1_General_CP1_CI_AS then 1 else 0 end
outputs
1
Here's a sample table
FirstName LastName UserName
----------------------------------
Bob Jones bjones
null null tomroberts
null Ricardo rricardo
Robby null robroy
George Glass gglass
I'm looking for a select that'll return
Jones, Bob
tomroberts
rricardo
robroy
Glass, George
If the first name OR last name are null or empty strings, I want to return the user name. Otherwise I want to return the "lastname, firstname"
Can someone help with the query?
Thanks!
An alternative to using a CASE statement is to use the COALESCE function which returns the first non-NULL argument.
SELECT
COALESCE(NULLIF(LastName,'') + ',' + NULLIF(FirstName,''), UserName)
FROM
MyTable
Note:- It's important to use the + to concat the columns as this will return NULL if any column contains null. The CONCAT function will return a value that contains the non-NULL parts.
You can use a CASE expression and a couple of NULLIFs to do this:
SELECT CASE WHEN NULLIF(FirstName,'') IS NULL
OR NULLIF(LastName,'') IS NULL THEN [UserName]
ELSE CONCAT(LastName, ', ', FirstName)
END
FROM dbo.YourTable;
Alternatively, if you prefer, you could use ISNULL({Value},'') = ''. Though I would personally suggest you don't allow someone to have a zero length name in your data; such a value should be stored as NULL.
You can use a simple ISNULL statement:
DECLARE #t TABLE(
firstname NVARCHAR(50)
,lastname NVARCHAR(50)
,username NVARCHAR(50)
)
INSERT INTO #t VALUES
('Bob', 'Jones', 'bjones')
,(null, null, 'tomroberts')
,(null, 'Ricardo', 'rricardo')
,('Robby', null, 'robroy')
,('George', 'Glass', 'gglass')
SELECT ISNULL(NULLIF(TRIM(lastname),'') + ', ' + NULLIF(TRIM(firstname),''), username)
FROM #t
Concatenating a string via + results in NULL whenever one of the values is NULL.
In Arabic there are 2 letters that pronounced the same but written differently
The letter ة
and the letter ت
I wanted to replace the letter ة with another letter ه
Now I used this
Update MyTable
SET MyColumn = Replace ( MyColumn, N'ة' , N'ه' )
But ended with replacing every letter that has ة or ت to be replaced with ه
How can I tell SQL Server to replace only ة Not ت ?
Specify a COLLATE clause with a binary collation to use the code points of the exact characters to be searched/replaced:
UPDATE dbo.MyTable
SET MyColumn = REPLACE( MyColumn COLLATE Arabic_BIN, N'ة' COLLATE Arabic_BIN, N'ه' COLLATE Arabic_BIN);
I use query to select name with all small letters but he gets all names with small letters and upper letter
this is what I used
select Name from _Client where Name = 'mrmiro'
I got 2 names with small and upper letters like this
mrmiro
MrMiro
I just want to select exactly what I searched
Small matter of changing COLLATE in your query
Declare #Table table (Name varchar(25))
Insert Into #Table values
('mrmiro'),
('MrMiro')
Select *
From #Table
Where Name = 'mrmiro' COLLATE SQL_Latin1_General_CP1_CS_AS
Returns
Name
mrmiro
I'm trying to find strings that are not just distinct, but where that sequence of characters in string is distinct when compared to other values for that field. Basically I'm only trying to find values that are not contained in any of the other values for that column. I've spent a while trying to figure out how to do this but haven't gotten anywhere.
If I understand correctly, you may try something like:
declare #table table (wordid int identity, word varchar(50))
insert into #table values
('abc')
,('abcd')
,('ef')
,('abcdef')
,('ghi')
,('klm')
,('zxcvb')
select word
from #table t
where not exists (
select 1 from #table t2
where charindex(t.word, t2.word) > 0 and t2.wordid != t.wordid
)
Output:
word
---------
abcdef
ghi
klm
zxcvb