How to update multiple values in a SQL server table? Here are the values in the table for the column X:
80, 81, 90
I want these to be updated to 0080, 0081, 0090
I have been updating each value this way:
update TBL_NAME
set TXN_ID = '0094'
where TXN_ID = '94'
I know the 'IN' operator didn't work. Any help would be appreciated. Thank you in advance.
This only makes sense if X is a string. In that case, you can use:
update tbl_name
set txn_id = right('0000' + txn_id, 4)
where txn_id <> right('0000' + txn_id, 4);
If x is a number of any sort, you cannot prepend it with zeros. You would have two options:
Add a generated column that is a string padded with zeros.
Convert the column to a string and then pad the value.
Related
I have a column with the given values
MRN
1946
456
27
557
The column values length is fixed.
If at all any value is less than 6characters,then it should concate 0's to the left and make it 6characters length.
The desired output is
MRN
001946
000456
000027
000557
This is called left paddings. In SQL Server, this is typically done with more basic string operations:
select right(replicate('0', 6) + mrn, 6)
If mrn is a number, then use the concat() function:
select right(concat(replicate('0', 6), mrn), 6)
You can also use the FORMAT function for this. (Demo)
SELECT FORMAT(MRN ,'D6')
FROM YourTable
Change the number 6 to whatever your total length needs to be:
SELECT REPLICATE('0',6-LEN(EmployeeId)) + EmployeeId
If the column is an INT, you can use RTRIM to implicitly convert it to a VARCHAR
SELECT REPLICATE('0',6-LEN(RTRIM(EmployeeId))) + RTRIM(EmployeeId)
And the code to remove these 0s and get back the 'real' number:
SELECT RIGHT(EmployeeId,(LEN(EmployeeId) - PATINDEX('%[^0]%',EmployeeId)) + 1)
We can achieve this by adding leading zero's
select RIGHT('0000'+CAST(MRN AS VARCHAR(10)),6)
I have a database column that is a text string. Some of the values are like
"12345"
and some are as year + sequential number like:
"2016-1, 2016-2, 2016-3, 2017-1, 2017-2, 2017-3" etc.
I want to update the column values to
"2016-001, 2016-002, 2016-003, 2017-001, 2017-002, 2017-003"
for the entire table.
I'm not sure how to do this. Any help would be appreciated. I already updated my stored procedure as such to generate new numbers with zero padding like:
rptnum = cast(year(getdate()) as varchar)
+ '-' + RIGHT('000'+ISNULL(Cast((select count(*)
from dbo.tablename where rptyr = (year(getdate()))) + 1 as varchar),''),3),
Something like this should do:
select left(rptnum, charindex('-', rptnum))+right('000'+substring(rptnum, charindex('-', rptnum)+1, 10), 3)
SQL Server 2008 R2
I have a table similar to this:
Example table:
ID Column
---------------
xxx1234
xxx12345
xxx123456
20150001
I am trying to get a conditional MAX value depending on the value of the column based on whether it meets as certain format. Using the above example, the fourth record, 20150001, represents a "good record" because it contains the current year, and the start of an increment. So, in my table, records that are considered "good" (those subject to the query I am trying to write) have the format "year + increment". The first three, that do not follow this format, should not be conditioned to the query since they don't match this format and should not be subject when computing the max value. Those are bad records. In the above example, the expected result would be "20150002".
The MAX query is simple enough to write, however I am wondering about an approach where I can sanitize the query to only include those records whom meet the particular format, and increment the last four digits (0001 to 0002).
TIA!
You can use the isdate function to filter out ID Columns that do not start with a valid year, and isnumeric to make sure the last 4 characters of the ID Column are valid increments. You also want the len to be 8, given this criteria. You can accomplish all this in the where clause:
-- load test data
declare #Example_Table table(ID_Column varchar(10))
insert into #Example_Table values
('xxx1234'),
('xxx12345'),
('xxx123456'),
('20150001')
-- return max valid ID_Column
select max(ID_Column) as max_ID_Column
from #Example_Table
where isdate(left(ID_Column,4)) = 1
and isnumeric(right(ID_Column,4)) = 1
and len(ID_Column) = 8
-- increment max valid ID_Column
update #Example_Table
set ID_Column = cast(ID_Column as int) + 1
where isdate(left(ID_Column,4)) = 1
and isnumeric(right(ID_Column,4)) = 1
and len(ID_Column) = 8
select * from #Example_Table
ID_Column
----------
xxx1234
xxx12345
xxx123456
20150002
You could use a regular expression to verify a correct year. The second half of the regular expression I taylored to your examples of 0001 and 0002, this could be opened up by adding '[0-9]' for each digit you're expecting.
DECLARE #Sample VARCHAR(30) = '20150001';
SELECT CASE WHEN (#Sample LIKE '[12][09][0-9][0-9]000[12]') THEN 'Yes' ELSE 'No' END;
SELECT
SUBSTRING(#Sample, 1, 4),
SUBSTRING(#Sample, 5, 4),
CASE WHEN (SUBSTRING(#Sample, 1, 4) LIKE '[12][09][0-9]') THEN 'Yes' ELSE 'No' END,
CASE WHEN (SUBSTRING(#Sample, 5, 4) LIKE '[0-9][0-9][0-9][0-9]') THEN 'Yes' ELSE 'No' END;
I have 2 fields in my SQL table. One has 5 digits (ex: 12345) and the 2nd field has 2 digits (ex: 99) and I need to know if there is a way to take the LAST digit from the first field and push it to the beginning of the 2nd field. So the first field would be 1234 and the 2nd field would be 599 ???
UPDATE YourTable
SET Column2 = RIGHT(Column1, 1) + COALESCE(Column2, ''),
Column1 = LEFT(Column1, LEN(Column1)-1)
WHERE COALESCE(LEN(Column1), 0) > 1;
Here is a "typical" way to do this.
update t
set col1 = left(col1, length(col1) - 1),
col2 = concat(right(col1, 1), col2);
The exact details depend on the database.
Notes:
This sort of assumes the values are stored as strings, although the logic will convert back and forth from numbers.
The left() and right() functions may differ by database, although most databases do support them.
concat() may be replaced by an appropriate operator, depending on the database ('||', '+', '&', come to mind).
length() is sometimes called len().
And if it's integer columns:
update tablename set
c1 = c1/10,
c2 = mod(c1,10)*dpower(10,ceiling(dlog10(c2)))+c2
I have a temp table that has numeric integer values in one column. I want to either replace the integer values with character values based on some criteria or I want to add another column of character type that automatically inserts values into itself based on some criteria.
If x <= 1, change to "SP" or make new column and store "SP" in that row
If x > 1, change to "FA" or make new column and store "FA" in that rowAlso, alter commands are not allowed on temp tables in my version of Informix.
SELECT id, yr, CASE WHEN yr_offset <= 1 THEN "SP" ELSE "FA" END CASE
http://publib.boulder.ibm.com/infocenter/idshelp/v10/index.jsp?topic=/com.ibm.sqls.doc/sqls909.htm
You're correct, you cannot alter a temp table. Adding an extra column with this derived value can be done with a CASE statement, ie:
SELECT enroll.ud, enroll.yr, (CASE
WHEN enrollsess.yr_offset <=1 THEN "FA"
ELSE "SP" END)::CHAR(2) AS sess, ...
The casting (ie the parentheses and ::CHAR(2)) are probably not necessary.
If the logic can be expressed as zero/non-zero (it's not clear in your example if yr_offset can be negative), then it's even simpler:
SELECT enroll.id, enroll.yr,
DECODE(enrollsess.yr_offset, 0, "FA", "SP")::CHAR(2) AS sess, ...
More details on CASE syntax from the manual
SELECT enrollsess.id,
enrollsess.yr,
"SP" sess
FROM enrollsess
WHERE enrollsess.yr_offset <= 1
UNION
SELECT enrollsess.id,
enrollsess.yr,
"FA" sess
FROM enrollsess
WHERE enrollsess.yr_offset > 1
INTO TEMP enrollsess2 WITH NO LOG;