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

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..

Related

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

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

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

Convert Date to formatted date

I have a column for dates and the values are like this 1181202 and i want to convert it to normal date format "02-12-2018" so i can compare it with another date however when i am trying the following it returning wrong year and date
SELECT CONVERT(Datetime, Text_UPD_DATE,106) -- i have tried all numbers
From Notes
it's returning 5134-01-09 00:00:00.000
Can you please advise on the correct command
At a total guess, based on your one example:
CONVERT(date,'20'+STUFF(Text_UPD_DATE,1,1,''),112)

Convert Data from yyyy-dd-mm to mm/dd/yyyy Issue

I have a column in my table with Dates in the format yyyy-mm-dd I want to convert all the dates in that column to the format mm/dd/yyyy
I am using the below query
UPDATE Test.dbo.Status
SET DateIn = CONVERT(DATE,DateIn ,101)
The DateIn column is defined as Date in my table (DateIn DATE NULL)
The query does no change to the data. am I doing some thing wrong here?
You can change the default format in which SQL Server displays a date, but you can't alter the way a DATE value is stored via CONVERT(). You can format a date however you want if you store it as a string, but you lose functionality when you do that and it's not advisable. If you are hell-bent on storing a formatted version, you might want to create a new VARCHAR() field so you can preserve your DATE version.
You're better off formatting the date at the application level.
The reason your query does nothing is that the actual DATE values are equivalent. Notice when you take any valid date format and CAST() it as DATE the resulting format is the same regardless of the input:
SELECT CAST('20040510' AS DATE)
SELECT CAST('2004-05-10' AS DATE)
SELECT CAST('May 10, 2004' AS DATE)
All return: 2004-05-10 on my instance of SQL Server.

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...