Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

days into completed weeks only Expand / Collapse
Author
Message
Posted Monday, September 10, 2012 5:43 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, September 12, 2014 4:46 AM
Points: 158, Visits: 504
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
Posted Monday, September 10, 2012 7:14 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:29 PM
Points: 5,618, Visits: 25,218
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
Posted Monday, September 10, 2012 7:36 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, September 12, 2014 4:46 AM
Points: 158, Visits: 504
Thanks, sorted the issue now.

Post #1356729
Posted Monday, September 10, 2012 7:38 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 10:24 AM
Points: 577, Visits: 3,414

i do:
select FLOOR(2.86)

Post #1356731
Posted Monday, September 10, 2012 7:47 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Wednesday, September 3, 2014 12:30 PM
Points: 945, Visits: 1,772
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
Posted Monday, September 10, 2012 8:04 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:12 PM
Points: 23,397, Visits: 32,241
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
Posted Tuesday, September 11, 2012 7:22 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 10:38 AM
Points: 1,945, Visits: 3,004
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
Posted Tuesday, September 11, 2012 7:54 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Friday, August 29, 2014 11:34 PM
Points: 790, Visits: 646
Please check if this can help you!!!
select CAST(20.0/7 AS INT) + CASE WHEN (20%7) > 3 THEN 1 ELSE 0 END


Regards,
Mitesh OSwal
+918698619998
Post #1357426
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse