How to trim chinese and japanses characters - sql

SELECT LOWER(LTRIM(RTRIM(' 価格 '))) AS TrimmedString;
It gives ?? as output.
How to put it here in function:
Create OR ALTER Function dbo.udf_GetTrimAndLower (#input NVARCHAR(max))
Returns NVARCHAR(max)
AS
BEGIN
DECLARE #output NVARCHAR(max);
select #output = LOWER(LTRIM(RTRIM(#input)));
RETURN (#output);
END

Try using this. I've added extra spaces for display purpose.
You have to use the nvarchar data type.
SELECT LTRIM(RTRIM(N' 価格 ')) AS TrimmedString;

Related

Mimic STRING_SPLIT without custom function in SQL Server 2008

It is asked many times, but not this way.
I am on SQL Server 2008, and there is no STRING_SPLIT function (like in 2016).
A query returns with the following row, see below a single example row. What you see below in bald is a single field actually, so one varchar column has it altogether:
Appple|10|admin|845687|Apr|26|11:32:29|2016|AwesomeApplication.zip
which I'd like to be split by the pipe | character.
I cannot write a CTE for this, or a custom function.
I have to extract the individual pipe delimited elements, into different columns, within one select statement using the built in string functions like CHARINDEX, PATINDEX.
Does anybody have any idea?
DECLARE #Result Table(Value varchar(50))
DECLARE #x XML
SELECT #X = CAST('<A>' + REPLACE(#StringList, '|', '</A><A>') + '</A>' AS XML)
INSERT INTO #Result
SELECT t.value('.', 'varchar(50)') as inVal
FROM #X.nodes('/A') AS x(t)
This will create a table with one column (Value). Each split value from your pipe-delimited string will create a new record in this table. Then you can join to it however you'd like. Please let me know if this is unclear or if it doesn't work on SQL 2008.
You can increase the size of the varchar, if needed - and you can modify the query to split on different values (comma-delimited, etc.).
ALTER FUNCTION [dbo].[split]
(
#string varchar(max),
#separator varchar(1) -- longer separator is also possible
)
RETURNS
#result TABLE (keyword varchar(max) )
AS
BEGIN
declare #pos int=0, #res varchar(100)
while len(#string)>0
begin
set #pos=CHARINDEX(#separator, #string+#separator,0)
select #res=left(#string,#pos),#string=SUBSTRING(#string,#pos+1,100)
insert #result values(#res)
end
RETURN
END
It's been a long while since this question was asked, and although the OP wanted a non-function solution, this is the only post where the answer pointed me in the right direction for me to write my code, so I thought I'd share my solution here.
What it does
This code, checks the master db compatibility mode and creates an appropriate _my_string_split function. If string_split exists, this is just a wrapper.
If not it will use the method Stan proposed in the accepted answer
So now, regardless of where I'm running my code, after creating the function, all I need to do is use master.dbo._my_string_split, for example:
SELECT * FROM master.dbo._my_string_split('Hello|World!','|')
The Code
USE master
IF OBJECT_ID('dbo._my_string_split') IS NOT NULL DROP FUNCTION [dbo].[_my_string_split]
GO
DECLARE #sqlCode NVARCHAR(MAX);
IF 130 <= (SELECT compatibility_level FROM sys.databases WHERE name = DB_NAME())
SET #sqlCode = '
--- 130+ string_split exists
CREATE FUNCTION [dbo].[_my_string_split]
(
#string nvarchar(max),
#separator nvarchar(1)
)
RETURNS
#result TABLE ([Value] nvarchar(max) )
BEGIN
INSERT INTO #result
SELECT [Value] FROM string_split(#string,#separator)
RETURN
END
'
ELSE
SET #sqlCode = '
--- before 130: string_split does not exists
CREATE FUNCTION [dbo].[_my_string_split]
(
#string nvarchar(max),
#separator nvarchar(1)
)
RETURNS
#result TABLE ([Value] nvarchar(max) )
BEGIN
INSERT INTO #result
SELECT [Value]=t.value(''.'', ''varchar(max)'')
FROM (SELECT x=(CAST((''<A>'' + REPLACE(#string, #separator, ''</A><A>'') + ''</A>'') AS XML))) a
CROSS APPLY a.x.nodes(''/A'') AS x(t)
RETURN
END
'
PRINT #sqlCode
EXEC sp_sqlexec #sqlCode
Hope others will find this useful.

Sql sever Replace function

I have file Location in my table like
FileLocation :- "\Saurabh\Rahul\Saurabh\ABC.text"
I need to replace "Saurabh" with "Ramesh" but i need to replace first "Saurabh" word only not all the "saurabh" which is in the string.
i tried with
select REPLACE(FILELOCATION,'saurabh','Ramesh')
How can i achive ?
try this:
You can Use STUFF and CHARINDEX functions.
DECLARE #str varchar(100) = '\Saurabh\Rahul\Saurabh\ABC.text'
SELECT STUFF(#str, CHARINDEX('saurabh', #str), LEN('saurabh'), '')
OUTPUT:
\\Rahul\Saurabh\ABC.text
To replace only the first occurence of a word in a string you can write as:
DECLARE #FileLocation VARCHAR(MAX)
DECLARE #ReplaceSubString VARCHAR(MAX),#NewSubString VARCHAR(MAX)
SET #FileLocation = '\\Saurabh\Rahul\Saurabh\ABC.text'
SET #ReplaceSubString = 'Saurabh'
SET #NewSubString = 'Ramesh'
SELECT STUFF ( #FileLocation ,
CHARINDEX(#ReplaceSubString, #FileLocation) ,
Len(#ReplaceSubString) ,
#NewSubString
)
Demo

Why has different output of function replace?

declare #tachareName varchar(200)
,#a varchar
set #tachareName='fsfhk,fsif,'
if(CHARINDEX(',',#tachareName)!=0)
--print #tachareName
--select REPLACE(#tachareName,',',' ')--The output is:fsfhk fsif
set #a=REPLACE(#tachareName,',',' ')
--REPLACE(#tachareName,',',' ')
print #a --The output is:f
What's wrong,i want to judge if the string has ',' symbal and repalce it with ' '.
The envirioment is SQL Server 2008.
Because of your declaration of #a - you have not declared length of variable so it defaulted to 1. Thus returning only the first letter of result.
Declare #a as:
declare #a varchar(200)
Read more about char and varchar declaration on MSDN.

SQL Server: How do you remove punctuation from a field?

Any one know a good way to remove punctuation from a field in SQL Server?
I'm thinking
UPDATE tblMyTable SET FieldName = REPLACE(REPLACE(REPLACE(FieldName,',',''),'.',''),'''' ,'')
but it seems a bit tedious when I intend on removing a large number of different characters for example: !##$%^&*()<>:"
Thanks in advance
Ideally, you would do this in an application language such as C# + LINQ as mentioned above.
If you wanted to do it purely in T-SQL though, one way make things neater would be to firstly create a table that held all the punctuation you wanted to removed.
CREATE TABLE Punctuation
(
Symbol VARCHAR(1) NOT NULL
)
INSERT INTO Punctuation (Symbol) VALUES('''')
INSERT INTO Punctuation (Symbol) VALUES('-')
INSERT INTO Punctuation (Symbol) VALUES('.')
Next, you could create a function in SQL to remove all the punctuation symbols from an input string.
CREATE FUNCTION dbo.fn_RemovePunctuation
(
#InputString VARCHAR(500)
)
RETURNS VARCHAR(500)
AS
BEGIN
SELECT
#InputString = REPLACE(#InputString, P.Symbol, '')
FROM
Punctuation P
RETURN #InputString
END
GO
Then you can just call the function in your UPDATE statement
UPDATE tblMyTable SET FieldName = dbo.fn_RemovePunctuation(FieldName)
I wanted to avoid creating a table and wanted to remove everything except letters and digits.
DECLARE #p int
DECLARE #Result Varchar(250)
DECLARE #BadChars Varchar(12)
SELECT #BadChars = '%[^a-z0-9]%'
-- to leave spaces - SELECT #BadChars = '%[^a-z0-9] %'
SET #Result = #InStr
SET #P =PatIndex(#BadChars,#Result)
WHILE #p > 0 BEGIN
SELECT #Result = Left(#Result,#p-1) + Substring(#Result,#p+1,250)
SET #P =PatIndex(#BadChars,#Result)
END
I am proposing 2 solutions
Solution 1: Make a noise table and replace the noises with blank spaces
e.g.
DECLARE #String VARCHAR(MAX)
DECLARE #Noise TABLE(Noise VARCHAR(100),ReplaceChars VARCHAR(10))
SET #String = 'hello! how * > are % u (: . I am ok :). Oh nice!'
INSERT INTO #Noise(Noise,ReplaceChars)
SELECT '!',SPACE(1) UNION ALL SELECT '#',SPACE(1) UNION ALL
SELECT '#',SPACE(1) UNION ALL SELECT '$',SPACE(1) UNION ALL
SELECT '%',SPACE(1) UNION ALL SELECT '^',SPACE(1) UNION ALL
SELECT '&',SPACE(1) UNION ALL SELECT '*',SPACE(1) UNION ALL
SELECT '(',SPACE(1) UNION ALL SELECT ')',SPACE(1) UNION ALL
SELECT '{',SPACE(1) UNION ALL SELECT '}',SPACE(1) UNION ALL
SELECT '<',SPACE(1) UNION ALL SELECT '>',SPACE(1) UNION ALL
SELECT ':',SPACE(1)
SELECT #String = REPLACE(#String, Noise, ReplaceChars) FROM #Noise
SELECT #String Data
Solution 2: With a number table
DECLARE #String VARCHAR(MAX)
SET #String = 'hello! & how * > are % u (: . I am ok :). Oh nice!'
;with numbercte as
(
select 1 as rn
union all
select rn+1 from numbercte where rn<LEN(#String)
)
select REPLACE(FilteredData,' ',SPACE(1)) Data from
(select SUBSTRING(#String,rn,1)
from numbercte
where SUBSTRING(#String,rn,1) not in('!','*','>','<','%','(',')',':','!','&','#','#','$')
for xml path(''))X(FilteredData)
Output(Both the cases)
Data
hello how are u . I am ok . Oh nice
Note- I have just put some of the noises. You may need to put the noises that u need.
Hope this helps
You can use regular expressions in SQL Server - here is an article based on SQL 2005:
http://msdn.microsoft.com/en-us/magazine/cc163473.aspx
I'd wrap it in a simple scalar UDF so all string cleaning is in one place if it's needed again.
Then you can use it on INSERT too...
I took Ken MC's solution and made it into an function which can replace all punctuation with a given string:
----------------------------------------------------------------------------------------------------------------
-- This function replaces all punctuation in the given string with the "replaceWith" string
----------------------------------------------------------------------------------------------------------------
IF object_id('[dbo].[fnReplacePunctuation]') IS NOT NULL
BEGIN
DROP FUNCTION [dbo].[fnReplacePunctuation];
END;
GO
CREATE FUNCTION [dbo].[fnReplacePunctuation] (#string NVARCHAR(MAX), #replaceWith NVARCHAR(max))
RETURNS NVARCHAR(MAX)
BEGIN
DECLARE #Result Varchar(max) = #string;
DECLARE #BadChars Varchar(12) = '%[^a-z0-9]%'; -- to leave spaces - SELECT #BadChars = '%[^a-z0-9] %'
DECLARE #p int = PatIndex(#BadChars,#Result);
DECLARE #searchFrom INT;
DECLARE #indexOfPunct INT = #p;
WHILE #indexOfPunct > 0 BEGIN
SET #searchFrom = LEN(#Result) - #p;
SET #Result = Left(#Result, #p-1) + #replaceWith + Substring(#Result, #p+1,LEN(#Result));
SET #IndexOfPunct = PatIndex(#BadChars, substring(#Result, (LEN(#Result) - #SearchFrom)+1, LEN(#Result)));
SET #p = (LEN(#Result) - #searchFrom) + #indexOfPunct;
END
RETURN #Result;
END;
GO
-- example:
SELECT dbo.fnReplacePunctuation('This is, only, a tést-really..', '');
Output:
Thisisonlyatéstreally
If it's a one-off thing, I would use a C# + LINQ snippet in LINQPad to do the job with regular expressions.
It is quick and easy and you don't have to go through the process of setting up a CLR stored procedure and then cleaning up after yourself.
Can't you use PATINDEX to only include NUMBERS and LETTERS instead of trying to guess what punctuation might be in the field? (Not trying to be snarky, if I had the code ready, I'd share it...but this is what I'm looking for).
Seems like you need to create a custom function in order to avoid a giant list of replace functions in your queries - here's a good example:
http://www.codeproject.com/KB/database/SQLPhoneNumbersPart_2.aspx?display=Print

TSQL using a wildcard in a where clause with dynamic sql

It appears that using the LIKE in a condition with wildcards and a variable inside of dynamic sql doesn't work, although it doesn't give an error. Here's an example.
The column called code has values like A0B01C02,A0B02C2D05,A0B02C2D05, etc and I am trying to match on rows containing a subset like 'B1'. When I do this it works and returns results as expected.
set #sql='select * from table where code like ''%B01%'''
exec sp_executesql #sql
If I hardcode the value of the variable
set #code='B01'
and modify the sql statement to concatenate the quotes and wildcards:
set #sql='select * from table where code like ' +''''+ '%'+#code + '%' + ''''
exec sp_executesql #sql
This returns the results as expected, but I had to hard code the variable. However, when I need to make the match using a variable for B01 and that the variable is set with a select statement, I don't get any results returned. I define an nvarchar like this:
set #code=(select top 1 code from anotherTable where USERID=#PersonId)
I confirmed that the select statement above returns the expected code, however. There is no error, but the query is "executed successfully". Am I missing something in the syntax for the where clause?
You can find a discussion of this at http://ask.sqlservercentral.com/questions/275/dynamic-where-clause-how-can-i-use-a-variable-in-an-in-predicate/312#312
My answer was to do the Parse By Comma Function.
/*****************************************************************
**** Parse A Comma Delimited String Into A Table
*****************************************************************/
ALTER FUNCTION [dbo].[ParseByComma] (
#String VARCHAR(600) )
RETURNS #TblSubString TABLE
(
VarSubString VARCHAR(50)
)
AS
BEGIN
DECLARE #intPos INT,
#SubStr VARCHAR(50)
-- Remove All Spaces
SET #String = REPLACE(#String, ' ','')
-- Find The First Comma
SET #IntPos = CHARINDEX(',', #String)
-- Loop Until There Is Nothing Left Of #String
WHILE #IntPos > 0
BEGIN
-- Extract The String
SET #SubStr = SUBSTRING(#String, 0, #IntPos)
-- Insert The String Into The Table
INSERT INTO #TblSubString (VarSubString) VALUES (#SubStr)
-- Remove The String & Comma Separator From The Original
SET #String = SUBSTRING(#String, LEN(#SubStr) + 2, LEN(#String) - LEN(#SubStr) + 1)
-- Get The New Index To The String
SET #IntPos = CHARINDEX(',', #String)
END
-- Return The Last One
INSERT INTO #TblSubString (VarSubString) VALUES (#String)
RETURN
END
I do not have a SQL Server in front of me at the moment but I wonder if this syntax might work for you?
set #sql='SELECT * FROM table WHERE UPPER(code) LIKE ''%''||(UPPER(COALESCE('''||#code||''',code)))||''%'' '
exec sp_executesql #sql
Best regards,
Kevin