Change comma to dot for every cell in column - sql

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, '.', ''), ',', '.')

Related

How to split string based on column length and insert into table

I have a string that I need to split and create table from it.
00001 00000009716496000000000331001700000115200000000000
I know the exact length of each column:
Col1 = 5
Col2 = 7
Col3 = 23
etc...
I need something like this (Empty values are NULL's)
Can you direct me to the right way of doing that?
Use substring():
select substring(col, 1, 5) as col1,
substring(col, 6, 2) as col2,
. . .
you can use computed column to improve your performance(visit https://www.sqlservertutorial.net/sql-server-basics/sql-server-computed-columns/)
use below function to fill your column
SUBSTRING(string, start, length)

Trim leading 0 for length greater than x in a dataset

Take this table for example
Table 1:
Name. ID
David 00513
George 0523
Carmen 3216
In this table, I want to trim the leading 0 for David only, because his ID is greater than 4 digits. I don't want to trim the leading 0 for George
Whats the best way to do this in SQL?
The simplest way is simply:
select right(id, 4)
If you are concerned about ids longer than 4 character but with non-zero initial characters:
select (case when length(id) > 4
then replace(ltrim(replace(id, '0', ' ')), ' ', '0')
else id
end)
If you're not concerned with initial non-zeros getting chopped, then
select substr(ID,-4);
should work. If there is possibility of having more than 4 digits with a non-zero initial, then use
select printf('%04d', ID);
(Assuming that all characters in ID are digits)

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

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

SQL - Changing data type of an alphanumeric column

I'm on Teradata. I have an ID column that looks like this:
23
34
W7
007
021
90
GS8
I want to convert the numbers to numeric so the 007 should be 7 and 021 be 21. When a number is stored as a string, I usually do column * 1 to convert to numeric but in this case it gives me a bad character error since there are letters in there.
How would I do this in a select statement within a query?
Assuming that numeric values always start with a number, then something like this should work:
update t
set col = (case when substr(col, 1, 1) between '0' and '9'
then cast(cast(col as int) as varchar(255))
else col
end);
Or, you can forget the conversion and do:
update t
set col = trim(leading '0' from col);
Note: both of these assume that if the first character is a digit then the whole string comprises digits. The second assumes that the values are not all zeroes (or, more specifically, that returns the empty string).
Simply use TO_NUMBER(col) which returns NULL when the cast fails.

SQL - Combine two strings having the second string always right aligned

I'm currently trying to attain alignment via SQL queries when combining two columns.
my current data set looks something like:
Col1 Col2
usd US Dollar
cad Canadian Dollar
mxn Mexican Peso
And I want to combine col1 + col2, but no matter how many characters are in col2, the data that comes out of col1 needs to always be aligned to the right in the display.
The display is limited at 49 characters. Col2 has no specific limit as it's a description column, while col1 is a percentage column so it will have a maximum of 7 characters: 100.00%
Any help will be appreciated.
Thanks
If I understood your question well and assuming that col1 has maximal length of 7 characters while col2 length is undetermined, the following query should give you the results needed:
SELECT
ISNULL(myTable.col1, '')
+ (CASE WHEN LEN(ISNULL(myTable.col2, '')) < 49 - LEN(ISNULL(myTable.col1, ''))
THEN SPACE(49 - LEN(ISNULL(myTable.col1, '')) - LEN(ISNULL(myTable.col2, '')))
+ ISNULL(myTable.col2, '')
ELSE ' ' + LEFT(ISNULL(myTable.col2, ''),
49 - LEN(ISNULL(myTable.col1, '')) - 1)
END) AS cols_for_49chars_display
FROM
myTable
Since col1 is assumed to have max length of 7 characters, the CASE statement verifies the length of col2 for specific row.
If it's lower than 49 - LEN(col1), prepend col2 with 49 - LEN(col1) - LEN(col2) spaces using TSQL SPACE function (docs here) to right-align col2 and then add col2 itself.
In opposite case add one space after col1 and left-cut col2 to the length of 49 - LEN(ISNULL(myTable.col1, '')) - 1 when 1 being the length of the added single space character.
Example input data and results of the query:
Lets take as an example the data provided in your answer adding an extra row to show what will happen when col2 value is too long:
myTable contents:
col1 col2
------------------------------------------------------------------------------------------------
usd US Dollar
cad PCanadian Dollar
mxn Mexican Peso
dummy Very long description of a "dummy" currency that in fact doesn't exist in the real world
The result of the query on above rows would be:
cols_for_49chars_display
-------------------------------------------------
usd US Dollar
cad PCanadian Dollar
mxn Mexican Peso
dummy Very long description of a "dummy" currency
I hope it helps at least slightly.