tsql

select data up to a space?

吃可爱长大的小学妹 提交于 2021-02-04 09:56:13
问题 I have an MSSQL database field that looks like the examples below: u129 james u300 chris u300a jim u202 jane u5 brian u5z brian2 Is there a way to select the first set of characters? Basically select all the characters up until the first line space? I tried messing around with LEFT, RIGHT, LEN, but couldn't figure out a way to do it with variable string lengths like in my example. Thanks! 回答1: You can use a combiation of LEFT and CHARINDEX to find the index of the first space, and then grab

SQL Server T-SQL - import multiple JSON files in a loop

橙三吉。 提交于 2021-02-04 08:12:25
问题 I am trying to use the following code snippet to iterate through a list of json files. I have excluded the iteration logic but essentially I will have to pass in a variable to openrowset function. DECLARE @i INT = 1 DECLARE @json AS VARCHAR(MAX), @file AS NVARCHAR(MAX), @command AS VARCHAR(MAX) WHILE(@i < 10) BEGIN SET @file = 'C:\file\path'; PRINT @file SELECT @json = BulkColumn FROM OPENROWSET (BULK ''' + @file +''', SINGLE_CLOB) AS j SELECT * FROM OPENJSON(@json) AS json -- Optionally,

SQL Server T-SQL - import multiple JSON files in a loop

為{幸葍}努か 提交于 2021-02-04 08:12:05
问题 I am trying to use the following code snippet to iterate through a list of json files. I have excluded the iteration logic but essentially I will have to pass in a variable to openrowset function. DECLARE @i INT = 1 DECLARE @json AS VARCHAR(MAX), @file AS NVARCHAR(MAX), @command AS VARCHAR(MAX) WHILE(@i < 10) BEGIN SET @file = 'C:\file\path'; PRINT @file SELECT @json = BulkColumn FROM OPENROWSET (BULK ''' + @file +''', SINGLE_CLOB) AS j SELECT * FROM OPENJSON(@json) AS json -- Optionally,

Problems with INNER JOIN and LEFT/RIGHT OUTER JOIN

我只是一个虾纸丫 提交于 2021-02-04 04:41:23
问题 I have three tables: Orders OrderId, int PK CustomerId, int FK to Customer, NULL allowed Customers CustomerId, int PK CompanyId, int FK to Company, NULL not allowed Companies CompanyId, int PK Name, nvarchar(50) I want to select all orders, no matter if they have a customer or not, and if they have a customer then also the customer's company name. If I use this query... SELECT Orders.OrderId, Customers.CustomerId, Companies.Name FROM Orders LEFT OUTER JOIN Customers ON Orders.CustomerId =

Problems with INNER JOIN and LEFT/RIGHT OUTER JOIN

时光总嘲笑我的痴心妄想 提交于 2021-02-04 04:40:23
问题 I have three tables: Orders OrderId, int PK CustomerId, int FK to Customer, NULL allowed Customers CustomerId, int PK CompanyId, int FK to Company, NULL not allowed Companies CompanyId, int PK Name, nvarchar(50) I want to select all orders, no matter if they have a customer or not, and if they have a customer then also the customer's company name. If I use this query... SELECT Orders.OrderId, Customers.CustomerId, Companies.Name FROM Orders LEFT OUTER JOIN Customers ON Orders.CustomerId =

Use set in a case statement in SQL Server

五迷三道 提交于 2021-02-02 09:56:25
问题 I would like to set a variable in SQL Server using a CASE statement. For example: DECLARE @UNITY VARCHAR(5) DECLARE @AUX VARCHAR(5) CASE WHEN @UNITY = 'U1' THEN @AUX = 'M1' WHEN @UNITY = 'U2' THEN @AUX = 'M2' WHEN @UNITY = 'U3' THEN @AUX = 'M3' END 回答1: You can't use case as a flow control. An SQL case is an expression that return a scalar value based on condition(s). It's well documented in the remarks section: The CASE expression cannot be used to control the flow of execution of Transact

Create a table using a dynamic complex query

瘦欲@ 提交于 2021-01-29 22:01:28
问题 Is there a way to create a table from a complex query result set? A parameter will contain a query and I need to take this query and put the result into a temporary table. I have it working for a simple query Declare @Src nvarchar(4000)='Select Distinct a,b,c from mytable' Declare @newId VARCHAR(50) SELECT @newId = REPLACE(CONVERT(VARCHAR(50),NEWID()),'-','') -- Make sure the table doesn't exist... if does we need to delete it. IF OBJECT_ID('TMP_' + @newId) IS NOT NULL BEGIN SET @SQLStr =

Use column of type bit to differentiate group by?

时光怂恿深爱的人放手 提交于 2021-01-29 20:42:53
问题 I have data that looks like the following: Cell Date Hr Min Value Kpi Exists CELL1 20141112 8 45 1 KPI1 NULL CELL1 20141112 8 45 2 KPI1 NULL CELL1 20141112 8 45 7 KPI1 NULL CELL1 20141112 8 45 7 KPI1 NULL CELL2 20141112 8 0 5 KPI2 1 CELL2 20141112 8 15 2 KPI2 1 CELL2 20141112 8 30 9 KPI2 1 CELL2 20141112 8 45 7 KPI2 1 CELL3 20141112 8 0 1 KPI3 1 CELL3 20141112 8 15 3 KPI3 1 CELL3 20141112 8 30 4 KPI3 NULL CELL3 20141112 8 45 6 KPI3 1 I would like to group by Cell, Date, HR, KPI in order to

Creating a calculated column based on the flag - TSQL

倖福魔咒の 提交于 2021-01-29 17:43:07
问题 I want to calculate the sum of the orders based on the flag. Sample table: +--------------+---------------+---------------+ | Order | Flag | Amount | +--------------+---------------+---------------+ | Order1 | Yes | 500 | | Order1 | Yes | 325 | | Order2 | Yes | 799 | | Order2 | No | 550 | | Order2 | Yes | 675 | | Order3 | No | 800 | +--------------+---------------+---------------+ I want to create 2 new columns. One column is the total amount of order and another one is the total amount of

Divide by Subquery returning 'Subquery returned more than 1 value.' error

有些话、适合烂在心里 提交于 2021-01-29 17:20:18
问题 I have the following table in which I have two sales orders and each sales order has a different number of transactions. SaleOrder Transaction Amount S1 T1 20 S1 T2 20 S2 T1 15 S2 T2 15 S2 T3 15 The problem is I am getting the total sales order amount against each transaction in SQL Server table when in fact they need to be divided equally among the number of transactions like this: SaleOrder Transaction Amount S1 T1 10 S1 T2 10 S2 T1 5 S2 T2 5 S2 T3 5 I've written the following query: SELECT