Selecting week / ISO week number from a date/time field - sql

Sorry - this may be a basic question, but I have been banging my head against this for a week.
I have a database field with the format "dd/mm/yyyy hh:mm:ss" called UpdateTime and referencing max(AuditHistory.ActionedDateTime) in the database.
I am trying to identify the Week / ISO Week from the date part of this field only using the dataset in ReportBuilder3.
I am trying to achieve an integer entry in a column called "WeekNo" giving me the week of the year that a transaction was made so I can use this for grouping results by year | by week number on a report for senior management.
I have tried many combinations of:
,DATEPART(WEEK,DAY(max(AuditHistory.ActionedDateTime)) AS WeekNo and
,DATEPART(WEEK,MONTH(max(AuditHistory.ActionedDateTime)) AS WeekNo.
If I use a static date, e.g. , DATEPART(WEEK,DAY('1900-01-20')) AS WeekNo, it returns perfectly as "4" but I cannot for the life of me get the datepart format correct to identify the week from the format of the field.
I believe my issue is getting SQL to accept that the field is "dd/mm/yyyy hh:mm:ss" and work out the week from the date element.
Before I go mad - I thought I'd ask if there is a quick way to achieve this.

The DATEPART function expects a date / datetime / datetime2 value. You are passing in an integer representing the day or month number.
Assuming you're storing your dates correctly, you just need to pass in the date value directly:
DATEPART(WEEK, Max(AuditHistory.ActionedDateTime)) As WeekNo

Related

how to change date format to just year and month but must stay as a date value not varchar, or char..?

I read other similar question where the answer is to use FORMAT(getdate(),'yyy-MM') or something similar. However the problem for me in using anything like this, is that it changes the date type to a varchar or char. I need it to stay as datetype but just want Year and Month.. I tried the following..-> FORMAT(a.completeddate,'yyy-MM') which works to change to year and month but the date is no longer a datetype or date format. So when I try to do the following -> select #FirstCompletion = (A.completeddate) i get this error..Conversion failed when converting date and/or time from character string. Basically I need to convert the date column to year and month as date format so I can then pass values to variables using select #FirstCompletion = (A.completeddate) and set #secondMonth = DATEADD(month, 2, #FirstCompletion) which are Datetype variables.. Would appreciate any help I can get.. Thanks..

Extract year from timestamp in hive

I am writing the query to show the data entries for a specific year. Date is stored in dd/mm/yyyy hh:mm:ss.(Date TIMESTAMP - e.g. 12/2/2014 0:00:00).
I am trying to display the two columns(name, orderdate) filtered by a specific year(year from orderdate). The requirement is to enter the specific year(2010 or 2020 etc) not the entire date. I tried using date_format() and regexp_replace() with WHERE but nothing helped.
Can someone help me?
If your are storing the date -- incorrectly -- as a string, then you can use string functions to do what you want:
where orderdate like '__/__/2010%'
However, you should really put your effort into storing the date using a correct format -- YYYY-MM-DD for strings at least.

Want week number from different columns of Year, Month & Day

I want week number from different columns of Year, Month & Day in same table of Hive QL.
IF you can give logic on SQL, that's also fine.
Concatenate the date, month and year into a proper date format and apply weekofyear().
Select weekofyear(cast(concat(year,"-",month,"-",date) as date)) from tablename.
Please note that I have used cast to convert the concatenated string into date.Howver, you might need to use different method based on your date format. Please refer to the below answer on handling string conversion to date formats.
Hive cast string to date dd-MM-yyyy

Concatenated string to date conversion

I am doing something like this,
i have two columns month and year and i want to create a new column which will be in mm/dd/yyy fromat. so in my case mm is month column , yyyy is year column and date is default 01.
update [tablename]
set period= convert(date,month+'/01/'+year)
now i want this period to be in date data type, i am doing:
alter table [tablename]
alter column period date
but it is giving me error that string can't be converted to date.
If anyone could help that will be very helpful.
You have bad data in your table. Since you didn't use the right data type in the first place, you could have anything at all in the month/year columns, since there is no built in validation that month is 1-12 and year is in the valid range of values. For example, someone could have put 99 in month and -213 in year.
Do this:
SELECT period FROM dbo.tablename WHERE ISDATE([month] + '/01/' + [year]) = 0;
This should identify the rows you need to fix. If this returns the whole table, or returns values that you think should be dates, then show a few examples. The syntax in your update doesn't look right (at the very least missing a +) so I'm not sure what you actually ran against your table, but that's not it. Also a much safer format would be:
SET period = [year] + [month] + '01';
This is because what you are using now, mm/dd/yyyy, is not safe from regional, language and dateformat settings. 06/05/2013 could be interpreted as May 6th instead of June 5th, and 11/13/2012 could return an error because SQL Server doesn't know of any 13th month. Loads of details here.
And you don't need to convert it to date first. Just do that with the ALTER - converting it to date does little for you when it's still stored as a string...

Storing just Month and Year in SQL Server 2008?

Is it possible to save the month/year in SQL Server 2008 with a regular datetime? I don't need a time stamp and the column is going to store unique mo/yr combinations (i.e. 10-11, 11-11, 12-11, etc,.).
Any recommendations?
Without knowing the intended use, it is not possible to advise correctly. However, storing month and year is easily done in at least three ways:
use a date field, but always store into it the first day of the month at midnight; then always custom format the date field for display
add integer year and month fields, and populate them by splitting a date
add an integer field where you encode it as year * 100 + month or some other useful scheme
Sql Server will store the datetime data in its entirety (year-month-day hour:min:sec.milliSec) no matter what.
When you store your dates, you can make the day the 1st of the month. Just format the dates how you want when you do your queries.
http://www.sql-server-helper.com/tips/date-formats.aspx
From SQLServer 2008 and onwards:
Save as a Date column and add the following check constraint to make sure the value is always the first of the month:
datepart(month, MyDate)<>datepart(month,dateadd(day, -1, MyDate))
You cant only store year and month in a DateTime column. Well, what you can do is default the rest of the values. ie: 2011.10.1.1 , 2011.11.1.1 like that.
Or you can store it as string.
SELECT RIGHT(CONVERT(VARCHAR(10), GETDATE(), 105), 7) AS [MM-YYYY]
You should try this