[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 74
  • Last Modified:

sql server rotate through word

I had this question after viewing sql server query.

The above id works perfectly and is at sentence level.

what I would like now to do is rotate just through a word alone and get its sequence.

I have query that gives this:
ClientCode      word
54553002      OCEAN
54553002      RACE
54553002      T6
54553002      XC90

for each word I would like to grab the sequence of chars ascii so ocean has a column with
O=79
C=67
E=69
A=65
N=78

79,67, 69, 65, 78

the following gets the ascii code already.
USE [Dictionary]
GO
/****** Object:  UserDefinedFunction [dbo].[func_ASCIIEncodeString]    Script Date: 15/09/2016 16:44:44 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

--Create the function
ALTER FUNCTION [dbo].[func_ASCIIEncodeString] (@inputString VARCHAR(15))
    RETURNS INT
AS
BEGIN
    -- Create the variables for the current character string position
    DECLARE @position INT = 1;  
    DECLARE @ASCIIEncodedValue INT = 0;

    -- Initialize the variables.  
    SET @position = 1;
    WHILE @position <= DATALENGTH(@inputString)  
       BEGIN  
          --Debug Point
          --SELECT ASCII(SUBSTRING(@inputString, @position, 1)) AS ASCIIValue,  
             --    CHAR(ASCII(SUBSTRING(@inputString, @position, 1))) AS CharacterValue

          --Add the ASCII value of the currently read character into the return variable
          SELECT @ASCIIEncodedValue += ASCII(SUBSTRING(@inputString, @position, 1));
          SET @position = @position + 1;
       END

    --Finally return to caller
    RETURN @ASCIIEncodedValue;
END

Open in new window

0
PeterBaileyUk
Asked:
PeterBaileyUk
1 Solution
 
ZberteocCommented:
First create this function:
CREATE FUNCTION [dbo].[fnTally]()
RETURNS TABLE 
AS
/*******************************************************************************\
Function	: fnTally

Purpose		: returns a set with numbers from 1 to 10,000 
			  to be used in parsing and sequential data generation whithout loop
			  
Parameters	: no parameters

Invoke		:
	
		select * from [dbo].[fnTally]()
		select N from [dbo].[fnTally]()
		select substring('abcdef',N,1) as chr from [dbo].[fnTally]() where N<len('abcdef') -- parsing a string
		select dateadd(dd, N, '2007-01-01') as dte from [dbo].[fnTally]() --gets dates for about 30 years

\*******************************************************************************/
RETURN
	WITH 
	E1(N) AS 
	( --10E+1 or 10 rows
		 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
		 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
		 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
	),                         
   E2(N) AS 
   ( --10E+2 or 100 rows	
		SELECT 1 FROM E1 a, E1 b
	),
   E4(N) AS 
   ( --10E+4 or 10,000 rows max
		SELECT 1 FROM E2 a, E2 b
	)
			 SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) as N FROM E4
	;

GO

Open in new window

And then use it like this:
declare
	@string varchar(1000)='what I would like now to do is rotate just through a word alone and get its sequence'

select 
	(
		select 
			cast(ascii(substring(@string,N,1)) as varchar(5))+','
		from 
			[zb_dba_maint].[dbo].[fnTally]() nr
		where
			nr.N<=len(@string) 
		FOR XML PATH('')
	) as ascii_seq

-- result:
ascii_seq
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
119,104,97,116,32,73,32,119,111,117,108,100,32,108,105,107,101,32,110,111,119,32,116,111,32,100,111,32,105,115,32,114,111,116,97,116,101,32,106,117,115,116,32,116,104,114,111,117,103,104,32,97,32,119,111,114,100,32,97,108,111,110,101,32,97,110,100,32,103,101,116,32,105,116,115,32,115,101,113,117,101,110,99,101,

Open in new window

0
 
PeterBaileyUkAuthor Commented:
thank you
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now