Showing posts with label books. Show all posts
Showing posts with label books. Show all posts

Tuesday, February 14, 2012

Convert function

In a query, how do I convert a datetime to display as mm/dd/yy hh:mm AMorPM?
I don't see that option on the list of values in Sql Books. The only option
I find for including the time abbreviates the month rather than using
numericals:
Convert(varchar, myDateTime, 9) displays: Jun 17 1998 12:00:00:000AM
where as I want it to display 06/17/98 12:00 AM
Same for using numbers that you want it to always display decimals.
Convert(varchar, myNumber, 1) displays 7.1750000e+002
whereas I want it to display 7.17, or I want 7.014 to display as 7.01 and I
want 7.1 to display as 7.10convert(VarChar(10), myDateTime, 1) + ' ' +
Right(convert(VarChar(26), myDateTime, 9), 14)
And for Numbers, Check out the Str() FUnction Takes 3 Arguments,
Str(val, N, M)
val is expression evaluates to a number (integral or floating)
N Is Number of digits to Display,
M is number of digits to right of decimal point to round off to...
"Tory" wrote:

> In a query, how do I convert a datetime to display as mm/dd/yy hh:mm AMorP
M?
> I don't see that option on the list of values in Sql Books. The only opti
on
> I find for including the time abbreviates the month rather than using
> numericals:
> Convert(varchar, myDateTime, 9) displays: Jun 17 1998 12:00:00:000AM
> where as I want it to display 06/17/98 12:00 AM
> Same for using numbers that you want it to always display decimals.
> Convert(varchar, myNumber, 1) displays 7.1750000e+002
> whereas I want it to display 7.17, or I want 7.014 to display as 7.01 and
I
> want 7.1 to display as 7.10
>
>|||The convert for the date works great! For the number, however, I don't know
the "N" as it may be 3 characters (123) or it may be 5 characters (1234.5).
Also I want the number to always display with 2 digits to the right, for
instance 123 would be returned as 123.00
Thanks.
"CBretana" <cbretana@.areteIndNOSPAM.com> wrote in message
news:F0CE3A77-FF02-40D6-AC58-B186D897B38A@.microsoft.com...
> convert(VarChar(10), myDateTime, 1) + ' ' +
> Right(convert(VarChar(26), myDateTime, 9), 14)
> And for Numbers, Check out the Str() FUnction Takes 3 Arguments,
> Str(val, N, M)
> val is expression evaluates to a number (integral or floating)
> N Is Number of digits to Display,
> M is number of digits to right of decimal point to round off to...
> "Tory" wrote:
>
AMorPM?
option
and I|||The N Just has to be big "enough" If you are outputing an integer, for
example, it can only go to 2 Billion, so N = 9 + hpwever many decimal places
will suffice... Str Fucntion will add extra spaces up to the value of N
Try this:
Select Str(123.5678912345, 12, 4) to see what I mean...
If you don't want the extra spaces, Use Ltrim() Function to strip them off..
Select Str(123.5678912345, 12, 4), LTrim(Str(123.5678912345, 12, 4))
"Tory" wrote:

> The convert for the date works great! For the number, however, I don't kn
ow
> the "N" as it may be 3 characters (123) or it may be 5 characters (1234.5)
.
> Also I want the number to always display with 2 digits to the right, for
> instance 123 would be returned as 123.00
> 8
> Thanks.
>
> "CBretana" <cbretana@.areteIndNOSPAM.com> wrote in message
> news:F0CE3A77-FF02-40D6-AC58-B186D897B38A@.microsoft.com...
> AMorPM?
> option
> and I
>
>|||While you have that "code museum" proprietary CONVERT (), the basic
principle of a tiered architecture is that you do frmatting and display
in the front end and never in the database. Standard SQL has one and
only one date format, based on ISO-8601. Each X3J host language
Standard has conversion rules for SQL data types.
You are still thinking in terms of a 3GL, and not SQL. In a 3GL, the
files and the apps were integrated, monolithic. In a tiered
architecture, each tier does one and only one task.|||Got it! Thanks so much, that works perfectly!
"CBretana" <cbretana@.areteIndNOSPAM.com> wrote in message
news:AA1384B2-F109-44C3-8F32-B7D7A8DDBFAA@.microsoft.com...
> The N Just has to be big "enough" If you are outputing an integer, for
> example, it can only go to 2 Billion, so N = 9 + hpwever many decimal
places
> will suffice... Str Fucntion will add extra spaces up to the value of N
> Try this:
> Select Str(123.5678912345, 12, 4) to see what I mean...
> If you don't want the extra spaces, Use Ltrim() Function to strip them
off..
> Select Str(123.5678912345, 12, 4), LTrim(Str(123.5678912345, 12, 4))
> "Tory" wrote:
>
know
(1234.5).
only
using
12:00:00:000AM
7.01

Sunday, February 12, 2012

Convert decimal to VarChar without trailing 0's?

According to Books online...
"To remove trailing zeros from a result set when you convert from numeric or
decimal data to character data, use the value 128 for style."
So how come this code returns 10.2500000000? And more to the point how do I
get 10.25?
create table #test(
Val Decimal(19,10)
)
Insert into #test( Val ) Values ( 10.25 )
Select
Val,
Convert( VarChar, Val, 128 )
From #test
Any ideas?
Colin.Colin,
Try function STR instead.
select ltrim(str(cast(10.25 as Decimal(19,10)), 8, 2))
AMB
"Colin Dawson" wrote:

> According to Books online...
> "To remove trailing zeros from a result set when you convert from numeric
or
> decimal data to character data, use the value 128 for style."
> So how come this code returns 10.2500000000? And more to the point how do
I
> get 10.25?
> create table #test(
> Val Decimal(19,10)
> )
> Insert into #test( Val ) Values ( 10.25 )
> Select
> Val,
> Convert( VarChar, Val, 128 )
> From #test
>
> Any ideas?
> Colin.
>
>|||That works fine for the specific example, but if I have a number with a
different number of decimal places the results are not correct.
10.125 will get rounded, and this cannot be allowed to happen in the Medical
application that we're developing.
So far, the best solution that I've found is
Replace( RTrim( Replace( Replace( RTrim( Replace( Val, '0', ' ' ) ), ' ',
'0' ), '.', ' ' ) ), ' ', '.' ),
But this is rather CPU intensive. Looks like, I may be starting my own CLR
function library that contains all the stuff that MS missed from SQL2005.
Colin.
"Alejandro Mesa" <AlejandroMesa@.discussions.microsoft.com> wrote in message
news:09C08477-DDA9-40C6-8CAF-208AC263B1F5@.microsoft.com...
> Colin,
> Try function STR instead.
> select ltrim(str(cast(10.25 as Decimal(19,10)), 8, 2))
>
> AMB
> "Colin Dawson" wrote:
>