This question already has answers here:
How do I cast a string to integer and have 0 in case of error in the cast with PostgreSQL?
(14 answers)
Closed 7 years ago.
I have a problem to convert column with text values into integer values.
In table address i have column 'postcode' type TEXT. I have created a new column name 'postcode_int' type integer.
In column 'postcode' some people wrote for example '330...' or '00234'.
How can i check if the value is an integer.And if a value is an Integer then how to convert this values from type TEXT into type Integer and set them in column 'postcode_int' so that later i can do 'between '11111' and '99999'.
i tried to do something like this:
UPDATE adresse
SET postcode_int= case
when pg_typeof( REGEXP_REPLACE( COALESCE(trim( LEADING '0' FROM postcode), '0'),
'[^0-9]*' ,'0')::integer
) = integer
then postcode_int
else 0
end;
With regards
Andrey
I believe the SQL you need is:
UPDATE adresse
SET postcode_int = CAST(postcode AS INT)
This is based on your example.
If this answer is not clear please ask for a revision.
Related
This question already has answers here:
Replace first occurrence of substring in a string in SQL
(4 answers)
Closed 8 months ago.
I have a table with multiple entries in the "Test1" column
These are for example:
123ABCsignal2342
23ABsignal234signal
ABBSDsignal2signal
I want to update the entries like this:
123ABC.signal2342
23AB.signal234signal
ABBSD.signal2signal
The position for inserting the dot (.) is always in a different place and is signed by the expression "signal". Even if "signal" occurs several times, the point should be inserted only once at the beginning.
Declare #strs varchar(50) = '23ABsignal234signal'
SELECT STUFF(#strs
, CHARINDEX('signal', #strs)
, LEN('signal')
, '.signal')
OR
As Suggested Squirrel
Declare #strs varchar(50) = '23ABsignal234signal'
SELECT STUFF(#strs, CHARINDEX('signal', #strs), 0, '.')
This question already has answers here:
MySQL variable format for a "NOT IN" list of values
(3 answers)
Closed 6 years ago.
This value is in a column in a table:
'962091','962092','962093'
I try to use this in a where. First I declare a variable:
DECLARE #KPLnr varchar(100)
SET #KPLnr = CONVERT(nvarchar(max), dbo.UF_GetOption('FastecKPL')) /* here I get the values in */
If I select, I get the correct values of #KPLnr: '962091', '962092','962093', but if I try to use it in a where statement, it seems like the value is set wrong.
I get 0 results, but if I set it manually with:
WHERE c.kpl IN ('962091', '962092','962093')
I got 414 results.
So why is WHERE c.kpl IN ('962091', '962092', '962093') not equal to
WHERE c.kpl IN (#KPLnr) in my code?
When an apostrophe is stored in a text column, you need to escape it by adding an extra apostrophe:
WHERE c.kpl IN ('962091'', ''962092'',''962093')
This question already has answers here:
ORA-12704: character set mismatch
(2 answers)
Closed 8 years ago.
I get the error code ORA-12704 with the following query:
SELECT COALESCE(BankDetails.description,'') as description FROM BankDetails
The datatype of description column nvarchar2. I'm assuming the '' is the cause of the issue as this is not matching with the datatype.
Try this
SELECT COALESCE(BankDetails.description,n'') as description FROM BankDetails
Reference
ORA-12704: character set mismatch
You should use the n variant, to cast the '' to a nvarchar:
SELECT COALESCE(BankDetails.description,n'') as description FROM BankDetails
Or you could use this one:
SELECT COALESCE(BankDetails.description, NULL) as description FROM BankDetails
I have a column Property in a table Order. We used to store a dictionary(varchar) in this column.
We now have a new column (int) called ShoeSize which has a default value of 0.
So what I want to achieve is to retrieve only the numeric value out of the Property column and update the ShoeSize column with that value.
The Property column value looks like this:
ShoeSize<|$à&£#>15<|#ç§~#>
or
ShoeSize<|$à&£#>3<|#ç§~#>
My question is:
How can I manipulate my SQL in select statement to select only the numeric value of the Property column? So in other words, How would I be able to only end up with 15 or 3
Thanks in advance.
If you are using MSSQL and your format is fixed then you can try like below
DECLARE #str VARCHAR(50)
SET #str = 'ShoeSize<|$à&£#>15<|#ç§~#>'
SELECT SUBSTRING(#str,CHARINDEX('£#>',#str) + 3, CHARINDEX('<|#',#str) - (CHARINDEX('£#>',#str) + 3))
You can use REGEXP_SUBSTR to extract the numbers from your string.
For example,
SELECT REGEXP_SUBSTR('ShoeSize<|$à&£#>15<|#ç§~#>','[[:digit:]]+') FROM dual;
will return 15 to you. Of course, my assumption is that the shoe size in your string will be a number, and your intention is not to collate all digits which occur in your string to come at a number which you will save as shoe size
I have a table with a field named MINIMUM_AGE. The values stored in this field are of type nvarchar:
17 years
54 years
N/A
65 years
I would like to apply a WHERE clause on the column to check for a certain age range. To do that I need to parse out the age from the field values.
So, I think I need to select the first two characters, then cast them into an integer. Also, some fields may not contain numbers for the first two characters. Some may simply be N/A. So, I will need to check for that before casting.
Can someone explain how to accomplish this?
Here is the SQL Fiddle that demonstrates the below query:
SELECT CASE
WHEN MINIMUM_AGE <> 'N/A'
THEN CAST(LEFT(MINIMUM_AGE, 2) AS int)
ELSE 0
END
FROM MyTable
Note: the CASE expression can only return one data type. So, in the example above if the MINIMUM_AGE is N/A then it returns 0.
If you would rather have it return null, then use the following:
SELECT CASE
WHEN MINIMUM_AGE <> 'N/A'
THEN CAST(LEFT(MINIMUM_AGE, 2) AS int)
END
FROM MyTable