Update multiple columns with Different Values - sql

I have table following this format :
Id BillNo: Voucher No:
1 W2015-16/0001 W2015-16/0001
2 W2015-16/0002 W2015-16/0002
3 W2015-16/0003 W2015-16/0003
4 W2015-16/0004 W2015-16/0004
5 W2015-16/0005 W2015-16/0005
6 W2015-16/0006 W2015-16/0006
7 W2015-16/0007 W2015-16/0007
8 W2015-16/0008 W2015-16/0008
9 W2015-16/0009 W2015-16/0009
10 W2015-16/0010 W2015-16/0010
But Instead of this Format:
Now I want to Update All "Voucher No" and "Bill No" below on this format :
Id BillNo: Voucher No:
1 W0001/2015-16 W0001/2015-16
2 W0002/2015-16 W0002/2015-16
3 W0003/2015-16 W0003/2015-16
4 W0004/2015-16 W0004/2015-16
5 W0005/2015-16 W0005/2015-16
6 W0006/2015-16 W0006/2015-16
7 W0007/2015-16 W0007/2015-16
8 W0008/2015-16 W0008/2015-16
9 W0009/2015-16 W0009/2015-16
10 W0010/2015-16 W0010/2015-16
11 W0011/2015-16 W0011/2015-16
like this i have a 1000+ records i should update , But i don't know is this possible to do in Lesser time, Kindly give your suggestion , i am new to SQL
Thanks Advance

Assuming everything is in a fixed format, then you would just use update and string manipulations:
update table t
set billno = left(billno, 1) + right(billno, 4) + '/' + substring(billno, 2, 7),
VoucherNo = left(VoucherNo, 1) + right(VoucherNo, 4) + '/' + substring(VoucherNo, 2, 7);

UPDATE YourTable
SET BillNo = SUBSTRING(BillNo,1,1) + SUBSTRING(BillNo,10,4) + '/' + SUBSTRING(BillNo,2,7)
, VoucherNo = SUBSTRING(VoucherNo,1,1) + SUBSTRING(VoucherNo,10,4) + '/' + SUBSTRING(VoucherNo,2,7)
Test:
DECLARE #a NVARCHAR(max) = 'W2015-16/0001'
SELECT SUBSTRING(#a,1,1) +
SUBSTRING(#a,10,4) + '/' + SUBSTRING(#a,2,7)

There is no straight forward way to achieve this. You may need to define some patterns in SQL to replace and play with all string functions. Here is
declare #name varchar(50) ='W2015-16/0001';
select SUBSTRING(STUFF(#name,2,0,(SUBSTRING(#name,CHARINDEX('/',#name) + 1,len(#name)) + '/')),0,14)
--W0001/2015-16
This gives you the result that you are looking for. So you need to set this into your update statement by joining with the table by ID. This would work only if the pattern you have given is correct.

Related

How can I check the maximum value from a set of tables in SQL Server (if possible)?

We have a set of databases (80 in total). Every single one has a table called tblProfessions. The tables are not standardized. For example:
EDIT: all the databases are on the same server.
The DB1.dbo.tblProfessions is like:
intProfessionCode
strProfessionDescription
1
lawyer
2
dentist
...
...
30
doctor
And the DB72.dbo.tblProfessions is as follows:
intProfessionCode
strProfessionDescription
1
designer
2
butcher
...
...
80
chef
Suppose I ran a script from DBO1 to DBO72, and I found that the biggest table has 80 entries (in this case the DBO72 is the biggest one).
By my limited knowledge, all I know is to run the below script database by database, and write it down in a spreadsheet manually:
SELECT MAX(intProfessionCode) FROM [DB].dbo.tblProfessions;
Is there a script to run and loop through all the tblProfessions and get the one with the most entries? All I want is the biggest number found.
Thanks in advance.
You should be able to do something like this:
WITH dat
AS
(
SELECT 'db1' AS database_name, MAX(intProfessionCode) AS max_intProfessionCode
FROM DB1.dbo.tblProfessions
UNION ALL
...
UNION ALL
SELECT 'db72' AS database_name, MAX(intProfessionCode) AS max_intProfessionCode
FROM DB72.dbo.tblProfessions
)
SELECT dat.db, dat.max_intProfessionCode
FROM dat
INNER JOIN (SELECT MAX(max_intProfessionCode) AS max_intProfessionCode_overall
FROM dat) y
ON dat.max_intProfessionCode = y.max_intProfessionCode_overall
For situations like this, I usually query the catalog to write the above script rather than typing it out:
WITH
dat AS
(
SELECT STRING_AGG('SELECT ''' + QUOTENAME(s.name) + ''' AS db,
MAX(intProfessionCode) AS max_intProfessionCode
FROM ' + QUOTENAME(s.name) + '.' + QUOTENAME('dbo') + '.' + QUOTENAME('tblProfessions') + '
UNION ALL',' ') AS s
FROM sys.databases s
WHERE s.name LIKE 'DB%' --EDIT APPROPRIATELY
)
SELECT 'WITH dat
AS
(' + SUBSTRING(s,1,LEN(s) - LEN(' UNION ALL')) + ')
SELECT dat.db, dat.max_intProfessionCode
FROM dat
INNER JOIN (SELECT MAX(max_intProfessionCode) AS max_intProfessionCode_overall
FROM dat) y
ON dat.max_intProfessionCode = y.max_intProfessionCode_overall' AS scrpt
FROM dat;
Make sure the above is only returning data for the appropriate databases by editing the WHERE clause in the CTE, then copy the output, paste elsewhere and run it to get your results.

Hi,. I want ssms query (SQL query)

I want ssms query (SQL query) which will help me in getting YTD data.
Example if I am in Jan month then take Jan data and create column to add month=1,
if I am in feb month then take Jan + feb and month column=2.
Which means when I will in feb I will get Jan with month=1 and Jan+ feb with month=2. And so on...
SELECT will be able to add months together for you, here's a query I ran on my crime database recently, you can change as needed:
--returns the total of all 2019
select LGA, ([Jan-2019] + [Feb-2019] + [Mar-2019] + [Apr-2019] + [May-2019] + [Jun-2019] + [Jul-2019] + [Aug-2019] + [Sep-2019] + [Oct-2019] + [Nov-2019] + [Dec-2019])
FROM Crime_LGA
alternatively, you can make a new column for each MTD, or maybe even a new table. Here's some code i used recently to add columns:
--to add a total 2019 column
ALTER TABLE Crime_Suburb
ADD Total_2010 int NOT NULL DEFAULT (0),
--to add values to the column
UPDATE Crime_Suburb
SET Total_2010 = ([Jan-2010] + [Feb-2010] + [Mar-2010] + [Apr-2010] + [May-2010] + [Jun-2010] + [Jul-2010] + [Aug-2010] + [Sep-2010] + [Oct-2010] + [Nov-2010] + [Dec-2010])
FROM Crime_Suburb

How to split a string in sql server using stored procedure and insert the data to table

<pre>update d
set d.Price = null
from dbo.SalDocumentDetail d
left join dbo.StkWarehouse w on w.WarehouseID = d.WarehouseID
where DocumentID=" + 1 + "
and DocumentTypeID=" + 2 + "
and FiscalYear= " + 2016 + "
and isnull(isPrescription,0) <>1
and w.POSType is null
and ProductName BETWEEN ''C' 'AND' 'M''
and Country LIKE ''%land%'''</pre>
Actually this string is only a sample one my original string is very large . i am not getting a point that if i break this string than how many variables i have to make to capture the data also after splitting the string i want that to be inserted into data table containing columns as Felid and Value?
I want my result like :
<pre>
Felid Value
DocumentID= 1
DocumentTypeID= 2
FiscalYear= 2016
isnull(isPrescription,0) <>= 1
w.POSType is= null
ProductName= C
ProductName= M
Country= land
</pre>
<pre>I Use this function but this function split 'and' not split like what i want in my result i want split 'and,or,like,is,between ' if function find any this split it to two columns (Felid and Value)</pre>
<pre>ALTER FUNCTION [dbo].[fnSplitString]
(#List NVARCHAR(MAX),#Delimiter NVARCHAR(255))
RETURNS #Items TABLE(Felid NVARCHAR(Max),Valu nvarchar(MAx))
WITH SCHEMABINDING
AS BEGIN
DECLARE #ll INT=LEN(#List)+1,#ld INT=LEN(#Delimiter);
WITH a AS
(SELECT
[end]=COALESCE(NULLIF(CHARINDEX(#Delimiter,#List,1),0),#ll),
[VlaueFelid]=SUBSTRING(#List,(select
CHARINDEX('where',#List)+5),COALESCE(NULLIF(CHARINDEX('=', #List,0),0),#ll) ) ,
[Value]=SUBSTRING(#List,(select CHARINDEX('="',#List)+2),(select CHARINDEX('and',#List))-(select C`enter code here`HARINDEX('="',#List)+3))
UNION ALL
SELECT
[end]=COALESCE(NULLIF(CHARINDEX(#Delimiter,#List,[end]+#ld), 0),#ll),
[VlaueFelid]=SUBSTRING(#List,[end]+#ld, COALESCE(NULLIF(CHARINDEX('=',#List, [end]+#ld),0),#ll)-[end]-#ld),
[Value]=SUBSTRING(#List,[end]+#ld+16, COALESCE(NULLIF(CHARINDEX('=',#List,[end]+#ld),0),#ll)-[end]-#ld-5)
FROM a WHERE [end]< #ll) INSERT #Items SELECT[VlaueFelid],[Value] FROM a WHERE LEN([VlaueFelid])>0 RETURN;
END</pre>

Dynamic XML node name in SQL XML

I am updating an XML column with values from columns in a temp table. I can update the table as below.
UPDATE tbWorkflow
SET xmlData.modify('insert
(<FromQueueName>
<CustomerID>{ sql:column("T.iVTollCustID") }</CustomerID>
<Date>{ sql:variable("#CurrDateTime") }</Date>
</FromQueueName>)
as first into (/configuration)[1]'),
vcQueue = 'qVtoll',
dtUpdTime = GETDATE()
FROM #Trxns T
WHERE T.biWorkflowID = tbWorkflow.biWorkflowID
However, I want the node name to be dynamic (from the temp table) like below. But it does not work.
UPDATE tbWorkflow
SET xmlData.modify('insert
(<**{ sql:column("T.vcQueue") }**>
<CustomerID>{ sql:column("T.iVTollCustID") }</CustomerID>
<Date>{ sql:variable("#CurrDateTime") }</Date>
</**{ sql:column("T.vcQueue") }**>)
as first into (/configuration)[1]'),
vcQueue = 'qVtoll',
dtUpdTime = GETDATE()
FROM #Trxns T
WHERE T.biWorkflowID = tbWorkflow.biWorkflowID
Any help is appreciated.
I think, you should use a subquery to select a string you need and then insert it into XML.
For example, in subquery get a <YourTag><CustomerID>123</CustomerID><Date>15.10.2012</Date></YourTag> and then you can simply insert it in XML
I found a workaround to do this.
I added another VARCHAR column to my temp table, created the xml node as a varchar and later modified my actual xml column using this varchar column.
UPDATE #Trxns
SET xmlVarchar = '<' + vcQueue + '>
<CustomerID>' + CONVERT(VARCHAR(10),iVTollCustID) + '</CustomerID>
<Date>' + CONVERT(VARCHAR(25), GETDATE(),22) + '</Date>' +
'</' + vcQueue + '>'
UPDATE tbWorkflow
SET xmlData.modify('insert
sql:column("xmlVarchar")
as first into (/configuration)[1]'),
vcQueue = 'qVtoll',
dtUpdTime = GETDATE()
FROM #Trxns T
WHERE T.biWorkflowID = tbWorkflow.biWorkflowID

Add a random number between 30 and 300 to an existing field

I have looked on the net as well as here but can't find an answer to the following MySQL question. I'm looking to replace the value of an existing field with a query that has a random number between 30 and 300.
Reason was because I've moved galleries and had 250,000,000 views on my images and there have been lost with the migration and a lot of my members are upset that they have lost views....
UPDATE the_table SET the_field = the_field + FLOOR(RAND() * (270 + 1)) + 30
Use RAND()
UPDATE table
SET field = FLOOR(30 + (RAND() * 270));
WHERE foo = 'bar'
I think this will do the trick:
UPDATE table SET field = ROUND(30 + (RAND() * 270)) WHERE id =1;