How do I append zeroes at the end of two combined fields? - sql

I have these 2 fields, SBSNBR, SEQNBR and I need to combine them into a 50 character length field. The total records of the two fields will be 11 in total and I want the remaining to populated with zeroes. Below is an example of the expected output.
Characters are left justified so it would look like this:
12345678900000000000000000000000000000000000000000000000000

You can use the LEFT function.
SELECT LEFT(SBSNBR+SEQNBR+'00000000000000000000000000000000000000000000000000',50)
FROM Table

Related

String length mismatch not giving data in another column

I've a column location in my table. I've used it as a common column to join two tables
1.Factory
2.Inventory
Like
Factory.location = inventory.location
But I'm having a problem..
For example Location in factory table just has 09 where inventory table has 009 doesn't match but for three digit numbers it's matching eg: 115 = 115, 999=999. But in the output, I still get 009 but I'm not getting data in another column for the ones which doesn't have three digits.
Please tell me how to make it 3 digit and join.
I tried putting it as
(Case when
length(factory.location)<3
Then concat('0',factory.location)
else inventory.location
End) as location
in select statement.
Please help
Assuming you're storing them as varchar for a reason, you could pad them to equalize length before matching. I chose 3 because it sounded like all the locations are 3 characters long. Change as required
lpad(f.location, 3, '0') = lpad(i.location, 3, '0')
If the values in location columns are all numbers that can be treated as integers, you could also do
f.location::int = i.location::int

SQLite3 Order by highest/lowest numerical value

I am trying to do a query in SQLite3 to order a column by numerical value. Instead of getting the rows ordered by the numerical value of the column, the rows are ordered alphabetically by the first digit's numerical value.
For example in the query below 110 appears before 2 because the first digit (1) is less than two. However the entire number 110 is greater than 2 and I need that to appear after 2.
sqlite> SELECT digit,text FROM test ORDER BY digit;
1|one
110|One Hundred Ten
2|TWO
3|Three
sqlite>
Is there a way to make 110 appear after 2?
It seems like digit is a stored as a string, not as a number. You need to convert it to a number to get the proper ordering. A simple approach uses:
SELECT digit, text
FROM test
ORDER BY digit + 0

T - SQL statement for number ranges

Create table temp
(
ID nvarchar(50)
)
ID contains numeric values prevailing zeros in some cases so it is defined as varchar
How to get values starts with 3555 to 3999 and 8000 to 9999.There is no specific rule that length is always 4.
Eg:
3555
35688888888888
3590909
8000
85805667
all of the values are valid and are to be fetched.
Please let me know T- SQL statement for the above scenario
You can use few expressions with LIKE. If you have an index on ID, it would use it, so it will be efficient. Something like this:
SELECT
ID
FROM
temp
WHERE
ID LIKE '3[5-9]%'
OR ID LIKE '[89]%'
LIKE '3[5-9]%' matches any string that starts with 3 and which second character is 5 or 6 or 7 or 8 or 9. After these two characters there can be 0 or more other characters. Any number of extra characters.
LIKE '[89]%' matches any string that starts with 8 or 9 and any number characters after.
You can extract the first four chars, convert that to a number and query like this:
SELECT
[ID]
FROM temp
WHERE convert(int,LEFT([ID],4)) BETWEEN 3500 AND 3999
OR convert(int,LEFT([ID],4)) BETWEEN 8000 AND 9999
For lots of data this will be horribly slow, so if you need performance I would recommend to add an indexed int column to the table where you store the number that represents the first four digits of ID.

How do I get the last three digits of a varying length column?

I want to write a select statement and get the last three digits of all of the rows in a column for which the length varies.
Any ideas on how I can achieve this?
Hypothetical column:
12312398098098
127865275
I want the resulting column to have the values:
Resulting column after the script:
098
275
SELECT RIGHT(CONVERT(VARCHAR(4000), [hypothetical column]), 3) FROM table;
(Added a convert in case this is a numeric column.)

SQL Query to separate data into two fields

I have data in one column that I want to separate into two columns. The data is separated by a comma if present. This field can have no data, only one set of data or two sets of data saperated by the comma. Currently I pull the data and save as a comma delimited file then use an FoxPro to load the data into a table then process the data as needed then I re-insert the data back into a different SQL table for my use. I would like to drop the FoxPro portion and have the SQL query saperate the data for me. Below is a sample of what the data looks like.
Store Amount Discount
1 5.95
1 5.95 PO^-479^2
1 5.95 PO^-479^2
2 5.95
2 5.95 PO^-479^2
2 5.95 +CA8A09^-240^4,CORDRC^-239^7
3 5.95
3 5.95 +CA8A09^-240^4,CORDRC^-239^7
3 5.95 +CA8A09^-240^4,CORDRC^-239^7
In the data above I want to sum the data in the amount field to get a gross amount. Then pull out the specific discount amount which is located between the carat characters and sum it to get the total discount amount. Then add the two together and get the total net amount. The query I want to write will separate the discount field as needed, see store 2 line 3 for two discounts being applied, then pull out the value between carat characters.
For SQL Server:
You can use ChardIndex(',',fieldname) in a sql statement to find the location of the comma and then Substring to parse out the first and second field.
For Oracle you can use a case statement like this in your select clause. Use one for each of the two discounts:
CASE WHEN LENGTH(foo.discount) > 0 AND INSTR(foo.discount,',') > 0 THEN
SUBSTR(foo.discount,1,INSTR(foo.discount,',',1,1)) ELSE foo.discount END AS discount_column_1
I finally figured out exactly how to separate the fields as I need them. Below is the code that breaks the discount field into two. I can now separate the fields as needed and insert the data separated into a temp table then use a similar set of code to pull out the exact amount enclosed by the carat characters. Thanks for the help in the two answers above. I used a combination of both to get exactly what I needed.
CASE LEN(X.DISCOUNT)-LEN(REPLACE(X.DISCOUNT,',',''))
WHEN 1 THEN SUBSTRING(X.DISCOUNT,1,CHARINDEX(',',X.DISCOUNT)-1)
ELSE X.DISCOUNT
END 'FIRST_DISCOUNT',
CASE LEN(X.DISCOUNT)-LEN(REPLACE(X.DISCOUNT,',',''))
WHEN 1 THEN SUBSTRING(X.DISCOUNT,CHARINDEX(',',X.DISCOUNT)+1,LEN(X.DISCOUNT)-CHARINDEX(',',X.DISCOUNT)+1)
ELSE ''
END 'SECOND_DISCOUNT'
This alternative solution uses LEFT and RIGHT functions for split the column.
select Store, Amount,
Discount1 = CASE
WHEN CHARINDEX(',',Discount) > 1 THEN LEFT(Discount, CHARINDEX(',',Discount)-1 )
ELSE Discount END,
Discount2 = CASE
WHEN CHARINDEX(',',Discount) > 1 THEN RIGHT(Discount, LEN(Discount) - CHARINDEX(',',Discount)-1 )
END
from #Temp