Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last month.
Improve this question
enter image description here
Output
enter image description here
I was trying but it is not working for me
Can anybody assist
Try this
drop table if exists #have;
CREATE TABLE #have
(
ID [INT]
, Status [VarChar](8)
);
insert into #have
values
(1 , 'P')
, (2 , 'F')
, (3 , 'F')
, (4 , 'P')
, (5 , 'P')
, (10, 'F')
, (22, 'F')
;
with cte as
(
select *
, lag(Status) over (order by ID) as prev
from #have
)
select ID, Status
from cte
where Status = 'F' and prev = 'P'
;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I am just getting data inserting to temp table.
DECLARE #XmlStringNPDBudget NVARCHAR(max) = '{"BudgetList":[{"BudgetId":4,"Month":1,"Year":2022,"Budget":750000},{"BudgetId":5,"Month":2,"Year":2022,"Budget":950000},{"BudgetId":0,"Month":3,"Year":0,"Budget":0},{"BudgetId":0,"Month":4,"Year":0,"Budget":0},{"BudgetId":0,"Month":5,"Year":0,"Budget":0},{"BudgetId":0,"Month":6,"Year":0,"Budget":0},{"BudgetId":0,"Month":7,"Year":0,"Budget":0},{"BudgetId":0,"Month":8,"Year":0,"Budget":0},{"BudgetId":0,"Month":9,"Year":"2022","Budget":"11111"},{"BudgetId":1,"Month":10,"Year":2022,"Budget":1000},{"BudgetId":2,"Month":11,"Year":2022,"Budget":350000},{"BudgetId":3,"Month":12,"Year":2022,"Budget":550000}]}'
DROP TABLE IF EXISTS #ParticularYearBudgetInsertUpdate
CREATE TABLE #ParticularYearBudgetInsertUpdate
(
[BudgetId] INT,
[Month] INT,
[Year] INT,
[Budget] DECIMAL
)
INSERT INTO #ParticularYearBudgetInsertUpdate([BudgetId], [Month], [Year], [Budget])
SELECT
[BudgetId], [Month], [Year], [Budget]
FROM
OPENJSON (#XmlStringNPDBudget)
WITH (BudgetId INT, [Month] INT, [Year] INT, [Budget] DECIMAL)
SELECT * FROM #ParticularYearBudgetInsertUpdate
SELECT * FROM NPDBudget
It's displaying column name and one row of NULLs. please help me out
Your JSON data is contained in a BudgetList array - so you need to take that into account - try this code for the SELECT:
SELECT
[BudgetId], [Month], [Year], [Budget]
FROM
OPENJSON (#XmlStringNPDBudget, N'$.BudgetList')
WITH
(
BudgetId INT '$.BudgetId',
[Month] INT '$.Month', [Year] INT '$.Year',
[Budget] DECIMAL '$.Budget'
)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a append query where the original value of the Manager_ID field is a varchar(250) value = 31.0. I need to convert or cast the value to display only '31' and remove everything after the decimal. I have tried both Convert & Cast to both an integer or nvarchar, without success. I keep getting the following error:
Conversion failed when converting the varchar value '31.0' to data type int.
I have played with the different field types without success both in the insert table and the data table.
What am I missing?
Thanks
Error MSG for convert(int, convert(decimal(9,2),[Manager_ID]))
Error converting data type varchar to numeric.
INSERT INTO [dbo].[tblUsers]
( [User_ID]
,[FirstName]
,[LastName]
,[FullName]
,[EMail]
,[UserRoles]
,[PostionType]
,[ManagerID]
,[UUID]
,[External_UUID]
,[home_Location_id]
,[Home_Organization_ID]
,[Record_types]
,[Location_Ceiling_ID]
,[Organization_Ceiling_ID]
,[Payroll_Identifier]
,[Created_Date]
,[Created_Time]
,[Update_Date]
,[Update_Time])
Select id-- User_ID
,first_name
,last_name
,full_name
,email
,role_id --UserRoles
,position --PositionType
,**cast(Manager_id as nvarchar(10)) as ManagerID**
,uuid
,external_uuid
,home_location_id
,home_organization_id
,[type] --Record_Types
,location_ceiling_id
,organization_ceiling_id
,payroll_identifier
,Left(Convert(varchar(20), created_at, 120),10) as Create_Date
,Right(convert(varchar(16), created_at, 120),5) as Create_Time
,left(Convert(varchar(20), updated_at, 120),10) as Update_Date
,Right(convert(varchar(16), updated_at, 120),5) as Update_Time
from [stg].[Users]
Final Solution
,SUBSTRING(manager_id, 1,
CASE WHEN CHARINDEX('.',manager_id) - 1 < 0
THEN LEN(manager_id)
ELSE CHARINDEX('.',manager_id) - 1 END) as ManagerID
Convert function is very helpful for this. Below is what I would use:
INSERT INTO [dbo].[tblUsers]
( [User_ID]
,[FirstName]
,[LastName]
,[FullName]
,[EMail]
,[UserRoles]
,[PostionType]
,[ManagerID]
,[UUID]
,[External_UUID]
,[home_Location_id]
,[Home_Organization_ID]
,[Record_types]
,[Location_Ceiling_ID]
,[Organization_Ceiling_ID]
,[Payroll_Identifier]
,[Created_Date]
,[Created_Time]
,[Update_Date]
,[Update_Time])
Select id as User_ID
,first_name
,last_name
,full_name
,email
,role_id as UserRoles
,position as PositionType
,convert(int, convert(decimal(9,2),[Manager_ID])) as ManagerID
,uuid
,external_uuid
,home_location_id
,home_organization_id
,[type] as Record_Types
,location_ceiling_id
,organization_ceiling_id
,payroll_identifier
,Left(Convert(varchar(20), created_at, 120),10) as Create_Date
,Right(convert(varchar(16), created_at, 120),5) as Create_Time
,left(Convert(varchar(20), updated_at, 120),10) as Update_Date
,Right(convert(varchar(16), updated_at, 120),5) as Update_Time
from [stg].[Users]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Suppose I have this table with the index given below:
CREATE TABLE [dbo].[Jobs]
(
[Id] BIGINT NOT NULL PRIMARY KEY IDENTITY(1,1),
[Type] SMALLINT NOT NULL,
[Path] NVARCHAR(MAX),
[Name] VARCHAR(256),
)
GO
CREATE INDEX [IX_Jobs_Name_Type] ON [dbo].[Jobs] ([Name], [Type])
Which query will have better performance:
1.
UPDATE TOP((#JobCount + 3) / 4 )
Jobs WITH (ROWLOCK, READPAST)
SET [Name] = #NName
WHERE [Name] IS NULL AND (Type = 1 OR Type = 4)
UPDATE TOP((#JobCount + 3) / 8 )
Jobs WITH (ROWLOCK, READPAST)
SET [Name] = #NName
WHERE [Name] IS NULL AND Type = 1
UPDATE TOP((#JobCount + 3) / 8 )
Jobs WITH (ROWLOCK, READPAST)
SET [Name] = #NName
WHERE [Name] IS NULL AND Type = 4
Ignore in this case the correctness of the amount of rows updated,
Can doing a single search with 'or' for the Type be less effective than 2 separate queries because of the index?
You should of course try on your database, but the database should be able to apply the index in the OR case. It would be better written as:
WHERE [Name] IS NULL AND Type IN (1, 4)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
create table #Temp1 (id int identity(1,1), name varchar(50))
insert into #Temp1 values('Gokul')
insert into #Temp1 values('Dhivakar')
create table #Temp2 (id int, name varchar(50))
insert into #Temp2 values(1, 'Srikanth')
insert into #Temp2 values(3, 'Yogish')
select * from #Temp1
select * from #Temp2
MERGE #Temp1 AS target
USING #Temp2 AS source
ON (target.id = source.id)
WHEN MATCHED THEN
UPDATE SET Name = source.Name
WHEN NOT MATCHED THEN
INSERT (Name)
VALUES (source.Name) ;
-- OUTPUT deleted.*, $action, inserted.* INTO #MyTempTable;
Yes, Merge Statement supported in SQL Server 2016. It is supported 2008 onwards.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Dept_Id E_Id
1 2
1 3
1 4
1 5
I want a output like :1-2,3,4,5
try this,
declare #t table (Dept_Id char, E_Id char)
insert into #t(Dept_Id , E_Id) values
(1,2),
(1,3),
(1,4),
(1,5),
(2,2)
select Dept_Id + '- '+STUFF((SELECT ',' + p.E_Id
from #t p
where p.Dept_Id=n.Dept_Id
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'') as result
from #t n group by Dept_Id