Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
T-SQL (SS2K8)
»
days into completed weeks only
days into completed weeks only
Rate Topic
Display Mode
Topic Options
Author
Message
LoosinMaMind
LoosinMaMind
Posted Monday, September 10, 2012 5:43 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 9:36 AM
Points: 140,
Visits: 424
Hi
how do i easilty convert days into weeks (obviously Days/7 will give me weeks as decimal)
Problem i have is I only want to count completed weeks:
ie: 20 days is 2.86 weeks.
need answer to be 2 and not 3 as i get in SQL.
Thanks
Post #1356671
bitbucket-25253
bitbucket-25253
Posted Monday, September 10, 2012 7:14 AM
SSCertifiable
Group: General Forum Members
Last Login: Yesterday @ 8:55 AM
Points: 5,099,
Visits: 20,190
Is this what you need to do ?
SELECT 2.86 /1 - 2.86 % 1
SELECT 2.16 /1 - 2.16 % 1
Result for both selects is 2.000000
Note that the "%" is the modulo operator - check it out in BOL it is a powerful tool to become familiar with.
If everything seems to be going well, you have obviously overlooked something.
Ron
Please help us, help you -before posting a question please
read
Before posting a performance problem please
read
Post #1356711
LoosinMaMind
LoosinMaMind
Posted Monday, September 10, 2012 7:36 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 9:36 AM
Points: 140,
Visits: 424
Thanks, sorted the issue now.
Post #1356729
davidandrews13
davidandrews13
Posted Monday, September 10, 2012 7:38 AM
SSC-Addicted
Group: General Forum Members
Last Login: Yesterday @ 10:13 AM
Points: 455,
Visits: 2,662
i do:
select FLOOR(2.86)
Post #1356731
CapnHector
CapnHector
Posted Monday, September 10, 2012 7:47 AM
SSC Eights!
Group: General Forum Members
Last Login: Yesterday @ 10:07 AM
Points: 935,
Visits: 1,709
LoosinMaMind (9/10/2012)
Hi
how do i easilty convert days into weeks (obviously Days/7 will give me weeks as decimal)
Problem i have is I only want to count completed weeks:
ie: 20 days is 2.86 weeks.
need answer to be 2 and not 3 as i get in SQL.
Thanks
watch your data types. using integers 20/7 = 2 since SQL Server throws away the remainder when performing integer math. if you want to be absolutely certain / more precise / more explicit, i would use float or decimal with FLOOR().
For faster help in answering any problems Please read
How to post data/code on a forum to get the best help - Jeff Moden
for the best way to ask your question.
For performance Issues see how we like them posted here:
How to Post Performance Problems - Gail Shaw
Need to Split some strings?
Jeff Moden's DelimitedSplit8K
Jeff Moden's
Cross tab and Pivots Part 1
Jeff Moden's
Cross tab and Pivots Part 2
Jeremy Oursler
Post #1356739
Lynn Pettis
Lynn Pettis
Posted Monday, September 10, 2012 8:04 AM
SSC-Insane
Group: General Forum Members
Last Login: Today @ 2:36 AM
Points: 21,588,
Visits: 27,374
LoosinMaMind (9/10/2012)
Thanks, sorted the issue now.
Proper forum etiquette would have you share your solution with others. It may help someone with a similar issue.
Lynn Pettis
For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here
or
when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here
and
here
Managing Transaction Logs
SQL Musings from the Desert
Fountain Valley SQL
(My Mirror Blog)
Post #1356755
CELKO
CELKO
Posted Tuesday, September 11, 2012 7:22 AM
SSCommitted
Group: General Forum Members
Last Login: Tuesday, January 15, 2013 11:11 AM
Points: 1,945,
Visits: 2,782
Build a calendar table with one column for the calendar data and other columns to show whatever your business needs in the way of temporal information. Since SQL is a database language, we prefer to do look ups and not calculations. I would build a calendar table with a ISO-8601 weekday-within-year format column in addition to the usual Common Era date. This format is 'yyyyWww-d' where yyyy is the year, W is as separator token, ww is (01-53) week number and d is (1-7) day of the week.
Put a computed column for the week:
week_within_year COMPUTED AS SUBSTRING (weekday_within_year, 1, 7)
Now the query is a simple group by:
SELECT ..
FROM Calendar AS C, Foobar AS F
WHERE F.foo_date = C.cal_date
GROUP BY C.week_within_year
HAVING COUNT(*) = 7;
There are several websites with calendars you can cut & paste, but you can start your search with: http://www.calendar-365.com/week-number.html
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1357388
Mitesh Oswal
Mitesh Oswal
Posted Tuesday, September 11, 2012 7:54 AM
Mr or Mrs. 500
Group: General Forum Members
Last Login: Friday, February 15, 2013 1:56 AM
Points: 593,
Visits: 371
Please check if this can help you!!!
select CAST(20.0/7 AS INT) + CASE WHEN (20%7) > 3 THEN 1 ELSE 0 END
Post #1357426
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.