Append number to front of string with leading 0 - sql-server-2008

How do I correctly replace the first character with 'M'? Suppose you have a PATIENT_ID_NONNUM = 'M001', and we want 1001 as a result.
UPDATE [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_SRC_BIOMETRICS]
SET PATIENT_ID = CONVERT(NUMERIC(22,0),'1' + CONVERT(NVARCHAR(50),PATIENT_ID))
WHERE SUBSTRING(PATIENT_ID_NONNUM, 1, 1) = 'M'
EDIT:
UPDATE [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_SRC_MEDICATION]
SET PATIENT_ID = CONVERT(NUMERIC(22,0),CONVERT(NVARCHAR(50),'1') + CONVERT(NVARCHAR(50),SUBSTRING(PATIENT_ID_NONNUM, 2, LEN(PATIENT_ID_NONNUM))))
WHERE SUBSTRING(PATIENT_ID_NONNUM, 1, 1) = 'M'

I find STUFF() (an often overlooked function) and LEFT() are a little more readable, but others may disagree:
UPDATE [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_SRC_BIOMETRICS]
SET PATIENT_ID = CAST(STUFF(PATIENT_ID_NONNUM, 1, 1, '1') AS NUMERIC(22,0))
WHERE LEFT(PATIENT_ID_NONNUM, 1) = 'M'

I would suggest something like this:
UPDATE [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_SRC_BIOMETRICS]
SET PATIENT_ID = CAST(('1' + SUBSTRING(PATIENT_ID_NONNUM, 2, LEN(PATIENT_ID_NONNUM) - 1)) AS NUMERIC(22,0))
WHERE SUBSTRING(PATIENT_ID_NONNUM, 1, 1) = 'M'
That's going to find all records where the first character is M, and replace the first character with a 1. I haven't tested this, but I believe it should work properly.
I would also suggest not running this type of operation on a production database as a test, which I would assume is what the -PROD stands for in your catalog name.
EDIT: Since it seems important that this query comes out with the PATIENT_ID as a NUMERIC(22,0), I've added the necessary CAST.

Related

query for serial number

Just want to get an idea & advise.. how to get the exact result for my situation when query..see below table..
I try to use this query and its look fine..but the problem is when I give the input ABC1001Z (different only last character Z).. the query still return Honda as result.. it's supposed not return any result/no result found.. any solution for my case?
SELECT Name
FROM CarNo
WHERE ('ABC1001Z' BETWEEN Start AND End)
AND (len('ABC1001Z') = len (Start));
Your kind support is much appreciated..
Update you code to
SELECT Name FROM CarNo
WHERE (SUBSTRING('ABC1001Z',0,7) BETWEEN SUBSTRING(Start,0,7) AND SUBSTRING(End,0,7) )
AND (len('ABC1001Z') = len (Start));
Maybe that's what you are looking for:
SELECT Name FROM CarNo
WHERE (Start = 'ABC1001Z' OR End = 'ABC1001Z')
AND (len('ABC1001Z') = len(Start));
You seem to want the "number" in the middle to be treated as a number. This suggests something like this:
where left('ABC1001Z', 3) between left(start, 3) and left(end, 3) and
substr('ABC1001Z', 4, 4) + 0 between substr(start, 4, 4) + 0 and substr(end, 4, 4) + 0
I'm not sure how the last character relates to a between query of this form, so I'm not addressing that.

SET inside IF in MYSQL(?)

I'm new in MYSQL and I'm trying to do this:
UPDATE team
SET member1 = IF(member1=0, 001, SET member2 = IF(member2=0, 001, member2)) WHERE ID = ?;
This is not working, explain:
Each "member" has a different number (001, 002, 003...), I want to put the member "001" in the table "member1" but if this is ocupated (it has a value different of 0) set the member "001" in the table "member2" (a diferent table) but I think is not possible to put a SET inside of an IF. The id doesn't matters.
Thanks for attention ^^
No you can't join SET statements like this. It'll have to be worked around:
UPDATE `team`
SET
`member2` = CASE WHEN `member1` = 0 AND `member2` = 0 THEN '001' ELSE `member2` END,
`member1` = CASE WHEN `member1` = 0 THEN '001' ELSE `member1` END
WHERE
`id` = ?
/* optional filter: considering adding it if it does not bother the performance: */
AND (`member1` != 0 OR `member2` != 0)
;
Notes:
I used backticks around table and column names, I recommend you do so*
I used CASE/WHEN instead of IF, because IF is not ansi sql (it is specific to mysql)
You could also do two queries instead of one. That would allow you to use a filter in the where clause, so you don't have to update the column when it's not required.
*as pointed out by Bohemian, they are not ansi. I like to use them for the structural and visual help they provide.

MySQL update with sequential number yields NULL

I want to update the values of a column with the name of a related entry appended with a number for each row. I've seen a couple of other questions which give roughly the same answer, but when I try the below, I get NULL added instead.
SET #i = 0;
UPDATE matrix_data md
SET col_id_4 = concat((SELECT title from titles t WHERE t.entry_id = md.entry_id), (#i:=#i+1));
If I replace (#i:=#i+1) with a static value, then the update works OK.
The col_id_4 column is set to text. Does the above only work with numeric column types? And if so, how do I achieve what I want to do?
How about you take the primary key of the titles table instead of using the iterator, thus filling in col_id_4 with a concatenation of titles.title and titles.entry_id, thus…
UPDATE matrix_data
INNER JOIN titles ON matrix_data.entry_id = titles.entry_id
SET matrix_data.col_id_4 = CONCAT(titles.title, "_", titles.entry_id)
Or maybe it’s a type issue; casting the iterator to char(50) as we concatenate with the string should work. Tested in a rudimentary database, not #craftcms or #eecms specifically.
SET #i := 0;
UPDATE matrix_data
INNER JOIN titles ON matrix_data.entry_id = titles.entry_id
SET matrix_data.col_id_4 = CONCAT(titles.title, "_", CAST(#i := #i + 1 as CHAR(50)))

Write to database from case results

I want to update a databased based upon results from a case, however my "If" statement doesn't seem to pull information from the right table.
declare #dt datetime
set #dt = GETDATE()
select
Ugenummer = datepart(wk, #dt) - datepart(wk,dateadd(m, DATEDIFF(M, 0, #dt), 0)) + 1,
case when (datepart(wk, #dt) - datepart(wk,dateadd(m, DATEDIFF(M, 0, #dt), 0)) + 1) % 2 = 1
then 'ulige' else 'lige' end Ugelighed;
The above code gets the weeknumber (ugenummer) and determines whether it's odd or even (Ulige/lige)
IF Ugelighed = ulige and datepart(dw,#dt) = 1
THEN
UPDATE laeger
SET Antal=1
WHERE Navn=Lægenavn
I would like to use the information from the first code to update a database in a SQL 2008 server
You aren't using Ugenummer so I edited it out. Also created a variable for Ugelighed, you forgot to create one:
declare #dt datetime
set #dt = GETDATE()
declare #Ugelighed VARCHAR(5);
select
#Ugelighed=case when (datepart(wk, #dt) - datepart(wk,dateadd(m, DATEDIFF(M, 0, #dt), 0)) + 1) % 2 = 1
then 'ulige' else 'lige' end;
In the following statements I used variable #Ugelighed to compare to ulige however it is a VARCHAR so you need to enclose it in single quotes (''). Same for Lægenavn, it is a VARCHAR so enclose it in single quotes:
IF #Ugelighed = 'ulige' and datepart(dw,#dt) = 1
UPDATE laeger
SET Antal=1
WHERE Navn='Lægenavn'
Personally I would have created a BIT field for #Ugelighed since even/odd can be captured as a simple boolean rather than text.

Updating column values as per our format

There are two types of records in my Db such as MS-NW and CS in the same column of table DICIPLINE I want to wrap if its CS (ANY TWO STRING LIKE CS,TE OR THE LIKE) then wrap it to BS(CS) (OR BS(TE) ETC) or if its MS-NW (Or MS-CS, MS-TE and the like) then wrap it to MS(NW) from the column dicipline.
I updated for two strings successfully and following is the query for that kindly let me know how can i do it for values like MS-NW OR MS-CS and convert it to the format like MS(NW) from following query .
UPDATE DEG set DICIPLINE = concat("BS(",DICIPLINE,")") where CHAR_LENGTH(DICIPLINE) = 2
The below query helps you to update your data.
update deg set DISIPLINE = if(length(DISIPLINE)= 2,concat('BC(',DISIPLINE,')')
,concat('MS(',substr(DISIPLINE, 4,4),')'));
See Sqlfiddle demo.
For safety, create a temporary column of same type and perform an update like this:
UPDATE deg
SET dicipline_temp = CASE
WHEN CHAR_LENGTH(dicipline) = 2
THEN CONCAT('BS(', dicipline, ')')
WHEN CHAR_LENGTH(dicipline) = 5 AND SUBSTRING(dicipline, 3, 1) = '-'
THEN CONCAT(REPLACE(dicipline, '-', '('), ')')
END
WHERE CHAR_LENGTH(dicipline) = 2 OR (CHAR_LENGTH(dicipline) = 5 AND SUBSTRING(dicipline, 3, 1) = '-')
If results are acceptable, update the actual column.