I am trying to write a query in SQL and I want to check the length of data in a column. Is there any inbuilt function for that?
you can use LEN() or LENGTH() inbuilt function as a example
SELECT LENGTH(column_name) FROM table_name
Try this one & tell if it's working or not?
Related
I need to display records from a column whose has only ".Jpg" Extension
How can I do so is sql query.
Any help would be helpful
Hello you can make use of wildcard operations in SQL using like clause. % in below represents the name of the file and will pull all records with .jpg extension.
select * from table_name where column_name like '%.jpg'
You can use function CHARINDEX().
The CHARINDEX() function searches for a substring in a string, and returns the position.
If the substring is not found, this function returns 0. It's more better than LIKE especially when you have spaces and other symbols in your column.
SELECT * FROM TABLE WHERE
CHARINDEX('.jpg', column-name) >0
the following function deletes all blanks in a text or varchar column and returns the modified text/varchar as an int:
select condense_and_change_to_int(number_as_text_column) from mytable;
This exact query does work.
Though my goal is to apply this function to all rows of a column in order to consistently change its values. How would I do this? Is it possible with the UPDATE-clause, or do i need to do this within a function itself? I tried the following:
UPDATE mytable
SET column_to_be_modiefied = condense_and_change_to_int(column_to_be_modiefied);
Basically i wanted to input the value of the current row, modify it and save it to the column permanantly.
I'd welcome all ideas regarding how to solve scenarios like these. I'm working with postgresql (but welcome also more general solutions).
Is it possible with an update? Well, yes and sort-of.
From your description, the input to the function is a string of some sort. The output is a number. In general, numbers should be assigned to columns with a number type. The assumption is that the column in question is a number.
However, your update should work. The result will be a string representation of the number.
After running the update, you can change the column type, with something like:
alter table mytable alter column column_to_be_modiefied int;
I googled this but couldn't find an easy answer:
I need to write a function that takes a substring as parameter and return those rows which have this substring in a certain column:
select * from myTable where myTable.someColumn contains #substring
appreciate your help
thank you
you may use
where myTable.someColumn like '%?%'
with '#substring' as parameter.
When I look at a view/table, I can get the column names using the following SQL query:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='table'
Obviously, this will work to show columns for tables; but it does not work for functions. Is there an easy way (besides getting one record from a function, and manually parsing it) to get the column names returned by a function?
Does this help?
SELECT *
FROM INFORMATION_SCHEMA.ROUTINE_COLUMNS
I'm unsure if this works for inline TVFs
I have a set of json format stored in a column and now i need to replace a particular word. How to use the replace query. Every time i use it, i'm getting token exception. please advise. Its a DB2 i'm using
I have 3 columns
Name Age Data
ABD 15 [{"Name":"ABC","type":"Regular","Math":18}]
In the Data column, I need to do a replace for "type", It should be StudentType.
REPLACE(Data,'type','StudentType');
This did not work. How to do it?
Thanks much in advance
Just like #mustaccio pointed out, if you use REPLACE in select statement it will just return your data with 'StudentType' instead of 'type'. This does not actually change data in your database. If you want to update your data you need UPDATE statement
UPDATE MyTable
SET MyColumn = REPLACE(MyColumn,'OldString','NewString')