Replace character in a string with space - sql-server-2008

I need the following output in SQL server 2008
problem is to remove numbers from string
i.e column name is associated_id which is nvarchar type and value could be (23,34,45)
But I want the following result (23,45).

May this help
CREATE FUNCTION [dbo].[GetAlphabetFromAlphaNumeric]
(#strAlphaNumeric VARCHAR(500))
RETURNS VARCHAR(500)
AS
BEGIN
DECLARE #intAlpha INT
SET #intAlpha = PATINDEX('%[0-9]%', #strAlphaNumeric)
BEGIN
WHILE #intAlpha > 0
BEGIN
SET #strAlphaNumeric = STUFF(#strAlphaNumeric, #intAlpha, 1, '' )
SET #intAlpha = PATINDEX('%[0-9]%', #strAlphaNumeric )
END
END
RETURN ISNULL(#strAlphaNumeric,0)
END

Related

Is it possible to search of a text in SSRS via Filters?

I would like to have a multiple values Paramter with text1, text2, text3
SSRS should only show me the rows that contain one or all of this Parameter values in a column.
I notice you can set a filter in a Dataset or the tablix. The problem is I do not have something a function that do both LIKE and IN
Do you someone have an idea?
I tried already the LIKE function and the VALUE =”*”+”Parameters!PAR.Value”+”*”.
It did work, but not on a multiple values parameter.
I don't think you will be able to do this using dataset filters using a multi-value parameter.
However, you should be able to change your dataset query to handle this.
CREATE PROC myProc (#myParameter varchar(1000))
AS
SELECT * FROM myTable t
JOIN string_split(#myParameter, ',') p on t.myColumn like '%' + p.value + '%'
When you pass the parameter to the stored proc you can use the following expression
=JOIN(Parameters!myParameter.Value, ",")
This will simply join each of your parameter values into a single comma separated string string ready to pass to the procedure.
If you do not have a version of SQL Server that support string_split
Hers's the code to create one. There are smaller versions around on the internet but this one was designed to handle special cases as well as the typical single character delimiters.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [Split](#sText varchar(8000), #sDelim varchar(20) = ' ')
RETURNS #retArray TABLE (idx smallint Primary Key, value varchar(8000))
AS
BEGIN
DECLARE #idx smallint,
#value varchar(8000),
#bcontinue bit,
#iStrike smallint,
#iDelimlength tinyint
IF #sDelim = 'Space'
BEGIN
SET #sDelim = ' '
END
SET #idx = 0
SET #sText = LTrim(RTrim(#sText))
SET #iDelimlength = DATALENGTH(#sDelim)
SET #bcontinue = 1
IF NOT ((#iDelimlength = 0) or (#sDelim = 'Empty'))
BEGIN
WHILE #bcontinue = 1
BEGIN
--If you can find the delimiter in the text, retrieve the first element and
--insert it with its index into the return table.
IF CHARINDEX(#sDelim, #sText)>0
BEGIN
SET #value = SUBSTRING(#sText,1, CHARINDEX(#sDelim,#sText)-1)
BEGIN
INSERT #retArray (idx, value)
VALUES (#idx, #value)
END
--Trim the element and its delimiter from the front of the string.
--Increment the index and loop.
SET #iStrike = DATALENGTH(#value) + #iDelimlength
SET #idx = #idx + 1
SET #sText = LTrim(Right(#sText,DATALENGTH(#sText) - #iStrike))
END
ELSE
BEGIN
--If you can't find the delimiter in the text, #sText is the last value in
--#retArray.
SET #value = #sText
BEGIN
INSERT #retArray (idx, value)
VALUES (#idx, #value)
END
--Exit the WHILE loop.
SET #bcontinue = 0
END
END
END
ELSE
BEGIN
WHILE #bcontinue=1
BEGIN
--If the delimiter is an empty string, check for remaining text
--instead of a delimiter. Insert the first character into the
--retArray table. Trim the character from the front of the string.
--Increment the index and loop.
IF DATALENGTH(#sText)>1
BEGIN
SET #value = SUBSTRING(#sText,1,1)
BEGIN
INSERT #retArray (idx, value)
VALUES (#idx, #value)
END
SET #idx = #idx+1
SET #sText = SUBSTRING(#sText,2,DATALENGTH(#sText)-1)
END
ELSE
BEGIN
--One character remains.
--Insert the character, and exit the WHILE loop.
INSERT #retArray (idx, value)
VALUES (#idx, #sText)
SET #bcontinue = 0
END
END
END
RETURN
END
GO
To test this try something like
select * from Split('abc,def,ghi', ',')
which will return

Function to check if a string contains uppercase characters?

I need to create a SQL Server function to check if an input string contains any uppercase characters. If yes then it should return "OK". If no It should return "NOT OKAY".
When the below statements are executed, it should return the expected values as below.
PRINT dbo.CheckStringOfUpperAlphaOK('abc') -- Expected: "NOT OK"
PRINT dbo.CheckStringOfUpperAlphaOK('ABC') -- Expected: "OK"
this is what i tried, but i do not know to specify the return values as mentioned above.
CREATE FUNCTION CheckStringOfUpperAlphaOK (#String varchar(MAX))
RETURNS VARCHAR(6)
AS
BEGIN
Declare #KeepValues as varchar(50)
Set #KeepValues = '%[^ ][A-Z]%'
While PatIndex(#KeepValues collate Latin1_General_Bin, #Temp) > 0
Set #Temp = Stuff(#Temp, PatIndex(#KeepValues collate
Latin1_General_Bin, #Temp) + 1, 0, ' ')
RETURN #Temp
END
How about this? I just simply compare the #String parameter to a (simplified) pattern that checks for an uppercase character - and based on that index, I define the return values and return the appropriate response:
CREATE FUNCTION CheckStringOfUpperAlphaOK(#String varchar(MAX))
RETURNS VARCHAR(6)
AS
BEGIN
DECLARE #UpperCasePos BIGINT
SELECT #UpperCasePos = PATINDEX('%[A-Z]%' collate Latin1_General_Bin, #String)
DECLARE #Response VARCHAR(6)
IF #UpperCasePos > 0
SET #Response = 'OK'
ELSE
SET #Response = 'NOT OK'
RETURN #Response
END
If you want to know if the string contains at least one upper-case character, why not do a case-sensitive comparison with the lower-caser version of the string?
CREATE FUNCTION CheckStringOfUpperAlphaOK
(
#STR VARCHAR(MAX)
)
RETURNS VARCHAR(6)
AS
BEGIN
RETURN CASE
WHEN #STR = LOWER(#STR) COLLATE Latin1_General_Bin THEN 'NOT OK'
ELSE 'OK'
END
END
;

How to convert TSQL query into MYSQL query?

I have developed a function for split string in tsql but mysql don't have some built in functions. I needed to function in MYSQL as i am new in mysql. Function should accept 2 parameters
1. String to be split
2. separator (',' or whatever)
Kindly reply me.
i had found solution on the internet you can into that.
DELIMITER //
DROP FUNCTION IF EXISTS `splitAndTranslate` //
CREATE FUNCTION splitAndTranslate(str TEXT, delim VARCHAR(124))
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE i INT DEFAULT 0; -- total number of delimiters
DECLARE ctr INT DEFAULT 0; -- counter for the loop
DECLARE str_len INT; -- string length,self explanatory
DECLARE out_str text DEFAULT ''; -- return string holder
DECLARE temp_str text DEFAULT ''; -- temporary string holder
DECLARE temp_val VARCHAR(255) DEFAULT ''; -- temporary string holder for query
-- get length
SET str_len=LENGTH(str);
SET i = (LENGTH(str)-LENGTH(REPLACE(str, delim, '')))/LENGTH(delim) + 1;
-- get total number delimeters and add 1
-- add 1 since total separated values are 1 more than the number of delimiters
-- start of while loop
WHILE(ctr<i) DO
-- add 1 to the counter, which will also be used to get the value of the string
SET ctr=ctr+1;
-- get value separated by delimiter using ctr as the index
SET temp_str = REPLACE(SUBSTRING(SUBSTRING_INDEX(str, delim, ctr), LENGTH(SUBSTRING_INDEX(str, delim,ctr - 1)) + 1), delim, '');
-- query real value and insert into temporary value holder, temp_str contains the exploded ID
SELECT <real_value_column> INTO temp_val FROM <my_table> WHERE <table_id>=temp_str;
-- concat real value into output string separated by delimiter
SET out_str=CONCAT(out_str, temp_val, ',');
END WHILE;
-- end of while loop
-- trim delimiter from end of string
SET out_str=TRIM(TRAILING delim FROM out_str);
RETURN(out_str); -- return
END//
reference http://www.slickdev.com/2008/09/15/mysql-query-real-values-from-delimiter-separated-string-ids/
In mysql they they dont support some functionality like sqlserver. so spliting will be difficult in mysql
SELECT e.`studentId`, SPLIT(",", c.`courseNames`)[e.`courseId`]
FROM ..
SELECT TRIM(SUBSTRING_INDEX(yourcolumn,',',1)), TRIM(SUBSTRING_INDEX(yourcolumn,',',-1)) FROM yourtable
CREATE FUNCTION [dbo].[SplitString]
(
#RowData nvarchar(2000),
#SplitOn nvarchar(5)
)
RETURNS #RtnValue table
(
--Id int identity(1,1),
Data nvarchar(100)
)
AS
BEGIN
Declare #Cnt int
Set #Cnt = 1
While (Charindex(#SplitOn,#RowData)>0)
Begin
Insert Into #RtnValue (data)
Select
Data = ltrim(rtrim(Substring(#RowData,1,Charindex(#SplitOn,#RowData)-1)))
Set #RowData = Substring(#RowData,Charindex(#SplitOn,#RowData)+1,len(#RowData))
Set #Cnt = #Cnt + 1
End
Insert Into #RtnValue (data)
Select Data = ltrim(rtrim(#RowData))
Return
END

sql create function error code 1064

I need to create a function that I found here: http://vyaskn.tripod.com/code/propercase.txt It converts text to "ProperCase," first letter of every word to uppercase.
CREATE FUNCTION PROPERCASE
(
-- The string to be converted to proper case
#input VARCHAR( 8000 )
)
-- This function returns the proper case string of varchar type
RETURNS VARCHAR( 8000 )
AS
BEGIN
IF #input IS NULL
BEGIN
-- Just return NULL if input string is NULL
RETURN NULL
END
-- Character variable declarations
DECLARE #output VARCHAR( 8000 )
-- Integer variable declarations
DECLARE #ctr INT, #len INT, #found_at INT
-- Constant declarations
DECLARE #LOWER_CASE_a INT, #LOWER_CASE_z INT, #Delimiter CHAR(3), #UPPER_CASE_A INT, #UPPER_CASE_Z INT
-- Variable/Constant initializations
SET #ctr = 1
SET #len = LEN(#input)
SET #output = ''
SET #LOWER_CASE_a = 97
SET #LOWER_CASE_z = 122
SET #Delimiter = ' ,-'
SET #UPPER_CASE_A = 65
SET #UPPER_CASE_Z = 90
WHILE #ctr <= #len
BEGIN
-- This loop will take care of reccuring white spaces
WHILE CHARINDEX(SUBSTRING(#input,#ctr,1), #Delimiter) > 0
BEGIN
SET #output = #output + SUBSTRING(#input,#ctr,1)
SET #ctr = #ctr + 1
END
IF ASCII(SUBSTRING(#input,#ctr,1)) BETWEEN #LOWER_CASE_a AND #LOWER_CASE_z
BEGIN
-- Converting the first character to upper case
SET #output = #output + UPPER(SUBSTRING(#input,#ctr,1))
END
ELSE
BEGIN
SET #output = #output + SUBSTRING(#input,#ctr,1)
END
SET #ctr = #ctr + 1
WHILE CHARINDEX(SUBSTRING(#input,#ctr,1), #Delimiter) = 0 AND (#ctr <= #len)
BEGIN
IF ASCII(SUBSTRING(#input,#ctr,1)) BETWEEN #UPPER_CASE_A AND #UPPER_CASE_Z
BEGIN
SET #output = #output + LOWER(SUBSTRING(#input,#ctr,1))
END
ELSE
BEGIN
SET #output = #output + SUBSTRING(#input,#ctr,1)
END
SET #ctr = #ctr + 1
END
END
RETURN #output
END
I need a function to do that, but it's giving me error...
You are using MySQL, but the syntax you have is for SQL Server. Please read the documentation about MySQL's syntax and convert your procedure to use that syntax. The major constructs are the same, but the syntax is a little different. Here are some things to start with:
Local variables never start with #.
IF is condition followed by THEN followed by any number of lines of code, followed by END IF. The BEGIN...END construct is not used for IF statements in MySQL.
The functions are different. You won't use CHARINDEX but instead INSTR.
Here's the relevant MySQL documentation: http://dev.mysql.com/doc/refman/5.5/en/stored-routines-syntax.html and http://dev.mysql.com/doc/refman/5.5/en/sql-syntax-compound-statements.html.

How to split the string numbers and characters in asp.net

I am getting id like this(max id in Storedprocedure) "100254A"(it is string),
now i can split the string "100254" and "A" after generate the next string like 100254B.
how to write the code generate the next strings like 100254C,...100254Z after 100255A will coming pls give me any suggestion
Thank u
hemanth
Maybe this can help:
DECLARE #String VARCHAR(200)
SET #String = '100254E'
DECLARE #number INT,
#letter VARCHAR(5)
SET #number = CAST(LEFT(#String, LEN(#String) - 1) AS INT)
SET #letter= SUBSTRING(#String,LEN(#String), LEN(#String))
--Check if it is Z
IF(ASCII(#letter)=90)
BEGIN
SET #number=#number+1
SET #letter=CHAR(65)
END
ELSE
BEGIN
SET #letter=CHAR(ASCII(#letter)+1)
END
SELECT CAST(#number AS VARCHAR(100))+#letter AS new
SELECT #String AS old
Use substring function.
try this hope it will help You
declare #temvalue varchar(50)
declare #givenvalue varchar(50)
set #givenvalue ='100254A'
set #temvalue= substring(#givenvalue,len(#givenvalue),1)
set #givenvalue=substring(#givenvalue,0,len(#givenvalue))
declare #tableid int
while ASCII(#temvalue)<=90
begin
set #tableid=ASCII(#temvalue)
declare #data varchar(1)
set #data=CHAR(#tableid)
print #givenvalue+#data
if #givenvalue <>'100257' -- UP TO THIS IT WILL generate STRING
begin
if #tableid=90
begin
set #givenvalue= CAST(cast(#givenvalue as int)+1 as varchar(50))
set #tableid=64
end
end
set #tableid+=1
set #temvalue=CHAR(#tableid)
end