Showing posts with label datatype. Show all posts
Showing posts with label datatype. Show all posts

Friday, March 30, 2012

How to trim leading zeros from a varchar column?

My table has a column named [Liability] varchar datatype which has the data in the format

(

3535.00,

00393.99,

00Loan,

0.00,

.00

*.00

)

I want to trim the leading zeros so that the output should be(trim only the leading zeros)

(

3535.00,

393.99,

Loan,

0.00,

.00

*.00

)

Can someone show my the sql statement for this?

Thanks.

Code Snippet

--To ignore leading '0' and <SPACE> characters:

select substring(Liability,patindex('%[^0 ]%',Liability),8000)

from <MyTable>

--To ignore ONLY leading '0' characters:

select substring(Liability,patindex('%[^0]%',Liability),8000)

from <MyTable>

|||

rusag2,

I think that your suggested solution will also remove the leading zero from [ 0.00 ] which 'should' be retained.

Perhaps something like this would be closer to the desired output:


Code Snippet

SET NOCOUNT ON

DECLARE @.MyTable table
( RowID int IDENTITY,
MyValue varchar(20)
)

INSERT INTO @.MyTable VALUES ( '3535.00' )
INSERT INTO @.MyTable VALUES ( '00393.99' )
INSERT INTO @.MyTable VALUES ( '00Loan' )
INSERT INTO @.MyTable VALUES ( '0.00' )
INSERT INTO @.MyTable VALUES ( '00.00' )
INSERT INTO @.MyTable VALUES ( '.00' )
INSERT INTO @.MyTable VALUES ( '*.00' )

SELECT

CASE
WHEN isnumeric( MyValue ) = 1 THEN cast( cast( MyValue AS decimal(18,2)) AS varchar(20))
ELSE substring( MyValue, patindex('%[^0]%', MyValue ), 20 )
END
FROM @.MyTable

--

3535.00
393.99
Loan
0.00
0.00
0.00
*.00

|||

Thanks Arnie.

That worked!!!!!

sql

Monday, March 12, 2012

how to suppress zeroes after decimal point at the end in a value

hi,
i have a sql table field price and datatype is
decimal 13(20,6).
when i insert values to this field, values are being
inserted correctly. i.e. 13.45 inserted as 13.45 and
145.653 inserted as 145.653 only.
But while fetching only the values are coming as 13.450000,
145.653000, because the datatype is decimal 13(20,6) with
6 decimals. but i want 13.45, 145.653 as in the table.
How suppress the unwanted zeroes at the end of those
numbers.
any help.
thanks,
hari.
see following example:
drop table test
create table test(c1 decimal (15,5))
insert into test values (3.567000)
insert into test values (232233.567000)
insert into test values (3.567)
query:
select c1,reverse(substring(reverse(cast(c1 as varchar(25))) ,
patindex('%[^0]%', reverse(cast(c1 as varchar(25)))) ,
len(cast(c1 as varchar(25))) - (patindex('%[^0]%', reverse(cast(c1 as
varchar(25)))) - 1)
)) 'no_zeros'
from test
Vishal Parkar
vgparkar@.yahoo.co.in | vgparkar@.hotmail.com
|||hi thanks,
but it is not working for whole number like ex:11120
it is giving it as 11120. (with point at the end)
how to do that.
thanks,
hari.
>--Original Message--
>see following example:
>drop table test
>create table test(c1 decimal (15,5))
>insert into test values (3.567000)
>insert into test values (232233.567000)
>insert into test values (3.567)
>query:
>select c1,reverse(substring(reverse(cast(c1 as varchar
(25))) ,
>patindex('%[^0]%', reverse(cast(c1 as varchar(25)))) ,
>len(cast(c1 as varchar(25))) - (patindex('%[^0]%',
reverse(cast(c1 as
>varchar(25)))) - 1)
>)) 'no_zeros'
>from test
>--
>Vishal Parkar
>vgparkar@.yahoo.co.in | vgparkar@.hotmail.com
>
>.
>
|||try:
select c1, reverse(
case when substring(substring(reverse(cast(c1 as varchar(25)))
,
patindex('%[^0]%', reverse(cast(c1 as varchar(25))))
,
len(cast(c1 as varchar(25))) - (patindex('%[^0]%',
reverse(cast(c1 as varchar(25)))) - 1)) ,1,1) = '.'
then
substring(reverse(cast(c1 as varchar(25))) ,
patindex('%[^0]%', reverse(cast(c1 as
varchar(25)))) + 1 ,
len(cast(c1 as varchar(25))) - (patindex('%[^0]%',
reverse(cast(c1 as varchar(25)))) - 2))
else
substring(reverse(cast(c1 as varchar(25))) ,
patindex('%[^0]%', reverse(cast(c1 as
varchar(25)))) ,
len(cast(c1 as varchar(25))) - (patindex('%[^0]%',
reverse(cast(c1 as varchar(25)))) - 1))
end)
'no_zeros'
from test
Vishal Parkar
vgparkar@.yahoo.co.in | vgparkar@.hotmail.com
"hari" <anonymous@.discussions.microsoft.com> wrote in message
news:207601c4a145$b5262d90$a601280a@.phx.gbl...[vbcol=seagreen]
> hi thanks,
> but it is not working for whole number like ex:11120
> it is giving it as 11120. (with point at the end)
> how to do that.
> thanks,
> hari.
> (25))) ,
> reverse(cast(c1 as
|||See if this works:
select
replace(rtrim(replace(
replace(rtrim(replace(
c1,'0',' ')),' ','0')
,'.',' ')),' ','.')
from test
Steve Kass
Drew University
hari wrote:
[vbcol=seagreen]
>hi thanks,
> but it is not working for whole number like ex:11120
> it is giving it as 11120. (with point at the end)
> how to do that.
>thanks,
>hari.
>
>(25))) ,
>
>reverse(cast(c1 as
>
>

Friday, February 24, 2012

How to store image datatype value to a variable?

I have image type col.
I'm trying to do the following,
DECLARE @.Data varbinary(16)
SET @.Data = (select imageCol from Table1 where id=3)
As image datatype returns varbinary value, so I want to store image col value to a varbinary variable(or any other type variable, eg., varchar). But getting following error,
========================================
Server: Msg 279, Level 16, State 3, Line 2
The text, ntext, and image data types are invalid in this subquery or aggregate expression.
========================================
Is there anyway to store image datatype value to a variable?
Cheers.

Try this:
select @.Data=imageCol from Table1 where id=3

|||

mbanavige wrote:

Try this:
select @.Data=imageCol from Table1 where id=3


Thanks a lot.

how to store a word document in sql server

I Need help on how to store a word document in sql server.what datatype and
how to convert it to store it.
Pleas Help
Thank youAsked and answered in .server -- please refrain from multiposting.
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
--
"Ricardo Le Roux South Africa" <ricardolr@.mweb.co.za> wrote in message
news:eEbLTwOGFHA.3376@.TK2MSFTNGP12.phx.gbl...
> I Need help on how to store a word document in sql server.what datatype
and
> how to convert it to store it.
> Pleas Help
> Thank you
>

how to store a word document in sql server

I Need help on how to store a word document in sql server.what datatype and
how to convert it to store it.
Pleas Help
Thank you
Asked and answered in .server -- please refrain from multiposting.
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
"Ricardo Le Roux South Africa" <ricardolr@.mweb.co.za> wrote in message
news:eEbLTwOGFHA.3376@.TK2MSFTNGP12.phx.gbl...
> I Need help on how to store a word document in sql server.what datatype
and
> how to convert it to store it.
> Pleas Help
> Thank you
>

how to store a word document in sql

I Need help on how to store a word document in sql server.what datatype and
how to convert it to store it.
Pleas Help
Thank you
See if this points you in the right direction:
http://vyaskn.tripod.com/programming_faq.htm#q5
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Ricardo Le Roux South Africa" <ricardolr@.mweb.co.za> wrote in message
news:O6Ja0wOGFHA.208@.TK2MSFTNGP12.phx.gbl...
I Need help on how to store a word document in sql server.what datatype and
how to convert it to store it.
Pleas Help
Thank you

how to store a word document in sql

I Need help on how to store a word document in sql server.what datatype and
how to convert it to store it.
Pleas Help
Thank youSee if this points you in the right direction:
http://vyaskn.tripod.com/programming_faq.htm#q5
--
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Ricardo Le Roux South Africa" <ricardolr@.mweb.co.za> wrote in message
news:O6Ja0wOGFHA.208@.TK2MSFTNGP12.phx.gbl...
I Need help on how to store a word document in sql server.what datatype and
how to convert it to store it.
Pleas Help
Thank you

how to store a word document in sql

I Need help on how to store a word document in sql server.what datatype and
how to convert it to store it.
Pleas Help
Thank you
Use the IMAGE data type to store the document. In general you would need to
store the document in memory and pass a reference to that memory as a
parameter to a stored procedure that deals with inserting or updating the
information. The implementation details on the application side would be
language specific.
Jim
"Ricardo Le Roux South Africa" <ricardolr@.mweb.co.za> wrote in message
news:OxWajwOGFHA.3908@.TK2MSFTNGP12.phx.gbl...
>I Need help on how to store a word document in sql server.what datatype
>and how to convert it to store it.
> Pleas Help
> Thank you
>

how to store a word document in sql

I Need help on how to store a word document in sql server.what datatype and
how to convert it to store it.
Pleas Help
Thank youSee if this points you in the right direction:
http://vyaskn.tripod.com/programming_faq.htm#q5
--
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Ricardo Le Roux South Africa" <ricardolr@.mweb.co.za> wrote in message
news:O6Ja0wOGFHA.208@.TK2MSFTNGP12.phx.gbl...
I Need help on how to store a word document in sql server.what datatype and
how to convert it to store it.
Pleas Help
Thank you