Showing posts with label calculate. Show all posts
Showing posts with label calculate. Show all posts

Thursday, March 8, 2012

Data casting, casting operations

Does anybody know how could I calculate the new date(adding @.c to @.b
or subtracting @.b to @.c), having for example a declaration like this:
DECLARE @.a CHAR(12)
DECLARE @.b DATETIME
DECLARE @.c INT
SET @.b='3.04.04';
SET @.c=6

and to calculate the number of days between two dates with this kind
of declaration(@.a-@.b or @.b - @.a):
DECLARE @.a CHAR(12)
DECLARE @.b DATETIME
DECLARE @.c INT
SET @.a='12.2.04';
SET @.b='3.04.04';

Thanks in advance.Take a look at the DATEADD and DATEDIFF functions in Books Online.
Don't specify dates in code in the format you have done. Use 'YYYYMMDD'
which will work safely regardless of regional server and connection
settings.

--
David Portas
SQL Server MVP
--

Sunday, February 19, 2012

DADIFF expressed as float

Hello,
I am trying to calculate an age in terms of years and express the result as
a float, such as 3.75 years or 5.33333 years etc. I have tried things
like...
CONVERT(float, DATEDIFF(d, MyStartDate, getdate()) ) /365 as YearsOld
But this gives results as whole numbers like 3.0 and 5.0. What do I need to
change in the syntax? (I realize that dividing by 365 is inaccurate, but it
is close enough for my purposes). Thanks in advance.Sorry for dup - my newsreader gave me an error and it didnt look like the
first one posted.
"Mark Hoffy" <mark@.here.com> wrote in message
news:vnuIe.291$Zo3.240@.fe03.lga...
> Hello,
> I am trying to calculate an age in terms of years and express the result
as
> a float, such as 3.75 years or 5.33333 years etc. I have tried things
> like...
> CONVERT(float, DATEDIFF(d, MyStartDate, getdate()) ) /365 as YearsOld
> But this gives results as whole numbers like 3.0 and 5.0. What do I need
to
> change in the syntax? (I realize that dividing by 365 is inaccurate, but
it
> is close enough for my purposes). Thanks in advance.
>|||Mark,
I can't reproduce the problem you are having:
declare @.t table (
MyStartDate datetime
)
insert into @.t values ('20030102')
select CONVERT(float, DATEDIFF(d, MyStartDate, getdate()) ) /365 as YearsOld
from @.t
Result:
YearsOld
----
2.5890410958904111
Can you possibly post a complete script that demonstrates the problem?
Steve Kass
Drew University
Mark Hoffy wrote:

>Hello,
>I am trying to calculate an age in terms of years and express the result as
>a float, such as 3.75 years or 5.33333 years etc. I have tried things
>like...
>CONVERT(float, DATEDIFF(d, MyStartDate, getdate()) ) /365 as YearsOld
>But this gives results as whole numbers like 3.0 and 5.0. What do I need t
o
>change in the syntax? (I realize that dividing by 365 is inaccurate, but i
t
>is close enough for my purposes). Thanks in advance.
>
>|||See other thread for reply.
SK
Mark Hoffy wrote:

>Sorry for dup - my newsreader gave me an error and it didnt look like the
>first one posted.
>"Mark Hoffy" <mark@.here.com> wrote in message
>news:vnuIe.291$Zo3.240@.fe03.lga...
>
>as
>
>to
>
>it
>
>
>