tsql

TSQL - Convert Rows per record to Columns

浪子不回头ぞ 提交于 2021-01-28 07:36:42
问题 Consider the following table: +------------------------------------------------------------------------------+ | GUID | DeviceGUID | DetailGUID | sValue | iValue | gValue | DateStored | | ENTRY1 | DEVICE1 | Detail1 | SN112 | | | 01/01/2020 | | ENTRY2 | DEVICE1 | Detail4 | | 1241 | | 01/01/2020 | | ENTRY3 | DEVICE1 | Detail7 | | | GUID12 | 01/01/2020 | | ENTRY4 | DEVICE2 | Detail1 | SN111 | | | 01/01/2020 | | ENTRY5 | DEVICE2 | Detail2 | RND123 | | | 01/01/2020 | | ENRTY6 | DEVICE2 | Detail4 |

TSQL - Convert Rows per record to Columns

≯℡__Kan透↙ 提交于 2021-01-28 07:21:56
问题 Consider the following table: +------------------------------------------------------------------------------+ | GUID | DeviceGUID | DetailGUID | sValue | iValue | gValue | DateStored | | ENTRY1 | DEVICE1 | Detail1 | SN112 | | | 01/01/2020 | | ENTRY2 | DEVICE1 | Detail4 | | 1241 | | 01/01/2020 | | ENTRY3 | DEVICE1 | Detail7 | | | GUID12 | 01/01/2020 | | ENTRY4 | DEVICE2 | Detail1 | SN111 | | | 01/01/2020 | | ENTRY5 | DEVICE2 | Detail2 | RND123 | | | 01/01/2020 | | ENRTY6 | DEVICE2 | Detail4 |

Distribute arbitrary rows on multiple columns

狂风中的少年 提交于 2021-01-28 06:11:05
问题 I have a table of materials. I need to fill a data entry form from that table. The problem is that the data entry form is divided into multiple columns each one containing a a number of materials as in the picture. How to write a tsql select query to obtain the first bunch of material names into a column, the second bunch into a second column and so on. 回答1: It might be easiest to do this in the client side, and not in the database, but if you really want to do this, I would assume the

Variable length substring between two characters

天大地大妈咪最大 提交于 2021-01-28 04:41:19
问题 Data looks like this: Initiative: Credible Sources; Initiative: Just in Time; Initiative: Database Normalization; I want it to look like this: Credible Sources Just in Time Database Normalization It's pretty simple to get rid of one or the other. This: SELECT DISTINCT LEFT(OPTIONAL_FIELD_2, CHARINDEX(';', OPTIONAL_FIELD_2 + ';')-1) AS OPTIONAL_FIELD_2 FROM my_table ORDER BY OPTIONAL_FIELD_2 Gives me this: Initiative: Credible Sources Initiative: Just in Time Initiative: Database Normalization

Parser for query filter expression tree

半腔热情 提交于 2021-01-28 03:20:33
问题 I am looking for a parser that can operate on a query filter. However, I'm not quite sure of the terminology so it's proving hard work. I hope that someone can help me. I've read about 'Recursive descent parsers' but I wonder if these are for full-blown language parsers rather than the logical expression evaluation that I'm looking for. Ideally, I am looking for .NET code (C#) but also a similar parser that works in T-SQL. What I want is for something to parse e.g.: ((a=b)|(e=1))&(c<=d)

Get Distinct characters from a given string in sql server

非 Y 不嫁゛ 提交于 2021-01-28 03:05:50
问题 i need a T-SQL function that remove duplicate charcters in a give String for exemple Fn_Remove('AVGHAHA') it will returns AVGH 回答1: Using NGrams8K, you can split the string into individual character "tokens", apply a number to that character set, and then rebuild with only the first of each character: WITH CTE AS( SELECT V.S, N.position, N.token, ROW_NUMBER() OVER (PARTITION BY N.token ORDER BY N.position) AS RN FROM (VALUES('AVGHAHA'))V(S) CROSS APPLY dbo.NGrams8k(V.S,1) N) SELECT V.S,

SQL Variable Containing Path in XML Node

风流意气都作罢 提交于 2021-01-28 02:45:49
问题 I need help with passing an XML path through a variable to the nodes() method. I have looked at several different posts and found that a node can be passed by using local-name and sql:variable . The example below works as expected: DECLARE @XML_Path VARCHAR(MAX) , @XML_In XML SET @XML_Path = 'GetData' SET @XML_In = ' <GetData> <test>234</test> </GetData> ' --THIS WORKS SELECT Item_Idx = Nodes.value('(test)[1]' ,'INT') FROM @XML_In.nodes('/*[local-name()=sql:variable("@XML_Path")]') Results

Get Distinct characters from a given string in sql server

戏子无情 提交于 2021-01-28 00:08:52
问题 i need a T-SQL function that remove duplicate charcters in a give String for exemple Fn_Remove('AVGHAHA') it will returns AVGH 回答1: Using NGrams8K, you can split the string into individual character "tokens", apply a number to that character set, and then rebuild with only the first of each character: WITH CTE AS( SELECT V.S, N.position, N.token, ROW_NUMBER() OVER (PARTITION BY N.token ORDER BY N.position) AS RN FROM (VALUES('AVGHAHA'))V(S) CROSS APPLY dbo.NGrams8k(V.S,1) N) SELECT V.S,

SQL Server 2008 R2: Concatenate alias name with column value

强颜欢笑 提交于 2021-01-28 00:01:20
问题 I have Product table with columns QtyID , Qty and Year_ID . I also have a table tbl_Years with ID and Year . I have a simple SELECT statement with SUM calculation of Qty : SELECT SUM(Qty) AS /*Sum_of_year_2016*/ FROM Product p INNER JOIN tbl_Years ty ON p.Year_ID = ty.Year_ID WHERE ty.Year_ID = 6; I want to define an alias name of Sum_of_year_2016 for the SUM(Qty) value. Note: the year should be fetched from the tbl_Years table. My attempt: SELECT SUM(Qty) AS 'Sum_of_year_' + ty.Year FROM

SQL Variable Containing Path in XML Node

杀马特。学长 韩版系。学妹 提交于 2021-01-27 21:50:23
问题 I need help with passing an XML path through a variable to the nodes() method. I have looked at several different posts and found that a node can be passed by using local-name and sql:variable . The example below works as expected: DECLARE @XML_Path VARCHAR(MAX) , @XML_In XML SET @XML_Path = 'GetData' SET @XML_In = ' <GetData> <test>234</test> </GetData> ' --THIS WORKS SELECT Item_Idx = Nodes.value('(test)[1]' ,'INT') FROM @XML_In.nodes('/*[local-name()=sql:variable("@XML_Path")]') Results