SQL Server: How to display a specific character based on position in a column - sql

So I'm attempting to display a single character based on its position in a string from one column. Since this is grid data, there is a simple math to it. The grid has 24 rows 'A-X', and 44 columns.
So lets say I want to see the value in D9. I already know the expected value should be a 'A1', so that means the character length is '2'. If I do the math: (A + B + C = 3 x 44, + 9). That two-character value for D9 starts at the 141st position of that string in Col2. I attempted to use SUBSTRING with no success
SELECT
Col1 , SUBSTRING('Col2',141,2)
FROM Table1
Query result displays data in Col1, but for Col2 its just blank. What am I missing?

Asked too soon. Figured out I had to remove the ' from the column name
SELECT
Col1 , SUBSTRING('Col2',141,2)
FROM Table1
Didn't work
SELECT
Col1 , SUBSTRING(Col2,141,2)
FROM Table1
Works

Related

Splitting the string into columns to extract values using BigQuery

How can i split the string by a specific character and extract the value of each. The idea is that i need to extract each word between the line including the start/end of the string as this information represents something. Is there a regex pattern ? or a way to split the info into columns ?
Name
A|B|C|D|E|F|G
Name col1 col2 col3 col4 col5 col6 col7
A|B|C|D|E|F|G A B C D E F G
I am using BigQuery for this and couldn't find a way to get the info of all of those. I tried the regex code which only works for the case where we have A|B|C.
I have to compare each column value and then create conditions using case when
CODE:
select
regexp_extract(name, "\\w+\\S(x|y)") as c2, -- gives either x or y
left(regexp_substr(name, "\\w+\\S\\w+\\S\\w+"),1) as c1,
right(regexp_extract(name, "\\w+\\S\\w+\\S\\w+"),1) as c3
from Table
Consider below approach
select * from (
select *
from your_table, unnest(split(name, '|')) value with offset
)
pivot(any_value(value) as col for offset in (0,1,2,3,4,5,6))
if applied to dummy data as in your question - output is
This seems like a use case for SPLIT().
select split(name,"|")[safe_offset(0)] as c1, split(name,"|")[safe_offset(1)] as c2, ..
from table
see https://cloud.google.com/bigquery/docs/reference/standard-sql/string_functions#split
Added use of safe_offset instead of offset per Array index 74 is out of bounds (overflow) google big query

Change comma to dot for every cell in column

In my table I have column with numbers. These numbers uses a comma as the decimal separator. I would like a dot instead for the comma for every cell in this column.
This is what I have:
Col1 Col2 NumCol
Value 1 Value 2 12,3
Value 3 Value 4 1,23
Value 5 Value 6 99,8
This is what I want:
Col1 Col2 NumCol
Value 1 Value 2 12.3
Value 3 Value 4 1.23
Value 5 Value 6 99.8
I am familiar with the REPLACE-function. However I don't know how to use it when it involves a whole column. How would a function like that look like?
Use replace()
select t.col1, t.col2, t.NumCol, replace(numcol, ',', '.') as NewNumCol
from table t;
EDIT : As side noted by #barthofland if you have large value then only one replace() might fail. So, you need instead :
replace(replace(numcol, '.', ''), ',', '.')

How to check the last two digits?

SUBBIS
SUBB1D
SUBBD3
SUBB12
In above values, how can I check the last two digits (IS, 1D, D3, 12) are numbers using a sql code?
Do you mean to fetch those values? You can do that with like:
where column like '%[0-9][0-9]'
If you need to ensure that the values always end with 2 numbers, you can do it with similar check constraint.
To check the last two digits are numbers in column, you can use the following script.
... WHERE ISNUMERIC(RIGHT(your_column,2)) = 1
Here RIGHT(your_column,2) will return the last two digits from the string.
or
SELECT ISNUMERIC(RIGHT(your_column,2))
will return 1 (if its number) otherwise 0
You can do it this way:
SELECT MyId,
ISNUMERIC(RIGHT(MyColumn,2)) -- your column to check last 2 (if numeric)
FROM (
----- replace with your table
SELECT 1 MyId,'SUBBIS' MyColumn UNION SELECT 2,'SUBB1D' UNION
SELECT 3,'SUBBD3' UNION SELECT 4,'SUBB12'
----- replace with your table
) A
Hope it helps. :)
You can use like and _ "underscore" to get last one digits record columName
SELECT columName FROM sub WHERE columName LIKE "SUBB__" ;
Record :
columName
SUBBIS
SUBB1D
SUBBD3
SUBB12
SUBBBA

postgresql update row to fixed digit number

I am beginner in sql query and I am trying to update my rows like that:
1--->0001
15-->0015
254-->0254
1458-->1458
My column's type is text and there are lots of columns so I cannot handle with
update table1 set col1 = 0001 where col1 = 1;
and so on..
This seems easy question but after research,I could not find a solution. all I need is something like
foreach row in col1
if((int)row>0 and < 10)
then row = "000" + row;
All texts are infact integer value but I have to keep them as text. Whats sql query of above code?
Thanks
You can use the lpad() function:
update table1
set col1 = lpad(col1, 4, '0')
where length(col1) < 4;
But the real question is: why are you storing numbers as text values? That is almost always a bad choice.

Get MAX value if column has a certain format

SQL Server 2008 R2
I have a table similar to this:
Example table:
ID Column
---------------
xxx1234
xxx12345
xxx123456
20150001
I am trying to get a conditional MAX value depending on the value of the column based on whether it meets as certain format. Using the above example, the fourth record, 20150001, represents a "good record" because it contains the current year, and the start of an increment. So, in my table, records that are considered "good" (those subject to the query I am trying to write) have the format "year + increment". The first three, that do not follow this format, should not be conditioned to the query since they don't match this format and should not be subject when computing the max value. Those are bad records. In the above example, the expected result would be "20150002".
The MAX query is simple enough to write, however I am wondering about an approach where I can sanitize the query to only include those records whom meet the particular format, and increment the last four digits (0001 to 0002).
TIA!
You can use the isdate function to filter out ID Columns that do not start with a valid year, and isnumeric to make sure the last 4 characters of the ID Column are valid increments. You also want the len to be 8, given this criteria. You can accomplish all this in the where clause:
-- load test data
declare #Example_Table table(ID_Column varchar(10))
insert into #Example_Table values
('xxx1234'),
('xxx12345'),
('xxx123456'),
('20150001')
-- return max valid ID_Column
select max(ID_Column) as max_ID_Column
from #Example_Table
where isdate(left(ID_Column,4)) = 1
and isnumeric(right(ID_Column,4)) = 1
and len(ID_Column) = 8
-- increment max valid ID_Column
update #Example_Table
set ID_Column = cast(ID_Column as int) + 1
where isdate(left(ID_Column,4)) = 1
and isnumeric(right(ID_Column,4)) = 1
and len(ID_Column) = 8
select * from #Example_Table
ID_Column
----------
xxx1234
xxx12345
xxx123456
20150002
You could use a regular expression to verify a correct year. The second half of the regular expression I taylored to your examples of 0001 and 0002, this could be opened up by adding '[0-9]' for each digit you're expecting.
DECLARE #Sample VARCHAR(30) = '20150001';
SELECT CASE WHEN (#Sample LIKE '[12][09][0-9][0-9]000[12]') THEN 'Yes' ELSE 'No' END;
SELECT
SUBSTRING(#Sample, 1, 4),
SUBSTRING(#Sample, 5, 4),
CASE WHEN (SUBSTRING(#Sample, 1, 4) LIKE '[12][09][0-9]') THEN 'Yes' ELSE 'No' END,
CASE WHEN (SUBSTRING(#Sample, 5, 4) LIKE '[0-9][0-9][0-9][0-9]') THEN 'Yes' ELSE 'No' END;