Print result by merging records in a table - sql

I have a table with name "PrintWord" and column name as col_letter and data in it is as follows:
"col_letter"
S
A
C
H
I
N
I would like to print the o/p from this table as:
SACHIN
Thanks!

DECLARE #t table
(
Name varchar(10)
)
INSERT INTO #t
SELECT 's' UNION ALL
SELECT 'a' UNION ALL
SELECT 'c' UNION ALL
SELECT 'h' UNION ALL
SELECT 'i' UNION ALL
SELECT 'n'
SELECT DISTINCT
stuff(
(
SELECT ' '+ [Name] FROM #t FOR XML PATH('')
),1,1,'')
FROM (SELECT DISTINCT Name FROM #t ) t

There is a hard-coded version :
SELECT col_letter
FROM PrintWord
ORDER BY
CASE col_letter
WHEN 'S' THEN 1
WHEN 'A' THEN 2
WHEN 'C' THEN 3
WHEN 'H' THEN 4
WHEN 'I' THEN 5
WHEN 'N' THEN 6
END
FOR XML PATH('')
You need an ORDER BY clause to guarantee the order of the letters.

Related

SQL to generate next sequence in an alphanumeric id

I gained some help from this question, but still need some further assistance.
I need to be able to generate the next available 2-digit alphanumeric code. I cannot change the table definition, before you ask. I am working in T-SQL.
So, for example, let's say I have the sequence
00, 01, 02,..., 09, 0A, 0B, 0C,..., 0Y, 0Z, 10, 11,...1Y, 1Z, 20, 21,..., 9Y, 9Z, I would like for the next id to be A0,
then A1, A2, ..., A9, AA, AB, AC, ..., AZ, I would like for the next id to be B0, then B1, etc.
So, in short, I would like to go from 00 all the way to ZZ and each time look for the MAX in that field and assign a new code 1 greater than the max. I would understand that A > 9, and the first column greater than the second, so A0 > 99 and AA > A9.
I wish I could just assign a numeric id to all of this, but the table definition is more critical at this point and so I'm not allowed to change it, so I am trying to maximize the available ids I'll have in such a limited space.
Thank you for your help.
Have a look at this. This is a really nasty problem for ID's. You've effectively limited yourself a low number of permutations of the key with 2 characters. Also you have a problem that you'll need to deal with if ZZ is used and this algorithm runs again. I have expanded these into as logical steps as possible for demonstration, but feel free to condense as needed.
DECLARE #ExistingTable TABLE (ID CHAR(2))
INSERT INTO #ExistingTable (ID) VALUES ('5A'),('5B')
DECLARE #NewID CHAR(2)
;WITH
Ranks AS (
SELECT '0' AS [Character] UNION SELECT '1' AS [Character] UNION SELECT '2' UNION SELECT '3' UNION SELECT '4' UNION SELECT '5' UNION SELECT '6' UNION
SELECT '7' UNION SELECT '8' UNION SELECT '9' UNION SELECT 'A' UNION SELECT 'B'UNION
SELECT 'C' UNION SELECT 'D' UNION SELECT 'E' UNION SELECT 'F' UNION SELECT 'G' UNION SELECT 'H' UNION
SELECT 'I' UNION SELECT 'J' UNION SELECT 'K' UNION SELECT 'L' UNION SELECT 'M' UNION SELECT 'N' UNION
SELECT 'O' UNION SELECT 'P' UNION SELECT 'Q' UNION SELECT 'R' UNION SELECT 'S' UNION SELECT 'T' UNION
SELECT 'U' UNION SELECT 'V' UNION SELECT 'W' UNION SELECT 'X' UNION SELECT 'Y' UNION SELECT 'Z'
), Permutations AS (
SELECT SecondChar.[Character] + FirstChar.[Character] AS PermuteID
FROM Ranks AS FirstChar
CROSS JOIN Ranks AS SecondChar
), PermutationsKeyed AS (
SELECT ROW_NUMBER() OVER (ORDER BY PermuteID ASC) AS PrimaryKeyHolder,
PermuteID
FROM Permutations
), MaxPK AS (
SELECT MAX(Perm.PrimaryKeyHolder) + 1 AS MaxPK
FROM #ExistingTable AS E
INNER JOIN PermutationsKeyed AS Perm ON (E.ID = Perm.PermuteID)
)
SELECT #NewID = Perm.PermuteID
FROM PermutationsKeyed AS Perm
INNER JOIN MaxPK AS M ON (Perm.PrimaryKeyHolder = M.MaxPK)
SELECT #NewID
I'm not sure how you wanted to go about returning the next value but I think this a simple and efficient ways to get all your values. Let me know if you need anything else.
DECLARE #values TABLE (val CHAR(1));
DECLARE #int INT = 48,
#letters INT = 65;
IF OBJECT_ID('dbo.tbl_keys') IS NOT NULL
DROP TABLE dbo.tbl_keys;
--This will hold the values so you can always reference them
CREATE TABLE dbo.tbl_Keys
(
--Primary key will create a clustered index on rank_id by default
rank_id INT PRIMARY KEY,
ID_Code CHAR(2)
);
--Another index on ID_Code
CREATE NONCLUSTERED INDEX idx_ID_Code ON tbl_keys(ID_Code);
--This is how I get all your individual values
WHILE (SELECT COUNT(*) FROM #values) < 36
BEGIN
IF(#int <= 57)
INSERT INTO #values VALUES(CHAR(#int));
INSERT INTO #values
VALUES (CHAR(#letters))
SET #int = #int + 1;
SET #letters = #letters + 1;
END
--Insert all possible combinations and rank them
INSERT INTO tbl_Keys
--ASCII is your best friend. It returns the ASCII code(numeric value) for characters
SELECT ROW_NUMBER() OVER (ORDER BY ASCII(A.val),ASCII(B.val)) AS rank_id,
A.val + B.val ID
FROM #values A
CROSS JOIN #values B;
I provide two different ways of getting the next ID_code(Read comments):
--Here's some dummy data
WITH CTE_DummyTable
AS
(
SELECT '00' ID_Code
UNION ALL
SELECT '01'
UNION ALL
SELECT '02'
)
----Here's how to get the next value with the assumption there are no gaps in your data
--SELECT MIN(ID_Code) next_id_code
--FROM tbl_Keys
--WHERE ID_code > (SELECT MAX(id_code) FROM CTE_DummyTable)
--This one doesn't assume the gaps and returns the lowest available ID_code
SELECT MIN(ID_Code) next_id_code
FROM tbl_Keys
WHERE ID_code NOT IN (SELECT DISTINCT id_code FROM CTE_DummyTable)
Note: If you were ever to want to convert your alphanumeric values really easily for whatever reason without changing the rank try this.
SELECT rank_id,
ID_code,
CAST(CONCAT(ASCII(LEFT(id_code,1)),ASCII(RIGHT(id_code,1))) AS INT) AS numeric_id_code
FROM tbl_Keys

create a list of the alphabet via SQL

I would like to produce results from the alphabet via SQL?
Something like this:
A
B
C
D
E
F
I have tried:
SELECT
'A','B','C'
But this just produces the letters across in columns.
Use table spt_values and convert values to chars
SELECT Char(number+65)
FROM master.dbo.spt_values
WHERE name IS NULL AND
number < 26
EDIT: This table is undocumented. But, it's used by many system storedprocedures and it's extremely unlikely for this table to disappear, since all those procs should be rewritten. This would be like poking a sleeping lion.
--
-- tally = 9466 rows in my db, select upper & lower alphas
--
;
with
cte_tally as
(
select row_number() over (order by (select 1)) as n
from sys.all_columns
)
select
char(n) as alpha
from
cte_tally
where
(n > 64 and n < 91) or
(n > 96 and n < 123);
go
The sys.all_columns is a documented table. It will be around for a while.
http://technet.microsoft.com/en-us/library/ms177522.aspx
It seems clear that the table, sp_values, is undocumented and can be removed in the future without any comment from Microsoft.
Try:
select 'A' union
select 'B' union
select 'C'
If you want to print from A to Z, then:
DECLARE #i int=65
WHILE #i < 91
BEGIN
PRINT CHAR(#i);
SET #i=#i+1;
END
You could use U-SQL
Select
[letter]
From
(
Values
('A')
,('B')
,('C')
) As [Letters]([letter])
with AlphabetList as
(
select char(65) letter
union all
select char(ascii(letter) + 1)
from AlphabetList
where letter <> 'Z'
)
select *
from AlphabetList
Using a recursive CTE (common table expression) to output the alphabet, A-Z, one row per letter/character:
;WITH cteAZ AS
(
SELECT ASCII('A') [AlphaCode],CAST('A' AS CHAR(1)) [Alpha]
UNION ALL
SELECT a.AlphaCode + 1 [AlphaCode],CAST(CHAR(a.AlphaCode + 1) AS CHAR(1)) [Alpha]
FROM cteAZ a WHERE a.AlphaCode < ASCII('Z')
)
SELECT
az.AlphaCode,az.Alpha
FROM
cteAZ az
Try this
;WITH CHARA2Z
AS (
SELECT
[ASCII] = ASCII('A'),
[LETTER] = CHAR(ASCII('A'))
UNION ALL
SELECT
[ASCII] + 1,
[LETTER] = CHAR([ASCII]+1)
FROM
CHARA2Z
WHERE
[ASCII] < ASCII('Z')
)
SELECT * FROM CHARA2Z
Replace 'A' & 'Z' by 'a' & 'z' for small letters.
Stemming from #MarkoJuvančič's answer, but a solution that will work on every SQL DBMS:
CREATE TEMPORARY TABLE alphabet (ltr CHAR(1));
SET #row_number = 0;
INSERT INTO alphabet
SELECT
CHAR((#row_number:=#row_number + 1) +64) -- 'A' is the 65th character on the ASCII table
FROM customer -- any table with 26 or more rows could suffice for this job
WHERE #row_number < 26;
SELECT 'A' letter
UNION ALL
SELECT 'B' letter
UNION ALL
SELECT 'C' letter
UNION ALL
SELECT 'D' letter
UNION ALL
SELECT 'E' letter
UNION ALL
SELECT 'F' letter
UNION ALL
SELECT 'G' letter
UNION ALL
SELECT 'H' letter
UNION ALL
SELECT 'I' letter
UNION ALL
SELECT 'J' letter
UNION ALL
SELECT 'K' letter
UNION ALL
SELECT 'L' letter
UNION ALL
SELECT 'M' letter
UNION ALL
SELECT 'N' letter
UNION ALL
SELECT 'O' letter
UNION ALL
SELECT 'P' letter
UNION ALL
SELECT 'Q' letter
UNION ALL
SELECT 'R' letter
UNION ALL
SELECT 'S' letter
UNION ALL
SELECT 'T' letter
UNION ALL
SELECT 'U' letter
UNION ALL
SELECT 'V' letter
UNION ALL
SELECT 'W' letter
UNION ALL
SELECT 'X' letter
UNION ALL
SELECT 'Y' letter
UNION ALL
SELECT 'Z' letter;

SQL splitting a word in separate characters

I need to change an application and the first thing I need is to change a field in a database table.
In this table I now have 1 to 6 single characters, i.e. 'abcdef'
I need to change this to '[a][b][c][d][e][f]'
[edit] It is meant to stay in the same field. So before field = 'abcdef' and after field = '[a][b][c][d][e][f]'.
What would be a good way to do this?
rg.
Eric
You can split string to separate characters using following function:
create function ftStringCharacters
(
#str varchar(100)
)
returns table as
return
with v1(N) as (
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
),
v2(N) as (select 1 from v1 a, v1 b),
v3(N) as (select top (isnull(datalength(#str), 0)) row_number() over (order by ##spid) from v2)
select N, substring(#str, N, 1) as C
from v3
GO
And then apply it as:
update t
set t.FieldName = p.FieldModified
from TableName t
cross apply (
select (select quotename(s.C)
from ftStringCharacters(t.FieldName) s
order by s.N
for xml path(''), type).value('text()[1]', 'varchar(20)')
) p(FieldModified)
SQLFiddle sample
DECLARE #text NVARCHAR(50)
SET #text = 'abcdef'
DECLARE #texttable TABLE (value NVARCHAR(1))
WHILE (len(#text) > 0)
BEGIN
INSERT INTO #texttable
SELECT substring(#text, 1, 1)
SET #text = stuff(#text, 1, 1, '')
END
select * from #texttable
Without using a function:
declare #t table(C varchar(18))
insert #t values('abc'), ('1234'), (' 1234a')
;with CTE as
(
select C, '[' + substring(c, a.n, 1) + ']' v, rn from
(select 1 n union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6) a
cross apply
(select c, row_number() over (order by C) rn from #t group by c) b
where a.n <= len(C)
)
update t3
set C = t4.[value]
FROM #t t3
JOIN
(
select C,
(
select v
from CTE t1
where t1.rn = t2.rn
for xml path(''), type
).value('.', 'varchar(18)') [value]
from CTE t2
group by t2.rn, C
) t4
ON t3.C = t4.C
SELECT * FROM #t

SQL multiple replace

I want to read the company table and take out all possible suffixes from the name. Here's what I have so far:
declare #badStrings table (item varchar(50))
INSERT INTO #badStrings(item)
SELECT 'company' UNION ALL
SELECT 'co.' UNION ALL
SELECT 'incorporated' UNION ALL
SELECT 'inc.' UNION ALL
SELECT 'llc' UNION ALL
SELECT 'llp' UNION ALL
SELECT 'ltd'
select id, (companyname = Replace(name, item, '') FROM #badStrings)
from companies
where name != ''
Ed Northridge's answer will work, and I have upvoted it, but just in case multiple replacements are required I am adding another option using his sample data. If, for example one of the companies was called "The PC Company LTD" This would duplicate rows in the output with one being "The PC LTD" and the other "The PC Company". To resolve this there are 2 option depending on your desired outcome. The first is to only replace the "Bad Strings" when they occur at the end of the name.
SELECT c.ID, RTRIM(x.Name) [Name]
FROM #companies c
OUTER APPLY
( SELECT REPLACE(c.name, item, '') AS [Name]
FROM #badStrings
-- WHERE CLAUSE ADDED HERE
WHERE CHARINDEX(item, c.Name) = 1 + LEN(c.Name) - LEN(Item)
) x
WHERE c.name != ''
AND x.[Name] != c.Name
This would yield "The PC Company" with no duplicates.
The other option is replace All occurances of the Bad Strings recursively:
;WITH CTE AS
( SELECT c.ID, c.Name [OriginalName], RTRIM(x.Name) [Name], 1 [Level]
FROM #companies c
OUTER APPLY
( SELECT REPLACE(c.name, item, '') AS [Name]
FROM #badStrings
WHERE CHARINDEX(item, c.Name) = 1 + LEN(c.Name) - LEN(Item)
) x
WHERE c.name != ''
AND RTRIM(x.Name) != c.Name
UNION ALL
SELECT c.ID, OriginalName, RTRIM(x.Name) [Name], Level + 1 [Level]
FROM CTE c
OUTER APPLY
( SELECT REPLACE(c.name, item, '') AS [Name]
FROM #badStrings
WHERE CHARINDEX(item, c.Name) = 1 + LEN(c.Name) - LEN(Item)
) x
WHERE c.name != ''
AND x.[Name] != c.Name
)
SELECT DISTINCT ID, Name, OriginalName
FROM ( SELECT *, MAX(Level) OVER(PARTITION BY ID) [MaxLevel]
FROM CTE
) c
WHERE Level = maxLevel
This would yield "The PC" from "The PC Company".
The error I got running the snippet was:
Msg 102, Level 15, State 1, Line 12
Incorrect syntax near '='.
The below code isn't an ideal solution - it will only return a list of companies where their name has been changed by the REPLACE function.
declare #companies table (id int, name nvarchar(50))
INSERT INTO #companies(id, name)
SELECT 1,'One Company' UNION ALL
SELECT 2, 'Two co.' UNION ALL
SELECT 3, 'Three incorporated' UNION ALL
SELECT 4, 'Four inc.' UNION ALL
SELECT 5, 'Five llc' UNION ALL
SELECT 6, 'Six llp' UNION ALL
SELECT 7, 'Seven ltd'
select * from #companies
declare #badStrings table (item varchar(50))
INSERT INTO #badStrings(item)
SELECT 'company' UNION ALL
SELECT 'co.' UNION ALL
SELECT 'incorporated' UNION ALL
SELECT 'inc.' UNION ALL
SELECT 'llc' UNION ALL
SELECT 'llp' UNION ALL
SELECT 'ltd'
select * from #badStrings
Here is the edited query:
select id, x.Name
from #companies c
OUTER APPLY (
SELECT Replace(c.name, item, '') AS [Name]
FROM #badStrings
) x
where c.name != ''
AND x.[Name] != c.Name
This returns:
id Name
----------- --------
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
7 Seven
(7 row(s) affected)
Hopefully it's useful
Edit:
An alternative to apply the match to those company names which end with the #badStrings value
select id, x.Name
from #companies c
OUTER APPLY (
SELECT Replace(c.name, item, '') AS [Name]
FROM #badStrings
WHERE c.Name LIKE '%'+item
) x
where c.name != ''

How to format TSQL SELECT output in SQL Sever

How to loop through a select statement results to have a formatted text?
for example the select is like:
select name from table
and we want a variable #names like this:
"name1,name2,name3"
Database is SQL Server 2005
If table contains several records, i.e.:
1, name1, ..
2, name2, ..
3, name3, ..
then this query:
DECLARE #names VARCHAR(MAX)
SELECT #names = COALESCE(#names + ', ', '') + name
FROM table
will produce next result:
name1, name2, name3
See COALESCE on MSDN
This would need to be done within a function. There's no quick way to do this. Normally you would do this within your client side code.
If you plan on making a function that you do on each row form another query it will be really slow, because the function needs to be called for each row. You should work with sets of data at one time, it does all rows at one time. This is an example of how to concatenate multiple values for each row of another query:
set nocount on;
declare #t table (id int, name varchar(20), x char(1))
insert into #t (id, name, x)
select 1,'test1', 'a' union
select 1,'test1', 'b' union
select 1,'test1', 'c' union
select 2,'test2', 'a' union
select 2,'test2', 'c' union
select 3,'test3', 'b' union
select 3,'test3', 'c'
SELECT p1.id, p1.name,
stuff(
(SELECT
', ' + x
FROM #t p2
WHERE p2.id=p1.id
ORDER BY name, x
FOR XML PATH('')
)
,1,2, ''
) AS p3
FROM #t p1
GROUP BY
id, name
OUTPUT:
id name p3
----------- -------------------- -----------
1 test1 a, b, c
2 test2 a, c
3 test3 b, c