Adding the value in column of SQL Server 2012 - sql-server

I need to add two zero in POS data to make it 14th character numbers because TV UPC has 14th character numbers UPC.
For example
Table 1
POS UPC
------------
123456789012
Table 2
TV UPC
--------------
00123456789012
I have to make it POS UPC 14 character number by adding two zero at the beginning of the number to compare exact match.

Prefix the number with 14 0s and select 14 characters from right which will give you desired output
DECLARE #Number varchar(100)='123456789012'
SELECT RIGHT('00000000000000'+#Number,14)
Output
00123456789012
Replace variable with your column name.
Fiddle here : http://sqlfiddle.com/#!3/9f1d32/1

Related

How to mask some digit using SQL Select

I have a column named Number
Suppose a value is 12345678997
and I want to output as *******8997, that means all leading digits will be masked by * except the last 4 digits
how can I achieve this using SQL Server Select ?
Try this:
declare #i bigint = '12345678997'
select stuff(#i,1,len(#i)-4,'*******')
Output:
*******8997
Using REPLICATE you can generate a string with a given number of the same character.
Then just add the last four digits to that.
select
Number,
concat(replicate('*',len(Number)-4), right(Number,4)) as MaskedNumber
from YourTable

Find values with 2 or more consecutive numeric characters (SQL)

I have a column of values that sometimes have consecutive numbers, how can I get all values that are 2+ consecutive numeric characters? For example:
value
-------
car1339
foo3bar9
there10yes
hellothere
What would the SQL statement be to get the following?
car1339
there10yes
SQL DEMO
SELECT *
FROM Table1
WHERE value LIKE '%[0-9][0-9]%' ;

SQL Server - How to get last numeric value in the given string

I am trying to get last numeric part in the given string.
For Example, below are the given strings and the result should be last numeric part only
SB124197 --> 124197
287276ACBX92 --> 92
R009321743-16 --> 16
How to achieve this functionality. Please help.
Try this:
select right(#str, patindex('%[^0-9]%',reverse(#str)) - 1)
Explanation:
Using PATINDEX with '%[^0-9]%' as a search pattern you get the starting position of the first occurrence of a character that is not a number.
Using REVERSE you get the position of the first non numeric character starting from the back of the string.
Edit:
To handle the case of strings not containing non numeric characters you can use:
select case
when patindex(#str, '%[^0-9]%') = 0 then #str
else right(#str, patindex('%[^0-9]%',reverse(#str)) - 1)
end
If your data always contains at least one non-numeric character then you can use the first query, otherwise use the second one.
Actual query:
So, if your table is something like this:
mycol
--------------
SB124197
287276ACBX92
R009321743-16
123456
then you can use the following query (works in SQL Server 2012+):
select iif(x.i = 0, mycol, right(mycol, x.i - 1))
from mytable
cross apply (select patindex('%[^0-9]%', reverse(mycol) )) as x(i)
Output:
mynum
------
124197
92
16
123456
Demo here
Here is one way using Patindex
SELECT RIGHT(strg, COALESCE(NULLIF(Patindex('%[^0-9]%', Reverse(strg)), 0) - 1, Len(strg)))
FROM (VALUES ('SB124197'),
('287276ACBX92'),
('R009321743-16')) tc (strg)
After reversing the string, we are finding the position of first non numeric character and extracting the data from that position till the end..
Result :
-----
124197
92
16

SQL Server - Split string on last occuring number

I have following column (with fictional data):
Location
---------------
15630London
45680Edinburg
138739South Wales
This column contains both Zipcodes and City names. I want to split those 2 into 2 seperate columns.
So in this case, my output would be:
Zip | City
-------|---------
15630 | London
45680 | Edinburg
138739 | South Wales
I tried the zipcode with
LEFT(location,LEN(location)-CHARINDEX('L',location))
But I couldn't find out how to set the CHARINDEX to work on all letters.
Any suggestions / other ideas?
Here is one way using PATINDEX and some string functions
SELECT LEFT(Location, Patindex('%[a-z]%', Location) - 1),
Substring(Location, Patindex('%[a-z]%', Location), Len(Location))
FROM (VALUES ('15630London'),
('45680Edinburg'),
('138739South Wales'))tc(Location)
Note : Above code considers always zip codes are numbers and characters start only with city name and city is present in all the rows (ie) strings are present in every row
Detect the first non-numeric character and pull of that many chars from the left, then read beyond that point to the end:
select
left(Location, patindex('%[^0-9]%', Location) - 1),
substring(Location, patindex('%[^0-9]%', Location), len(Location))
from t
declare #string varchar(200)='15630London'
select substring(#string,1,patindex('%[a-z,A-Z]%',#string)-1),
substring(#string,patindex('%[a-z,A-Z]%',#string),len(#string))

SQL Server SELECT query ordering by substring

I have a column in a SQL Server table that has the following rows:
MyColumn : C1_xxx1,C2_xxx1,C3_xxx1,C1_xxx2,C1_xxx3,C3_xxx2 etc
It is a text column that contains strings that have the following format: CY_mystring where Y is a number from 1 to 5, followed by the '_' character then mystring that can have any value.
Is there a way to make a select return this column ordered as following:
C1_xxx1
C1_xxx2
C1_xxx3
......
C1_xxxn
C2_xxx1
......
C2_xxxn
C3_xxx1
.......
C3_xxxn
etc
Ordered by the CY_ substring.
thank you
This should do it .. (order first by the first two chars, and then by the last char (assuming that the final n is always one digit long))
SELECT
Column1
FROM
TABLENAME
ORDER BY
LEFT(Column1,2) ASC,
RIGHT(Column1,1) ASC
You say that Y is a number from 1 to 5 it's always one character long. Assuming the format is xY_xxxZ, you can order on Y then Z like:
order by
substring(MyColumn,2,1) -- Second digit
, right(MyColumn,1) -- Last digit
If Z can be longer than one character (i.e. 10 or higher) you can use pathindex to determine the number of digits at the end:
order by
substring(MyColumn,2,1) -- Second digit
, right(MyColumn, patindex('%[^0-9]%', reverse(MyColumn))-1) -- Digits at end

Resources