Tuesday, 25 October 2011

Convert Varchar to Money datatype where data ends with "+" or "-" using CASE statment

select
CONVERT(money, CASE WHEN [Column ] LIKE '%-%' THEN '-' + replace([Column ], '-', '')ELSE [Column ] END) AS TransCurfrom
Test_Table

Remove unused zeros or some other data in front of all Records or from Columns data where the data type is VARCHAR

Example:

create table test ([values] varchar(30))go
insert
into test values ('000001234')insert into test values ('00000000123456')insert into test values ('000123467') insert into test values ('001234787')insert into test values ('00000001234787')insert into test values ('000012343435')go

SELECT
substring([values],PATINDEX('%[^0]%',[values]),10) as [values_modified],'Zeroes is Removed' as Removed_Valuesfrom
test