how to calculate a string statement - tsql

Given a string combination which is a calculation statement, how can I get the result, in this case is column cal in below code.
I know I can use case but is there any direct way to do the calculation?
create table tl_test
(
cl1 int
)
create table tl_test2
(
cl1 char(1)
)
insert into tl_test values (21), (43), (13), (36), (41)
insert into tl_test2 values ( '+'), ('-'), ('*'), ('/')
select *,
cast(c1 as varchar) + f1
+ cast(c2 as varchar) + f2
+ cast(c3 as varchar) + f3
+ cast(c4 as varchar) + f4
+ cast(c5 as varchar) as cal
from(
SELECT A.cl1 as c1, f1.cl1 as f1, b.cl1 as c2,f2.cl1 as f2, C.cl1 as c3, f3.cl1 as f3, D.cl1 as c4, f4.cl1 as f4, E.cl1 as c5
FROM TL_TEST A
CROSS JOIN TL_TEST2 f1
CROSS JOIN TL_TEST B
CROSS JOIN TL_TEST2 f2
CROSS JOIN TL_TEST C
CROSS JOIN TL_TEST2 f3
CROSS JOIN TL_TEST D
CROSS JOIN TL_TEST2 f4
CROSS JOIN TL_TEST E
)a
WHERE c1 != c2
and c1 != c3
and c1 != c4
and c1 != c5
and c2 != c3
and c2 != c4
and c2 != c5
and c3 != c4
and c3 != c5
and c4 != c5

You could store the result of the cal column in a string and use EXEC to calculate your answer.
DECLARE #sql NVARCHAR(MAX) = 'SELECT ' + (SELECT TOP 1 cal FROM result)
EXEC(#sql)

Read this related thread, especially the answer by Erland Sommarskog.
I'm sorry, there is no way to do this in pure ad-hoc T-SQL.
Possible workarounds:
1) You can use dynamic SQL, which has one flaw: Your example would be calculated for INTs
DECLARE #cmd VARCHAR(100)='SELECT (' + '21*41-36 / 13+43' /*Your formula coming from somewhere*/ + ')';
EXEC(#cmd);
Results 902
2) You can use XMLs implicit ability to calculate like this
SELECT CAST('' AS XML).value('21*41-36 div 13+43','float')
hint: "/" must be replaced with " div "
Results 901.23077
It's a pitty, that the XML type's .value() knows expressions and values. A value can be introduced from a table's column dynamically (sql:column("ColumnName")), but an expression must be a literal.
3) And you might include .Net-code as assembly (CLR function).
Conclusio
Sorry, there is no easy going...
If you need exact results you should use the XML approach in dynamically created SQL.
UPDATE Working example
DECLARE #tbl TABLE(ID INT IDENTITY,SomeFormula VARCHAR(100));
INSERT INTO #tbl(SomeFormula) VALUES
('1+2')
,('21*41-36/13+43')
,('(1+3)*4')
,('3.6 div (2.5-1)');
DECLARE #cmd VARCHAR(MAX)=
(
SELECT 'DECLARE #x XML=CAST('''' AS XML);' +
STUFF
(
(
SELECT ' UNION ALL SELECT ' + CAST(ID AS VARCHAR(MAX)) + ' AS ID, #x.value(''' + REPLACE(SomeFormula,'/',' div ') + ''',''float'')'
FROM #tbl
FOR XML PATH('')
),1,11,''
)
);
PRINT #cmd;
EXEC (#cmd);
This is the generated statement
DECLARE #x XML=CAST('' AS XML);
SELECT 1 AS ID, #x.value('1+2','float')
UNION ALL SELECT 2 AS ID, #x.value('21*41-36 div 13+43','float')
UNION ALL SELECT 3 AS ID, #x.value('(1+3)*4','float')
UNION ALL SELECT 4 AS ID, #x.value('3.6 div (2.5-1)','float')
And this is the result
ID
1 3
2 901,23077
3 16
4 2,4

Related

Ho do i convert this code into PostgreSQL Store Procedure?

I am new to PostgreSQL. I want to converter or add this code to Store Procedure.
WITH RECURSIVE t(i) AS (
SELECT * FROM unnest((select regexp_split_to_array('signature',''))::char[])
), cte AS (
SELECT i AS combo, i, 1 AS ct
FROM t
UNION ALL
SELECT cte.combo || t.i, t.i, ct + 1
FROM cte, t
WHERE ct <= 8
AND position(t.i in cte.combo) = 0
)
SELECT distinct cc.combo,ww.word
FROM cte cc
inner join words ww ON ww.word=cc.combo
WHERE length(combo)>1
AND ww.source_id in(1,2,19,21,24,26,33,34)
ORDER BY cc.combo ASC;
Just put it into a function:
create function generate_anagrams(p_word text)
returns table(combo text, word text)
as
$$
WITH RECURSIVE t(i) AS (
SELECT *
FROM unnest((select regexp_split_to_array(p_word,''))::char[])
), cte AS (
SELECT i AS combo, i, 1 AS ct
FROM t
UNION ALL
SELECT cte.combo || t.i, t.i, ct + 1
FROM cte, t
WHERE ct <= 8
AND position(t.i in cte.combo) = 0
)
SELECT distinct cc.combo,ww.word
FROM cte cc
inner join words ww ON ww.word=cc.combo
WHERE length(combo)>1
AND ww.source_id in(1,2,19,21,24,26,33,34)
ORDER BY cc.combo ASC
$$
language sql;
You can use it like this:
select *
from generate_anagrams('signature');

SQL Columns to List

I have the following table contents:-
And I need to produce the following output:-
P1 C1
P1 C3
P2 C1
P2 C4
P3 C2
P3 C3
P3 C4
How can I create that list from my table?
Thanks
P
You have to use UnPivot to get the desired result
DECLARE #MyTable TABLE
(Attribute VARCHAR(10) , C1 VARCHAR(10), C2 VARCHAR(10), C3 VARCHAR(10), C4 VARCHAR(10))
INSERT INTO #MyTable VALUES
('P1','X', NULL,'X',NULL), ('P2','X',NULL,NULL,'X'),('P3',NULL,'X','X','X')
SELECT * FROM #MyTable
SELECT uPivot.Attribute, uPivot.Quatr, uPivot.IsMarked
FROM #MyTable Tab1
UNPIVOT
(
IsMarked
for Quatr in (C1, C2, C3, C4)
) uPivot;
SQL FIDDLER EXAMPLE
This should work based off the information provided.
select u.P, -- whatever the column containing p1, p2, etc is called
u.C,
u.Contents -- what is stored in each cell
from xtable
unpivot
(
Contents
for C in (c1, c2, c3)
) u
where u.Contents like 'x'
http://sqlfiddle.com/#!3/4d908/1/0
I pretty much slightly modified the example given in what Ako linked in the comments.
This was the solution I implemented in the end
DECLARE #SQL NVARCHAR(MAX)
DECLARE #ColName TABLE (Name VARCHAR(500))
DECLARE #ColCSV VARCHAR(MAX)
INSERT INTO #ColName
SELECT
Column_Name
FROM
information_schema.columns
WHERE
Table_Name = 'SMC_PatientCondition'
AND Column_Name NOT IN
('ReportingCode',
'P_PatientId',
'P_SysId',
'Condition')
SET #ColCSV = (SELECT SUBSTRING((SELECT ',' + Name FROM #ColName FOR XML PATH ('')),2,2000) AS CSV)
SET #SQL =
('
SELECT
P_SysID,
Conditions
FROM
(
SELECT
P_SysId,
RIGHT(Conditions, LEN(Conditions)-2) Conditions,
HasCondition
FROM
SMC_PatientCondition
UNPIVOT
(
HasCondition FOR Conditions IN ('+ #ColCSV +')
)Con
) UserConditions
WHERE
HasCondition = ''Yes''
')
EXEC sp_executesql #SQL

Dynamic field definitions - can this be done in T-SQL?

I have a requirement that I'm struggling to implement. If possible, I'd like to achieve this with native T-SQL.
I have the following tables:
CUSTOMER
========
ID,
Name
FIELDDEF
========
ID,
Name
FieldType (Char T, N, D for Text, Number or Date)
CUSTOMERFIELD
=============
ID,
CustomerID,
FieldDefID,
CaptureDate,
ValueText,
ValueNumber,
ValueDate
Basically, the purpose of these tables is to provide an extensible custom field system. The idea is that the user creates new field definitions that can be a text, number or date field. Then, they create values for these fields in the ValueText, ValueNumber OR ValueDate field.
Example:
*Customer*
1,BOB
2,JIM
*FieldDef*
1,Mobile,T
1,DateOfBirth,D
*CustomerField*
ID,CustomerID,FieldDefID,CaptureDate,ValueText,ValueNumber,ValueDate
1,1,1,2011-01-1,07123456789,NULL,NULL
2,1,2,2011-01-1,NULL,NULL,09-DEC-1980
3,1,1,2011-01-2,07123498787,NULL,NULL
I need to create a view that looks like this:
*CustomerView*
ID,Name,Mobile,DateOfBirth
1,BOB,07123498787,09-DEC-1980
Note that Bob's mobile is the second one in the list, because it uses the most recent capture date.
Ideally, I need this to be extensible, so if I create a new field def in the future, it is automatically picked up in the CustomerView.
Is this possible in T-SQL at all?
Thanks,
Simon.
This would not be possible with a view, unless the view is dynamically recreated on the fly every time FieldDef changes because view schemas are locked-in at creation time. However, it may be possible with a stored procedure, which may or may not work depending on how you are using it.
Edit 1
Here is a sample query that works just for your current field names, and would have to be modified by dynamic SQL to work in general:
Edit 2
Modified to grab the newest values from the customer field table
with CustomerFieldNewest as (
select
cf1.*
from
customerfield cf1
inner join
(
select
customerid,
fielddefid,
max(capturedate) as maxcapturedate
from
customerfield cf2
group by
customerid,
fielddefid
) cf2 on cf1.customerid = cf2.customerid
and cf1.fielddefid = cf2.fielddefid
and cf1.capturedate = cf2.maxcapturedate
)
,CustomerFieldPivot as (
select
C.ID as ID
,max(case when F.Name = 'Mobile' then CF.ValueText end) as Mobile
,max(case when F.Name = 'DateOfBirth' then CF.ValueDate end) as DateOfBirth
from
Customer C
left join
CustomerFieldNewest CF on C.ID = CF.CustomerID
left join
FieldDef F on F.ID = CF.FieldDefID
group by
C.ID
)
select
C.*
,P.Mobile
,P.DateOfBirth
from
Customer C
left join
CustomerFieldPivot P on C.ID = P.ID
Edit 3
Here is T-SQL code to generate the view on the fly based on the current set of fields in FieldDef (this assumes the view CustomerView already exists, so you will need to create it first as a blank definition or you will get an error). I'm not sure about the performance of all this, but it should work correctly.
declare #sql varchar(max)
declare #fielddef varchar(max)
declare #fieldlist varchar(max)
select
#fielddef = coalesce(#fielddef + ', ' + CHAR(13) + CHAR(10), '') +
' max(case when F.Name = ''' + F.Name + ''' then CF.' +
case F.FieldType
when 'T' then 'ValueText'
when 'N' then 'ValueNumber'
when 'D' then 'ValueDate'
end
+ ' end) as [' + F.Name + ']'
,#fieldlist = coalesce(#fieldlist + ', ' + CHAR(13) + CHAR(10), '') +
' [' + F.Name + ']'
from
FieldDef F
set #sql = '
alter view [CustomerView] as
with CustomerFieldNewest as (
select
cf1.*
from
customerfield cf1
inner join
(
select
customerid,
fielddefid,
max(capturedate) as maxcapturedate
from
customerfield cf2
group by
customerid,
fielddefid
) cf2 on cf1.customerid = cf2.customerid
and cf1.fielddefid = cf2.fielddefid
and cf1.capturedate = cf2.maxcapturedate
)
,CustomerFieldPivot as (
select
C.ID as ID,
' + #fielddef + '
from
Customer C
left join
CustomerFieldNewest CF on C.ID = CF.CustomerID
left join
FieldDef F on F.ID = CF.FieldDefID
group by
C.ID
)
select
C.*,
' + #fieldlist + '
from
Customer C
left join
CustomerFieldPivot P on C.ID = P.ID
'
print #sql
exec(#sql)
select * from CustomerView
You need to build a crosstab which you do with the Pivot statement in TSQL. Here's an article that talks about how to build the pivot dynamically.
http://sqlserver-qa.net/blogs/t-sql/archive/2008/08/27/4809.aspx
Just for completeness there is sql_variant:
declare #t table (typ varchar(1), yuk sql_variant)
insert #t values ('d', getdate())
insert #t values ('i', 1234)
insert #t values ('s', 'bleep bloop')
select
yuk,
case typ
when 'd' then convert(datetime, yuk, 106)+50
when 'i' then cast(yuk as int) * 2
when 's' then reverse(cast(yuk as varchar))
else yuk
end
from #t

Set operation on TSQL (SQL 2005/2008)

When a set is given say {1,2,3,4,5,6}
The task is to separe pair of subsets
{1,2},
{1,3},
{1,4},
{1,5},
{1,6},
{2,3},
{2,4},
{2,5},
{2,6},
{3,4},
{3,5},
{3,6},
{4,5},
{5,6}
So when i have a table
Table Element
1
2
3
4
5
6
What is the way to list out all possible pair of comma separated subset ?
(Duplicates can be ignored (i.e) {1,2} is identical to {2,1})
SELECT T1.elem, T2.elem
FROM MyTable T1
INNER JOIN MyTable T2
ON T2.elem > T1.elem
...gets you most of the way there - if you want these shown as sets then...
SELECT '{' + CAST(T1.elem AS VARCHAR(12)) + ', ' + CAST(T2.elem AS VARCHAR(12)) + '}'
FROM MyTable T1
INNER JOIN MyTable T2
ON T2.elem > T1.elem
...is what you're after.
Here is a solution to the problem using a CTE. It isn’t particularly elegant, but it gets the job done.
DECLARE #set TABLE (Element INT);
INSERT INTO #set(Element) VALUES (1);
INSERT INTO #set(Element) VALUES (2);
INSERT INTO #set(Element) VALUES (3);
INSERT INTO #set(Element) VALUES (4);
INSERT INTO #set(Element) VALUES (5);
INSERT INTO #set(Element) VALUES (6);
;WITH array (Element1, Element2, Row)
AS
(
SELECT t.Element
, t2.Element
, ROW_NUMBER() OVER(ORDER BY t.Element)
FROM #set AS t
CROSS JOIN #set AS t2
WHERE t.Element <> t2.Element
)
SELECT a.Element1
, a.Element2
, '{' + CONVERT(VARCHAR(5),a.Element1) + ',' + CONVERT(VARCHAR(5),a.Element2) + '}' AS 'Subset'
FROM array AS a
WHERE NOT EXISTS (SELECT *
FROM array AS sa
WHERE sa.Element1 = a.Element2
AND sa.Element2 = a.Element1
AND sa.Row < a.Row
);

T-SQL: Pivot but for semicolon-separated values instead of columns

I've got semicolon-separated values in a column Values in my table:
Values
1;2;3;4;5
I would like to transform it in a procedure to have there values as rows:
Values
1
2
3
4
5
How could I do it in T-SQL?
Solution 1(using xml):
declare #str varchar(20)
declare #xml as xml
set #str= '1;2;3;4;5'
SET #xml = cast(('<x>'+replace(#str,';' ,'</x><x>')+'</x>') as xml)
SELECT col.value('.', 'varchar(10)') as value FROM #xml.nodes('x') as tbl(col)
Solution 2(using recursive cte)
declare #str as varchar(100)
declare #delimiter as char(1)
set #delimiter = ';'
set #str = '1;2;3;4;5' -- original data
set #str = #delimiter + #str + #delimiter
;with num_cte as
(
select 1 as rn
union all
select rn +1 as rn
from num_cte
where rn <= len(#str)
)
, get_delimiter_pos_cte as
(
select
ROW_NUMBER() OVER (ORDER BY rn) as rowid,
rn as delimiterpos
from num_cte
cross apply( select substring(#str,rn,1) AS chars) splittedchars
where chars = #delimiter
)
select substring(#str,a.delimiterpos+1 ,c2.delimiterpos - a.delimiterpos - 1) as Countries
from get_delimiter_pos_cte a
inner join get_delimiter_pos_cte c2 on c2.rowid = a.rowid+1
option(maxrecursion 0)
The thing that struck me as possibly leaving room for an additional answer, or additional improvement was that most of the answers/links given were how to split values like this for a single scalar value as opposed to how to apply that kind of splitting logic for a column of values in a table.
I include both a numbers table solution and an XML solution. The XML solution was inspired by the earlier post priyanka.sarkar. I think that a numbers table solution, using an actual numbers table instead of the CTE as in the below solution is probably the fastest, but the XML approach deserves to be developed upon because it's really nice looking.
So, here goes my attempt.
CREATE PROCEDURE PARSE_DELIMITED_VALUES
AS
WITH FIRST_NUMBERS (N) AS (
SELECT 1 UNION ALL SELECT 1
), SECOND_NUMBERS (N) AS (
SELECT E1.N
FROM FIRST_NUMBERS E1
CROSS JOIN FIRST_NUMBERS E2
), THIRD_NUMBERS (N) AS (
SELECT E1.N
FROM SECOND_NUMBERS E1
CROSS JOIN SECOND_NUMBERS E2
), FOURTH_NUMBERS (N) AS (
SELECT E1.N
FROM THIRD_NUMBERS E1
CROSS JOIN THIRD_NUMBERS E2
), FIFTH_NUMBERS (N) AS (
SELECT E1.N
FROM FOURTH_NUMBERS E1
CROSS JOIN FOURTH_NUMBERS E2
), NUMBERS (N) AS (
SELECT N
FROM NUMBERS
WHERE N <= 8000 /*adjust these as needed to come up with a max number equal to the max character length allowed in the Values column*/
/*or better yet, if you can, just remove this first...numbers... header stuff so long as you create a temp or permanent table that contains the same numbers to work with*/
)
SELECT SUBSTRING(
MYTABLE.Values,
CASE
WHEN NUMBERS.NUMBER = 1 THEN 1
ELSE NUMBERS.NUMBER + 1
END,
CASE CHARINDEX(';', MYTABLE.Values, NUMBERS.NUMBER + 1)
WHEN 0 THEN LEN('^' + MYTABLE.Values + '^') - 2 + 1
ELSE CHARINDEX(';', MYTABLE.Values, NUMBERS.NUMBER + 1)
END
- CASE
WHEN NUMBERS.NUMBER = 1 THEN 1
ELSE NUMBERS.NUMBER + 1
END
) AS PARSED_VALUE
FROM MYTABLE
INNER JOIN NUMBERS
ON NUMBERS.NUMBER <= LEN('^' + MYTABLE.Values + '^') - 2
AND (
NUMBERS.NUMBER = 1
OR SUBSTRING(MYTABLE.Values, NUMBERS.NUMBER, 1) = ';'
)
GO
-- if your values column can contain NULL values I would change the join at the end as follows:
--from INNER JOIN NUMBERS
--to LEFT OUTER JOIN NUMBERS
The above would probably be most performant if the WITH NUMBERS ... CTEs were replaced by a temporary or permanent table containing the same numeric values.
On the other hand the CTE does the job and keeps it more in one place.
CREATE PROCEDURE PARSE_DELIMITED_VALUES
AS
SELECT E.x.value('.', 'VARCHAR(MAX)') AS PARSED_VALUE
FROM (
SELECT CAST('<x>' + REPLACE(Values, ';', '</x><x>') + '</x>' AS XML) my_x
FROM MYTABLE
) TT
CROSS APPLY my_x.nodes('/x') AS E(x)
GO
-- if your values column can contain NULL values I would change the join at the end as follows:
from `CROSS APPLY`
to `OUTER APPLY`
It's not the most elegant approach, but this might be worth a try. It creates a Sql Command as a string, and at the end executes it.
DECLARE #Values VARCHAR(8000)
-- Flatten all values lists into one string
SET #Values = REPLACE(REPLACE((SELECT [Value] FROM [dbo.MyTable] FOR XML PATH('')), '<Value>', ''), '</Value>', ';')
SET #Values = SUBSTRING(#Values, 0, LEN(#Values))
DECLARE #SeparatorIndex INT
SET #SeparatorIndex = (SELECT TOP 1 PATINDEX('%[;]%', #Values))
DECLARE #InsertClause VARCHAR(50)
SET #InsertClause = 'INSERT INTO [dbo.MyTable] VALUES ('
DECLARE #SQL VARCHAR(500)
SET #SQL = #InsertClause + SUBSTRING(#Values, 0, #SeparatorIndex) + '); '
SET #Values = RIGHT(#Values, LEN(#Values) - (#SeparatorIndex - 1))
SET #SQL = REPLACE(#SQL + (SELECT (REPLACE(#Values, ';', '); ' + #InsertClause))) + ')', '; )', '')
EXEC (#SQL)
The command ends up (in Sql Server 2005) as:
INSERT INTO [dbo.MyTable] VALUES (1); INSERT INTO [dbo.MyTable] VALUES (2); INSERT INTO [dbo.MyTable] VALUES (3); INSERT INTO [dbo.MyTable] VALUES (4); INSERT INTO [dbo.MyTable] VALUES (5) ...'
Do you actually mean, "rows," as in, "tuples," (so you can insert the data into another table, one element per row) or do you mean you want the data displayed vertically?
I'd think a string Replace (look up T-SQL's String Functions) would do the trick, no? Depending on the output target, you'd replace ; with CRLF or . You could even use Replace to create dynamic SQL Insert statements that could be executed by the SP to do row inserts (if that was your intent).
For presentation purposes, this is bad practice.
If it is purely for presentation and you are permitted, I'd output everything as XML then XSLT it any way you want. Honestly, I don't remember the last time I operated directly on a recordset. I always output to XML.