Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

How to remove "Tab" from column data (SQL)

Problem:
Remove leading or trailing "Tab" from data values
Eg:
ColumnA

val1
val2
val3
val4
val5



So to remove these "tab" from data values we cannot use trim function directly


update testtable set ColumnA=trim(ColumnA);


Solution:
To solve this problem we can slightly modify the trim function and include Replace function also in it by specifying ascii value of "tab" which is 9.


update testtable set ColumnA=trim(replace(ColumnA,Char(9),Char(32)));

How to Apply DISTINCT condition on only one Column

Problem:- Lot of time we need to apply Distinct on only one column, but the DISTINCT keyword doesn't provided by SQL applies distinct to whole row.

Solution:- The following query can solve the problem.


Select * from TestTable T where Testno IN(Select MAX(Testno) from TestTable where T.Testname=Testname)

In this query Testno is the Testname is the column by which you want to distinct the rows, and Testno is the column which is not distinct (for e.g. Primary key)