Transact SQL :: Get Week Number From DateTime Field?

Aug 21, 2015

Any easier way to do what I am trying, without having a table with all the dates and week numbers.

Scenario: Week number of a DateTime Field where the year does not start on January 01<sup>st</sup> but April 1<sup>st</sup> to 31 Mach.

Issue: A week always starts on a Monday so if the 1<sup>st</sup> is on Tuesday, the first week is Tuesday – Sunday, if the 1<sup>st</sup> April is on Friday, the 1<sup>st</sup> week is Friday – Sunday and 7 day periods from there.

CREATE
TABLE [dbo].[DailyCanx](     
[ID] [int] IDENTITY(1,1)
NOT NULL,     
[DateCancelled] [datetime] NULL
)
ON [PRIMARY]

[code]....

I could create a table with all the start date and end dates of all the week numbers but I think there must be a better way. using SQL Server 2008 R2.

View 5 Replies


ADVERTISEMENT

How To Get Starting Datetime(monday) Of The Week And Ending Datetime Of The Week(sunday)

May 2, 2007

hi friends,



how to get the date of the first/last day of the week in sql...

can any one help me





Cheers,

raj

View 5 Replies View Related

Transact SQL :: Select All Days In Week Number X Including Last Year If Necessary

May 24, 2015

SQL express 2012. I am trying to case in the where part and having a syntax errors - This is what i am trying to do:

select all the days in week number x including last year if necessary... so if the year start not at the beginning of the week then look in last year as well ( for the same week number of this year and last week nu of last year)

declare
@yyyy int = 2014,-- THE YEAR
@mm int = 1,-- THE MONTH
@week1No int = 1,-- THE WEEK NUMBER IN THE YEAR
@week2No int = 37-- THE last WEEK NUMBER IN last YEAR
select count(tblDay.start)-- tblDay.start IS smallDatetime

[Code] ....

View 2 Replies View Related

Transact SQL :: Select All The Days In Week Number X Including Last Year If Necessary

May 24, 2015

SQL express 2012

I am trying to case in the where part and having a syntax errors - this is what i am trying to do:

Select all the days in week number x including last year if necessary... so if the year start not at the beginning of the week then look in last year as well ( for the same week number of this year and last week nu of last year)

declare
@yyyy int = 2014,-- THE YEAR
@mm int = 1,-- THE MONTH
@week1No int = 1,-- THE WEEK NUMBER IN THE YEAR
@week2No int = 37-- THE last WEEK NUMBER IN last YEAR
select count(tblDay.start)-- tblDay.start IS smallDatetime

[Code] ...

View 2 Replies View Related

SQL Server 2008 :: Get Week Ending Date Given Week Number

Apr 9, 2013

How can I get Saturday's date given the week number?

This is my week number, SELECT DATEPART(WEEK, DATEADD(MONTH, +3, ApptDt2)). I need to get Saturday's date from this week.

View 9 Replies View Related

Convert Week Number To Date Of 1st Day Of That Week...

Feb 2, 2008



I'm looking for a way to convert the [week number, year] back to the date for the 1st day of that week.

Example (assuming DATEFIRST has been set to 1) : Week 3, 2008 = January 14, 2008

Is there a SQL function that will do this?


Thanks,
Mike

View 11 Replies View Related

Transact SQL :: Start Week Of The Month For A Given Week

Jul 23, 2015

I need a Select sentence that return me the first week of the month for a given week.

For example If I have week number 12 (Begins 2015/03/16 and Ends 2015/03/22) I need that returns 9, I mean Week number 9 wich is the first week of march (having in mind @@DATEFIRST).

I only need give a week number of the year and then returns the week number of the first week of that month.

View 34 Replies View Related

Transact SQL :: Query DateTime Field By Date Range

Dec 2, 2015

I have a table of errors with a DateTime field for when the error occurred.  I want to query the table for a given date range omitting the time portion.  What is the most efficient way to perform this query?

View 5 Replies View Related

Transact SQL :: Convert Int To Time And Concatenate It With Datetime Field?

Sep 16, 2015

I have a datetime field for e.g 2015-09-15 00:00:00.000 and an integer field for e.g.100809.

The integer field is actually a time value as in hours,minutes,seconds. For e.g. the value

100809 means
10-hours
08-minutes
09-seconds

I need to combine the datetime and the int field to get the output as below

2015-09-15 10:08:09

View 7 Replies View Related

Transact SQL :: Divide A Number By Field And Date

Jul 7, 2015

I have this rows as result of my query:

aaaaa     bbbbb       date        result
-------     -------      --------    ---------
a            100          7-7-15
a            100          7-7-15
a            100          8-7-15
b            200          7-7-15
b            200          8-7-15
b            200          8-7-15

I need this (divide bbbbb by row count as result, group by "aaaaa and date":

aaaaa     bbbbb       date        result
-------     -------      --------    ---------
a            100          7-7-15       50
a            100          7-7-15       50
a            100          8-7-15      100
b            200          7-7-15      200
b            200          8-7-15      100
b            200          8-7-15      100

Can I do it?

My SQL Code is

(date=FEC, RESULT=COSTECUB, aaaaaa=TRACTA, bbbbb=TRAPR3)  :
SELECT VTRA00F.TRACTA, ACRDES, TRAPRL, TRAPR1, TRAPR2, TRAPR3,
CASE WHEN TRAPR3 IS NULL THEN
(CASE WHEN (ALLQDE+ALLQCN+ALLQCR+ALLQMR+ALLQNN+ALLQNR+ALLQV1+ALLQV2) <= TRAPRL THEN (TRAPR1*(ALLQDE+ALLQCN+ALLQCR+ALLQMR+ALLQNN+ALLQNR+ALLQV1+ALLQV2)) ELSE (TRAPR2*

[Code] .....

View 8 Replies View Related

Transact SQL :: Update Time Portion Of DateTime Field With Time Parameter

Oct 3, 2015

I hope to update a DateTime column value with a Time input parameter.  Poor attempt below but it looks like the @ApptTime param is coming in as 10:45:00.0000000 and I might have an existing @SendOnDate as: 2015-10-05 07:00:00.000...I hope to end up with 2015-10-05 10:45:00.000

ALTER PROCEDURE [dbo].[SendEditUPDATE]
@QuePoolID int=null
,@ApptTime time(7)
,@SendOnDate datetime

[code]...

View 14 Replies View Related

Week Number

Jan 23, 2008

Hi all!

I'm working on a report that will look like this.

WeekNb - Item - QtySold
1 - Item1 - 25
1 - Item2 - 6
2 - Item1 - 10
2 - Item3 - 73
etc...

I use this function to get the week number DATEPART(wk, transactionDate)

But now I have a problem,

Lets says I want the sells for 2008.

The 1st of January is a Tuesday, but I also want the sells that I made on the 31st of December 2007 as it's on the 2008 1st week (monday).

How can I get this to work ?

Help please

Regards,

Or Tho

View 5 Replies View Related

Week Number Conversion

Aug 29, 2002

I am working with Week Numbers and was wondering if there is a way to display Week 1 as 01.

The problem that I am having is that when I list the weeks from Week 1 to Week 12 is that the sorting is 1, 10, 11, 12, 2, 3 and I would like to get it to look like 01, 02, 03....09, 10.


Thanks......

View 1 Replies View Related

How Can I Get The Date From Week Number

Jan 17, 2008



I have week number and i want to get the date from that
for example Week=52 will be the first day of that day which is monday 12/24/2007


Thanks

View 9 Replies View Related

Getting The Date From Week Number

Oct 7, 2007

I have a column in my dataset that contains the week number.

I would like to use 'Expression' Or code function to do that
how can i get the date from week number? for example the week number is 38

so i wil get this output: 09/17/07 - 09/23/07 (the week starts on Monday and ends on Sunday)
Thanks

View 10 Replies View Related

Week Number Calcluation

Aug 2, 2006

Hi Chaps,



I want to calculate the week number by the following rule

"For the week that has the 1st January included if any days are not in January then set the next week as week number 1"

Can any tell me the piece of code



thanx

Anas

View 6 Replies View Related

How To Determine Day Of Week From Datetime Data Type

Feb 16, 2001

Our reporting group would like to generate reports based on day of the week. Is there a function or other means of determining the day based on data in a datetime field? Thanks for any assistance in advance.

Brad

View 2 Replies View Related

DateTime :: To_CHAR :: Change Week Start

Dec 28, 2007

So I need to select and average a whole bunch of data by week. Currently, I group by:


GROUP BY TO_CHAR(m.ENDTIME, 'IW')


and select using


SELECT ... TO_CHAR(m.ENDTIME, 'IW') Week

to summarize by week.

Since the first day of the year was a monday, it sets the weeks as monday-monday. I need to set the weeks to be Thursday 7pm to Thursday 7pm. How can this be achieved? Thanks.

-steve

View 6 Replies View Related

Get Date From Week Number In A Year

Feb 11, 2014

Can I get a date from a week number in a certain year, like I know it's week 7 in 2014 (I may know the month.)

View 7 Replies View Related

Convert Date To Week Number

Apr 30, 2015

I want to convert date to week consider saturday is first day of week and friday is end of week. for input is orderdate and output should be week. example orderdate 11.04.15 to 17.04.15 week should be 16. orderdate 18.04.15 week should change to 17.

Order DateWeek
11-Apr-201516
12-Apr-201516
13-Apr-201516
14-Apr-201516
15-Apr-201516
16-Apr-201516
17-Apr-201516

Order DateWeek
18-Apr-201517

View 3 Replies View Related

Convert Week Number Back To First Date

Feb 28, 2008

hi guys,
How could I convert 'week 9 2008' back into the first day of that week ?

is that even possible ?

View 5 Replies View Related

How To Get The Week Number Of A Specific Date In SQL Server 2005

Jan 10, 2008

Hi,


Can anybody tell me to How to get the week number of a specific Date in SQL Server 2005.

Like : say Date "03/01/2008" belongs to the 1st week of the year 2008, same way "21/02/2008" is 8th week of the year 2008.

So, the same way I need to know the week number of a given (variable) date.


I need to write / use a Function in SQL server which will return the Number (INT) (i.e. 1 or 8 or 10 etc...) against a specific (Parameter) date.

regards,

Saibal


Note : Please feel free to write me if my question is not clear.

View 5 Replies View Related

DateTime Unable To Save In Datetime Field Of SQL Database

Mar 14, 2007

 Hi all, having a little problem with saving dates to sql databaseI've got the CreatedOn field in the table set to datetime type, but every time i try and run it i get an error kicked up  Error "The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.The statement has been terminated."I've tried researching it but not been able to find something similar.  Heres the code: DateTime createOn = DateTime.Now;string sSQLStatement = "INSERT INTO Index (Name, Description, Creator,CreatedOn) values ('" + name + "','" + description + "','" + userName + "','" + createOn + "')"; Any help would be much appreciated 

View 4 Replies View Related

How To Add Date Field And Time Field (not Datetime Field )

May 4, 2006

Good morning...

I begin with SQL, I would like to add a field that will be date like 21/01/2000.

Actually i find just "datetime" format but give me the format 21/01/2000 01:01:20.

How to do for having date and time in two different field.

Sorry for my english....

Cordially

A newbie

View 3 Replies View Related

Transact SQL :: How To Generate Week Ranges From Monday To Sunday

May 28, 2015

I need to generate the week ranges like this format :

Here from date and to date would be picked up from the table but just to make you understand i have hard coded it but this is the real date which is falling inside the table. 

Note : Week should be generated from Monday to Sunday within desired date range

View 6 Replies View Related

Transact SQL :: How To Generate Week Ranges For A Year In 2005

Mar 22, 2011

I have to insert YEAR   WEEKNUMBER   STARTDATE   ENDDATE values to a datatable (sayweekrange), if I pass 2011 as year.

Week range starts at 2011-03-28 to 2011-04-03 (because in my database 2010 last week range ends with 2011-03-27) like this I have to generate for 52 weeks.

I want to write a stored procedure, that takes only year as parameter. with this year I have to generate week ranges and insert into my table as shown above.

How can I do this ?

View 8 Replies View Related

Transact SQL :: Date Calculation - 7 Days Prior To Specified Day Of Last Week

Sep 8, 2015

I need to run a select on Mondays to pull data for 7 days prior to the Thursday of last week; i.e. Friday - Thursday inclusive.  I'm sure this is simple, but I work with dates so infrequently that I need a refressher. 

View 7 Replies View Related

How To Convert Datetime Field To A Date Field So Excel Recognize It As Data Type

May 17, 2015

I embedded a SQL query in excel that gets some datetime fields like "TASK_FINISH_DATE" .

How can I convert a datetime field to a date field in SQL in a way that excel will recognize it as a date type and not a text type?

I tried:
CONVERT(varchar(8),TASK_FINISH_DATE ,3)
CONVERT(Date,TASK_FINISH_DATE ,3)
CAST(TASK_FINISH_DATE as date)

**all of the above returned text objectes in excel and not date objects.

View 3 Replies View Related

Transact SQL :: Check Result Of In Between Two Different Times In Week And Particular Time Period

Oct 16, 2015

I have a query to check the records of job has received after 4pm Monday to Friday and it has completed before 9am next day and also weekend it should be Friday after 4pm and before Monday 10am for particular financial year period. I have my job table which has full date but it doesn't show the date exactly Monday to Friday it shows only as 12-12-2014 like that.

View 2 Replies View Related

Informix Date Type Field To SQL Server Datetime Field Error

Oct 17, 2007



I am trying to drag data from Informix to Sql Server. When I kick off the package
using an OLE DB Source and a SQL Server Destination, I get DT_DBDATE to DT_DBTIMESTAMP
errors on two fields from Informix which are date data ....no timestamp part

I tried a couple of things:

Created a view of the Informix table where I cast the date fields as datetime year to fraction(5), which failed.

Altered the view to convert the date fields to char(10) with the hopes that SQL Server would implicitly cast them
as datetime but it failed.

What options do I have that will work?

View 1 Replies View Related

Transact SQL :: How To Select A Number Less Or Equal Than A Supplied Number

Jun 23, 2015

Got this query and I need the following result;

declare @NumberToCompareTo int
set @NumberToCompareTo = 8
declare @table table
(
number int
)
insert into @table 
select 4

[Code] ....

The query selects 4 and 5 of course. Now what I'm looking for is to retrieve the number less or equal to @NumberToCompareTo, I mean the most immediate less number than the parameter. So in this case 5

View 4 Replies View Related

Converting Datetimeoffset Field To Datetime Field / Why Milliseconds Value Is Incorrect

Nov 17, 2012

DECLARE @datetimeoffset datetimeoffset(3)
DECLARE @datetime datetime
SELECT @datetimeoffset = '2012-11-08T17:22:13.575+00:00'
SELECT @datetime = @datetimeoffset
SELECT @datetimeoffset AS '@datetimeoffset ', @datetime AS 'datetime'
__________________________________________________ ___________
Result of above SQL is
@datetimeoffset datetime
2012-11-08 17:22:13.575 +00:002012-11-08 17:22:13.577
__________________________________________________ ____________

The result should be '2012-11-08 17:22:13.575', why the milliseconds value is incorrect

View 2 Replies View Related

Transact SQL :: Return Field When A Field Contains Text From Another Field

Aug 25, 2015

I'm new to SQL and I'm trying to write a statement to satisfy the following:

If [Field1] contains text from [Field2] then return [Field3] as [Field4].

I had two tables where there were no matching keys. I did a cross apply and am now trying to parse out the description to build the key.

View 8 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved