Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
661 views
in Technique[技术] by (71.8m points)

tsql - Convert to ASCII char in sql server

In Oracle I can convert ?êíABCDE to AEIABCDE using:

SELECT CONVERT('?êíABCDE', 'US7ASCII') from dual;

Output:

AEIABCDE

How can I do the same in SQL Server?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can try following query:

 SELECT CAST('?êíABCDE' AS varchar(100)) COLLATE SQL_Latin1_General_CP1253_CI_AI

Caveat:

This does not support UNICODE strings so do not replace varchar with nvarchar

demo sql fiddle: http://sqlfiddle.com/#!6/9eecb7/2016

Explanation:

Read about collate on MSDN here: https://msdn.microsoft.com/en-us/library/ms184391.aspx

Edit:

On comment

if '?êíABCDE' = CAST('?êíABCDE' AS varchar(100)) COLLATE SQL_Latin1_General_CP1253_CI_AI print 'same' else print 'not same' prints same. Why??

Collation is forced in the WHERE condition which collates both side of comparison. If you need not same as result, try below

declare @t varchar
set @t= CAST('?êíABCDE' AS varchar(100)) 
COLLATE SQL_Latin1_General_CP1253_CI_AI
select 
case 
when '?êíABCDE' like  @t
then 'same' else  'not same' end as result

demo link: http://sqlfiddle.com/#!6/9eecb7/2022


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...