I have so many strings like below
COLGATE GRF 75 GMS (Rs. 65)( 72 PCS )
COLGATE GRF 100 GM (RS.90) - RELAUNCH( 72 PCS )
COLGATE GRF 100 GM (RS.88) COLLECTIBLES( 72 PCS )
COLGATE GRF 100 GMS (Rs. 85)( 72 PCS )
and want to remove (Rs. 65) and so on from string in sql. What I have try is below.
select SUBSTRING ('COLGATE GRF 75 GMS (Rs. 65)( 72 PCS )',0,CHARINDEX('(','COLGATE GRF 75 GMS (Rs. 65)( 72 PCS )'))
You can Use Both SUBSTRING & REPLACE functions to achieve this:
DECLARE #STR VARCHAR(MAX)='COLGATE GRF 75 GMS (RS. 65)( 72 PCS )'
SELECT SUBSTRING (#STR,CHARINDEX('(',#STR),(CHARINDEX(')',#STR)-CHARINDEX('(',#STR))+1)
SELECT REPLACE (#STR,SUBSTRING (#STR,CHARINDEX('(',#STR),(CHARINDEX(')',#STR)-CHARINDEX('(',#STR))+1),'')
Output:
COLGATE GRF 75 GMS ( 72 PCS )
Reading your intentions as removing the first set of brackets from the string (you say nothing about all bracket) then that's simply a single statement:
declare #s varchar(100)='COLGATE GRF 100 GM (RS.88) COLLECTIBLES( 72 PCS )'
select Left(#s,CharIndex('(',#s)-1)+Right(#s,Len(#s)-charIndex(')',#s))
Find the start and end of the parenthesis. For start, find using (Rs. After that use STUFF() to remove it. This will works for (Rs ??) as the first occurrence or not. Note that it will only remove the first occurrence.
SELECT *,
STUFF(col, s, e - s + 1, '')
FROM tbl t
CROSS APPLY
(
SELECT s = CHARINDEX('(Rs', col)
) s
CROSS APPLY
(
SELECT e = CHARINDEX(')', col, s)
) e
dbfiddle demo
Related
I have a data set which looks like
Group abc cde efg ...
PQR 70 60 50
LMN 30 40 30
XYZ 70 80 90
My data set contains around 35000 row & 30000 columns.
I need to write a SQL Query so that It can pool maximum value & the column contributing maximum. My Sample output looks like
Group Max Column
PQR 70 abc
LMN 40 cde
XYZ 90 efg
Can you suggest me how should I proceed ?
select Max(grp) grp,Max(abc) abc, Max(cde) cde,Max(efg) efg into #tmp2 from testtbl
select * from #tmp2
select grp,MX,qq from (select grp,abc,cde,efg from #tmp2) p
UNPIVOT (qq for MX in (
abc,cde,efg
)
) as unpvt
I'm getting frequent errors in code where it says
CustomerSales is not a recognized table hints option. If it is intended as a parameter to a table-valued function, ensure that your database compatibility mode is set to 90.
It's happened with several other tables eg Hires, CustomerNumbers, etc.
I came across this suggestion on google, but I'm not sure if it's the best thing to do? Or what may have caused the error in the first place?
Can anyone please advise what is likely to cause this type of error and the best solution?
ALTER DATABASE database_name SET COMPATIBILITY_LEVEL = { 150 | 140 | 130 | 120 | 110 | 100 | 90 }
https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql-compatibility-level?view=sql-server-ver15
An example of the code I get the error with is this:
WITH CustomerAges AS
(
SELECT
P.FirstName, P.LastName,
FLOOR (DATEDIFF(DAY, PD.BirthDate, GETDATE()) / 365.25) AS AGE
FROM
Sales.vPersonDemographics PD
INNER JOIN
Person.Person P ON P.BusinessEntityID = PD.BusinessEntityID
)
SELECT
*,
CASE
WHEN Age IS NULL THEN 'Unknown Age'
WHEN Age BETWEEN 0 and 17 THEN 'Under 18'
WHEN Age BETWEEN 18 and 24 THEN '18 - 24'
WHEN Age BETWEEN 25 and 34 THEN '25 - 34'
WHEN Age BETWEEN 35 and 49 THEN '35 - 49'
WHEN Age BETWEEN 50 and 64 THEN '50 - 64'
ELSE 'Over 65'
END AS AgeRange
FROM
CustomerAges
I am trying to create a query that will give me random rows from a view (view_XYZ) based on the value in the SampleSizeByRow (in view_SampleSizeByRow) then it inserts the random value from view_XYZ into tbl_Randomization.
view_SampleSizeByRow looks like
ID_x SampleSizeByRow
49 1
87 1
47 6
41 2
247 13
96 3
99 1
31 1
91 13
DECLARE #CurrSize AS INT
DECLARE #CurrID AS INT
DECLARE #SampleByValTbl AS TABLE(
Samplesize INT,
ID INT
)
INSERT INTO #SampleByValTbl (
Samplesize
, ID
)
SELECT SampleSizeByRow, ID_x
FROM view_SampleSizeByRow
ORDER BY ID_x;
WHILE(1=1) -- Infinite Loop until it is broken
BEGIN
SET #CurrID = NULL -- Reset pointer variables
SET #CurrSize = NULL
SELECT TOP(1) #CurrID = ID
FROM #SampleByValTbl
SELECT TOP(1) #CurrSize = Samplesize
FROM #SampleByValTbl
IF (#CurrID IS NULL OR #CurrSize IS NULL) -- End loop if there are no ID's to go through
BREAK
IF #CurrSize <= 0
BEGIN
DELETE TOP(1) FROM #SampleByValTbl
CONTINUE
END
ELSE
BEGIN
WHILE #CurrSize > 0
BEGIN
INSERT INTO tbl_Randomization (
ID_x
, Num_y
)
SELECT TOP(1) ID_x
, Num_y
FROM view_XYZ
ORDER BY NEWID() -- Random Selection
SET #CurrSize = #CurrSize - 1
END
DELETE TOP(1) FROM #SampleByValTbl -- To shorten the size of the table each time we parse through it
END
END
Results should be something like the table below
ID_x Num_y
41 4888084
41 4895898
46 4889509
46 4889493
47 4891612
47 4898679
47 4889485
47 4902432
49 4893435
91 4898447
91 4898892
91 4895738
91 4888350
91 4898920
91 4886422
91 4899137
91 4895741
91 4886918
91 4894146
91 4888301
91 4888383
91 4882095
91 4898402
96 4893927
96 4893738
96 4887504
99 4893713
247 4897332
247 4902343
247 4897769
247 4895529
247 4885333
247 4895512
247 4895488
247 4885279
247 4893031
247 4891872
247 4896523
247 4901417
247 4885181
247 4897541
...
I have tried to use CTE and various joins but I can't figure out how to code them so they return the same results.
If I followed you correctly, you want to select sample_size random values from view_XYZ for each id_x listed in view_SampleSizeByRow.
Here is an approach that uses a recursive query to generate the rows, then a correlated subquery to retrieve the random value:
with cte as (
select id_x, sample_size - 1 from view_SampleSizeByRow where sample_size > 0
union all
select id_x, sample_size - 1 from cte where sample_size > 0
)
insert into tbl_Randomization (id_x, num_y)
select
id_x,
(select top (1) num_y from view_XYZ v where v.id_x = c.id_x order by newid())
from cte c
i want to get maximum student interm marks from below table.
Student name Interm1 marks Interm2 marks Interm3 marks
Raj 60 75 89
raju 78 74 67
ram 67 79 65
balaji 91 89 93
My required Output is:
Balaji 93
option:
raju 78
raj 89 etc..
like this i need output.
can any body help me here for this query.
You'll need to first unpivot your data, and then get the MAX value. I prefer using VALUES to unpivot data, rather than the UNPIVOT operator:
SELECT YT.StudentName,
MAX(IM.ItermMark) AS MaxItermMark
FROM YourTable YT
CROSS APPLY(VALUES(Interm1Mark),(Interm2Mark),(Interm3Nark))IM(ItermMark)
GROUP BY StudentName;
Have a look at this. This should do what you want to do
SELECT [Student name], MAX(MaxMark)
FROM
(
SELECT [Student name],
(SELECT MAX(v) FROM (VALUES ([Interm1 marks]), ([Interm2 marks]), ([Interm3 marks])) AS value(v)
) AS [MaxMark]
) AS subquery
GROUP BY [Student name]
I have an table with this structure (example) ORIGNTABLE:
ID IDParent
80 0
81 80
82 0
83 82
Then I make a duplicate of this data obtaining the following data:
ID IDParent
80 0
81 80
82 0
83 82
---- duplicated data ---
84 0
85 80
86 0
87 82
on the other hand, with the duplication of data also get a temporary table with the following information AUXTABLE:
OldID NewID OldParentID
80 84 0
81 85 80
82 86 0
83 87 82
So, my problem is update the new data, specifically, the IDParent. I'm failing to do this.
I pretend the final data like that:
ID IDParent
80 0
81 80
82 0
83 82
---- duplicated data ---
84 0
85 **84**
86 0
87 **86**
Anyone can help me to update the data? i'm trying many options but none work:
1st try
UPDATE ORIGINTABLE
SET IDParent = AT.NewID
FROM ORIGINTABLE OT
INNER JOIN AUXTABLE AT ON AT.NewID=OT.ID
AND AT.OldParentID=OT.IDParent
2st try
UPDATE ORIGINTABLE
SET IDParent =AT.NewID
FROM ORIGINTABLE OT
INNER JOIN AUXTABLE AT ON AT.OldParentID=OT.ID
The trick is to join AUXTABLE twice. One instance will act as a filter, so that only new rows become updated. The other instance will provide the necessary mapping between old and new ID values.
This is the query:
UPDATE ot
SET ot.IDParent = mapping.NewID
FROM ORIGINTABLE AS ot
INNER JOIN AUXTABLE AS filter ON filter.NewID = ot.ID
INNER JOIN AUXTABLE AS mapping ON mapping.OldID = ot.IDParent
;