I have a serial no column in my table. I want to insert values like this:
00001
00002
.........
00010
00011
........
00100
How can I write a query for this?
Since you are trying to store a number and padding with leading zero wont mean anything.
But while displaying you can pad it with leading zero and select number of digit you want using RIGHT() function
SELECT RIGHT('00000' + CAST([serial_number] AS varchar(5)) , 5)
FROM [Table_name]
Related
I need to add leading zero in my records, but it doesn't work then I have no idea to solve this. the data as follow
textmess
convert
8
08
8
008
14
014
this is the query i have run
Select right('000' + rtrim(ltrim(cast(TEXTMESS as varchar(3)))), 3) convert, TEXTMESS from mytable
I thought it would work but it didn't work for first row, it should be 008 right? why the result is different with second row? . please help to solve this query
You can go for FORMAT and apply preceding 0.
DECLARE #table table(textmess int)
insert into #table
values (8),(8),(14)
SELECT textmess, FORMAT(textmess,'000') as convertedTExt from #table
textmess
convertedTExt
8
008
8
008
14
014
It seems that you have a special non-visible character in that row. First check that varchar value in hex to check which character code is it:
Select
right('000' + rtrim(ltrim(cast(TEXTMESS as varchar(3)))), 3) AS Converted,
TEXTMESS,
CONVERT(VARBINARY, TEXTMESS) AS HexValues
from
mytable
Seems that for a row you have 0x38 and for the other 0x381C. Assuming that the values are in ASCII, 38 is the 8 and 1C is the hex for symbol FS (file separator).
If you check any ASCII table online, you can see that the code for FS is 28, so just use REPLACE with CHAR(28). Check the result first:
Select
right('000' + rtrim(ltrim(cast(TEXTMESS as varchar(3)))), 3) AS Converted,
TEXTMESS,
REPLACE(TEXTMESS, CHAR(28), '') AS Replaced,
right('000' + rtrim(ltrim(cast(REPLACE(TEXTMESS, CHAR(28), '') as varchar(3)))), 3) AS ReplacedConverted
from
mytable
Then remove the character with UPDATE for all rows that have any FS:
UPDATE T SET
TEXTMESS = REPLACE(TEXTMESS, CHAR(28), '')
FROM
mytable AS T
WHERE
CHARINDEX(CHAR(28), TEXTMESS) > 0
try this
SELECT RIGHT('000'+CAST(textmess AS VARCHAR(3)),3)convertedText, textmess from mytable
This may sound like a weird request, but I have to make an excel sheet with EXACTLY the same format as their old sheet. The values in the column in question will be a numeric code. I need to display up to 8 digits, and no less than 6 digits. The code will only be 7 or 8 digits if the first number in the sequence is not zero. If there are 6 digits or less, I need to display 6 digits including leading zeroes. Here is an example:
The data comes in like this:
000023547612
000000873901
000031765429
000000000941
000000055701
I need those numbers to display as:
23547612
873901
31765429
000941
055701
Is there a way to achieve this in a SQL statement?
You can check the length of the string and use right to pad it. But GSerg's answer is better.
declare #Test table (Number varchar(12))
insert into #Test (Number)
values ('000023547612'),('000000873901'),('000031765429'),('000000000941'),('000000055701')
select Number
, case when len(convert(varchar(12), convert(int, Number))) <= 6 then right('000000'+convert(varchar(12), convert(int, Number)),6) else convert(varchar(12), convert(int, Number)) end
, format(convert(int, Number), N'##000000') -- GSerg's Answer
from #Test
Returns:
Number Attempt1 Attempt2 (GSerg)
000023547612 23547612 23547612
000000873901 873901 873901
000031765429 31765429 31765429
000000000941 000941 000941
000000055701 055701 055701
PS: In future if you provide test data in this format (table variable or temp table) you will make it much easier for people to answer.
You can use
SELECT Substring('0078956', Patindex('%[^0 ]%', '0078956' + ' '), Len('0078956') ) AS Trimmed_Leading_0;
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 am trying to find the best way to extract specific values from a string which can contain nulls/duplicates for the values. The issue is I have to do this in a query and pull those values into a view for use down the line.
Example of the string:
ABCD: 123 EFG: 03 HIJ: NGAB XYZ: XYZ: 133
EFG: 03 HIJ: NGAB XYZ: 133
I am trying to extract the values for ABCD, EFG, HIJ, and XYZ.
For example, the first string should return:
123 (for value of ABCD)
03 (for value of EFG)
NGAB (for value of HIJ)
133 (for value of XYZ)
Second string should return:
NULL (for value of ABCD)
03 (for value of EFG)
NGAB (for value of HIJ)
133 (for value of XYZ)
The length of the values to return are always static (i.e. ABCD will always be ABCD and 123 will always be the length of the value to return - i.e 3 characters. Same applies for EFG and 03 - EFG will always be EFG and 03 will always be 2 characters and so on).
I am trying to use below to try and return my values:
SELECT substring(replace(replace(TEMPFIELD,' ',''),':',''), charindex('XYZ',replace(replace(TEMPFIELD,' ',''),':',''))+3,3) AS XYZ FROM MYTABLE
I change my query per field and adjust the length on the substring as needed. The issue is that when there are duplicates, I return the wrong values and when there are nulls, I return the wrong value.
For example, my query returns XYZ as the value of XYZ in the first string instead of 123. It also returns 03H as the value of ABCD in the second string instead of NULL. Is there a better function for me to use in this case to handle both scenarios of nulls/duplicates?
Updated query:
SELECT CASE WHEN TEMPFIELD LIKE '%XYZ%XYZ%'
THEN substring(stuff(replace(replace(TEMPFIELD ,' ',''),':',''),charindex('XYZ',replace(replace(TEMPFIELD ,' ',''),':','')),3,''), charindex('XYZ',stuff(replace(replace(TEMPFIELD ,' ',''),':',''),charindex('XYZ',replace(replace(TEMPFIELD ,' ',''),':','')),3,''))+3,3)
WHEN TEMPFIELD LIKE '%XYZ%'
THEN substring(replace(replace(TEMPFIELD ,' ',''),':',''), charindex('XYZ',replace(replace(TEMPFIELD ,' ',''),':',''))+3,3)
ELSE NULL
END AS XYZ
Ideally if you can you would want to sort out the string you are trying to pass as this could get very messy, but I can offer you this solution, based on the following assumptions:
Duplicated keys only occur twice as per your example
The values cannot contain the key
That when the keys are duplicated that the last one is the value you want.
CASE WHEN TEMPFIELD LIKE '%XYZ%XYZ%'
THEN SUBSTRING(TEMPFIELD, CHARINDEX('XYZ: ', TEMPFIELD, CHARINDEX('XYZ: ', TEMPFIELD) + 1) + 5, 3)
WHEN TEMPFIELD LIKE '%XYZ%'
THEN SUBSTRING(TEMPFIELD, CHARINDEX('XYZ: ', TEMPFIELD) + 5, 3)
ELSE NULL
END AS XYZ
Add in the replacements if you really need to but if you know the format is reliable enough you shouldn't need to do this as it will just add processing time.
To explain what this does a bit: the first case statement will deal with the duplicated values by getting the CHARINDEX of the second key, if that case criteria does not match then it will fall to the second case and do something similar to what you were already doing and finally if the string does not match either of those it simply gives you null.
I have the following data in a column of a table which is not a primary key
0000
0001
0002
I would like to increment the value to increment by 1, which means result should be
0003
using a sql statement.
How can I do this
Column data type is varchar2
Edit 1
SELECT col + 1
FROM tab2
where prod_id = 'A267'
The result is 3, ideally it should be 0003
Use LPAD fo adding '0' left side and TO_CAHR for converting number in string (because col+1 get a number)
SELECT LPAD(TO_CHAR(col + 1),4, '0')
FROM tab2
where prod_id = 'A267';
In fact for oracle numbers 0003 and 3 are the same, difference only in output formatting
In case you want format your output or use this value as a char sequence, you can just follow to this article -https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm and do something like this:
SELECT TO_CHAR(number, '0999')
FROM DUAL;
If you want just use this value for sub-select as number don't take this difference into account.