Add Zeros to a product number in SQL [duplicate] - sql

This question already has answers here:
Pad a string with leading zeros so it's 3 characters long in SQL Server 2008
(18 answers)
Closed 8 years ago.
I have two tables with product numbers. They both are limited to 12 characters (varchar(12)). One of them (product A) has a number structure like this:
Product No:
2345
568
89
And product B has the same exact numbers but with zeros to fill the 12 characters missing. It is something like this:
Product No:
000000002345
000000000568
000000000089
I just want to modify product A table to add the zeros at the beginning of the sequence. I had an idea with REPLACE() function but to add the zeros I might need another function. Thanks for reading and sorry for the time.

Try this, you can use this statement
RIGHT('000000000000'+ISNULL(ProductNo,''),12)

This should do it:
UPDATE tblA
SET ProductNo = REPLICATE('0', 12 - LEN(ProductNo)) + ProductNo

I just want to modify product A table to add the zeros at the beginning of the sequence.
Big hairy question for you: Are you absolutely certain that you want to store these values in the table with leading zeros, or just be able to display it as-needed with leading zeros?
Reason I ask is because the varchar(12) implies 13 bytes in memory, where an int only takes 4, which will make a big difference if this column participates in indexes and foreign key relationships with other tables.

Related

How do you do multiple substrings for a field in Teradata?

I have a field to pull account numbers which have different lengths and I want to pass the last four digits of the account number. The dilemma I am having is that since they are different lengths I am having trouble in substringing the fields. The standard length is 11 digits but there are accounts with 9 digits and 7 digits.
How do I substring those values in multiple substrings to capture all the account last 4 digits in one query?
This currently what I have:
SELECT SUBSTRING(ACCT_NBR,7,4) AS BNK_ACCT_NBR
FROM NAMEOFTABLE;
I want to have additional substrings to capture the account numbers that don't have 11 digits similar to
SUBSTRING(ACCT_NBR,5,4)
SUBSTRING(ACCT_NBR,4,4)
The results should look like:
76587990891 - 0891
654378908 - 8908
45643456 - 3456
Can you please help me in figuring out how I can do that?
Thanks.
Is ACCT_NBR a VarChar or an INT?
VarChar:
Right (ACCT_NBR,4)
Substr(ACCT_NBR,Char_Length(x)-3)
INT:
ACCT_NBR MOD 10000

Select a range of numbers not in a table in SQL [duplicate]

This question already has answers here:
What is the best way to create and populate a numbers table?
(12 answers)
Closed 4 years ago.
I am wondering if it is possible to get a query that will take a range of numbers, in this case 8 to 17, compare it against a field in a table and remove the ones that do appear in the table and return the rest?
I assume the peusdo code would look something like
Select nums from range(8-17) where nums not in (select column from table)
Is this possible at all?
Edit
To clarify my question.
In table I might have the following:
Intnumber
9
10
16
I would like to have the numbers between 8-17 that do not appear in this table, so 8,11,12,13,14,15,17
Kind regards
Matt
select nums from table where nums not between 8 and 17;

how to manipulate string column of numbers separated by dots using t-sql [duplicate]

This question already has answers here:
How Can I Sort A 'Version Number' Column Generically Using a SQL Server Query
(4 answers)
Closed 5 years ago.
I have a column of nvarchar(255) type that represents software version
numbers:
VersionNumber
---------------
1.0.0.505
1.0.0.506
1.0.0.507
1.0.0.508
1.0.0.509
1.0.1.2
I need to extract the maximum version number (the min version number in the example above is 1.0.0.505 and the max version number is 1.0.1.2, values arranged from the smallest to the highest).
in order to explain exactly what i need - if i could use imperative programming language i think i would do something like that to detect the max version number:
lets say version number is d.c.b.a.
i would separate each version number to four different variables: a b
c d
that i will sum each series.
a will be summed by tens
b will be summed by hundreds
c will be summed by thousands
d will be summed by milions
than the maximum total sum of each Max(a+b+c+d) will be the max version.
but what is the technic to achieve something like that in sql?
for future readers: based on #AlexK. link that is the solution:
select TOP 1 VersionNumber from Users order by (cast('/' + replace(VersionNumber , '.', '/') + '/' as hierarchyid)) DESC;
try this
select max(replace(version,'.','')) from yourtable

Access Update Query First 5 digits, but last number goes up one

I am using Access Update Query to change a column to the first 5 digits, I got that part. But I ALSO need the last digit to go up one. So if its 12345 I need it to be 12346.
This is what I have so far:
Left([Num],5)
Try this:
CLng(Left([Num], 5)) + 1
The CLng is only necessary if the original column isn't already a number field.

SQL query to extract a number and its decimal variations [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have column with all kinds of numbers, I am specifically trying to extract numbers that have either
555
or
555.xx
or
555.x
The output should look like this
555
555.1
555.5
555.9
555.58
555.22
.
.
IE I need an sql query that will return the rows that have just the number 555 with any decimal fraction from my column of arbitrary numbers.
You can try LIKE statement
WHERE Col LIKE '555.%'
OR Col = '555'
As a fast approach I would do
CAST((ValueOfTable * 100.00) AS DECIMAL(18, 2))
my table name is Diagnosis and the column name is code, where should I
add the table name and column name in this code ?
In your situation:
SELECT
CAST((code * 100.00) AS DECIMAL(18, 2))
FROM Diagnosis ;
I'm expecting this to be an integer. You can find out executing:
\d Diagnosis ;
one of the output lines should look similar to
(...)
code | integer |
(...)
Assuming the column contains numbers (not as string/varchar), search for "number>=555 and number<556". This would give you 555, 555.01... etc.
If the CODE column is a varchar (which I understand it to be from your comments, but you might want clarify that in the question body itself), and can/does contain values that are not numbers, then you have to be very careful about using functions which only accept a number.
With this sample data, you can see that having one value that can't be cast to a number will cause the whole query to error out.
select * from diagnosis order by code;
CODE
-----------
555
555.0
555.43
555.99
Not a Num
(5 rows)
select code + 1 from diagnosis;
ERROR: pg_atoi: error in "Not a Num": can't parse "Not a Num"
The usual solution to this is to either match the column value via regular expression, or use a function to test whether or now the value in each row is a number.
Here are two solutions, each of which depends on a function that is provided with Netezza, but not necessarily installed by default. Your administrator can install these for you.
The first uses the regexp_instr from the SQL Extension Toolkit. Here you use a regular expression to match the values you want without having to do an actual CAST (implicit or explicit) to a numeric.
SELECT code FROM diagnosis
WHERE regexp_instr(code, '^555(\.\d+)?$') > 0;
CODE
--------
555
555.0
555.43
555.99
(4 rows)
The second solution, which is a bit more involved, uses the isnumeric() UDF provided as part of the Netezza InDatabase Analytics package (in the /nz/extensions/nz/nzlua/examples directory when installed), to test whether the CODE column is a numeric before casting CODE as a numeric.
SELECT code
FROM (
SELECT code
FROM diagnosis
WHERE isnumber(code)
)
foo
WHERE floor(code::NUMERIC(38,2)) = 555;
CODE
--------
555
555.0
555.43
555.99
(4 rows)
Both of these functions are included with Netezza, but both require installation by your administrator before you can use them. In each case this is a simple task for the administrator, although they may not be aware of their availability.