convert Microsoft access string value to datetime - sql

i'm working on a microsoft access DB with a column with values in the format Jan-2013, Feb-2014, May-2015.
Is it possible to convert those values to a valid datetime value from an Access select query? Only the month and year are important so day can be taken as just the first day of the month specified.

This works in Russian locale, so i suppose English will be good too:
cdate("01-" & [FieldWithMM-YYYY])

Since your fine using the first day of the month you can use DateValue to tag "the first" to the start of your existing string
Dim newDate as Date
newDate = DateValue("01-" & yourDateColumn)
or
Select DateValue("01-" & yourDateColumn) as FormattedAsADateNow
From yourTable
If you have nulls you'll need to protect against those or this will give a Type Mismatch.

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

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

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

Change date format in SQL for ASP

I have the following code:
"SELECT top 1 * FROM CensusFacility_Records WHERE Division_Program = 'Division 1' AND JMS_UpdateDateTime = '" & date & "'"
The date format in column JMS_UpdateDateTime is:
8/22/2013 12:00:07 AM
How can I make sure that my "date" in the query is converted to the correct time format?
My date variable in my SQL query is a real/date time. I would like for it to match the format within the JMS_UpdateDateTime field.
If your database table is small enough, you can cast the value to an actual datetime inside your query, and pass the value in as a datetime parameter, so you're comparing dates instead of comparing strings.
If your table has a decent amount of records, don't do this, because it will be a performance hog.
SELECT top 1 *
FROM CensusFacility_Records
WHERE Division_Program = 'Division 1'
AND cast(JMS_UpdateDateTime as datetime) = #dateParam
I believe SQL Server will be able to read the string that's in your database and automatically cast it properly, assuming your server settings are standard.
But in any case, use parameterized SQL instead of passing in a string like you've got.
The format of your SQL DateTime is actually a bit of a red herring here - it can be displayed in any way the front end (e.g. Management Studio) chooses. The important thing here is that your date variable is in an unambiguous format. With this in mind I'd recommend using the ISO 8601 date/time format, e.g. date.ToString("o") (assuming date is a DateTime).

Date Manipulation as text field

I am using Access 2010 as a front-end to a database on SQL Server 2008. I have a date field which is stored as a nvarchar(50). I have the following value in the text field DateHr 12/04/11 16:49:23 , which should translate to April 11, 2012 4:49 PM (As is the date and time the record was created.).
I cannot change the datatype of the field to DateTime as it messes up the dates even more (Ex. 12/4/2011 4:49:23 PM). I cannot change the way the record is entered.
I need to display this field in the format "mm/dd/yy" and be able to do where clause in this format.
I have tried the following just to see if it displaying correctly but dtDate is displaying 11/12/04:
Select (Format(CDate([DateHr]),"yy/mm/dd")) as dtDate
If you need to carry out the conversion in access then you can try either of these:
Select Mid([DateHr],4,2) & "/" & Mid([DateHr],1,2) & "/" & Mid([DateHr],7,2) as dtDate
This I think will give you the date in a string format, otherwise you could try the following to get it in a valid date format:
Select Format(DateSerial(Mid([DateHr],7,2),Mid([DateHr],4,2),Mid([DateHr],1,2)),"MM/DD/YYYY") as dtDate
Create a SQL Server view to expand the year component of the date text to 4 digits. Then SELECT CDate([DateHr]) AS dtDate FROM YourView should work from the Access side. However it might be better still to have the view cast the date text to an actual date type ... then you could use it directly from Access without the need for CDate().