Convert Text to Date - sql

I have a text field in Access called "TempD"
which contains data like below
Mon, Oct 6, 2014
Mon, Nov 10, 2014
I need to convert this to proper date format using SQL and put it in Field "Emaildate"
I am using the sql query below:
UPDATE MissingT
SET MissingT.emaildate = format(cdate(Right([tempd],4) & "/" & Mid([tempd],6,3) & "/" & Mid([tempd],9,2)));
But the issue is that the TempD field varies in length if the day is single/double. When the day is in 2 digits
(Mon, Nov 10, 2014)
it is converting this text to 2014-11-01 instead of 2014-11-10
Any idea how can this be resolved?

Discard the abbreviated day name, comma, and space from the beginning of your date string ...
? Mid("Mon, Oct 6, 2014", 5)
Oct 6, 2014
The resulting substring can be converted directly to a Date/Time value using CDate() ...
? CDate(Mid("Mon, Oct 6, 2014", 5))
10/6/2014
Then if you want that Date/Time value as a string in yyyy/mm/dd format ...
? Format(CDate(Mid("Mon, Oct 6, 2014", 5)), "yyyy/mm/dd")
2014/10/06
If that is what you're after, you can do it in an UPDATE query like this ...
UPDATE MissingT
SET emaildate = Format(CDate(Mid(tempd, 5)), "yyyy/mm/dd");

It makes no sense to apply Format on a date value passed to a Date field.
This is how to solve it:
UPDATE
MissingT
SET
emaildate = DateValue(Mid([tempd], 5))

Related

Obtaining the correct date using VBA

01/02/2018 12:25:00 PM
I have the above date which is in an Excel spreadsheet's B1 cell. The date is 01 February 2018 but the VBA code below
thedate = CDate(Application.Cells(linecount, 2))
converts this to 02 January 2018
What VBA code do I use to have this remain 01 February 2018?
If you want to use VBA to return a Date data type value of string 01/02/2018 12:25:00 PM representing 01 February 2018 in your local regional date settings (in a message box), you could use:
Dim dt as String
dt = "01/02/2018 12:25:00 PM"
MsgBox DateSerial(Mid(dt, 7, 4), Mid(dt, 4, 2), Left(dt, 2))
† This is how it displays for me. See note below.
If you actually want a string to be returned (in a messages box) in the specific format you mentioned, "dd MMMM yyyy", you could use:
Dim dt as String
dt = "01/02/2018 12:25:00 PM"
MsgBox Format(DateSerial(Mid(dt, 7, 4), Mid(dt, 4, 2), Left(dt, 2)), "dd MMMM yyyy")
If you wanted to store this data (including the time), or use it for calculations, then the proper way to do it would be more like:
Dim strMyDate as String, dtMyDate as Date
strMyDate = "01/02/2018 12:25:00 PM"
dtMyDate = DateSerial(Mid(strMyDate, 7, 4), Mid(strMyDate, 4, 2), _
Left(strMyDate, 2)) + TimeValue(Mid(strMyDate, 11))
MsgBox dtMyDate
† Note that these MsgBox's are displaying date/time's based on my system's Windows settings (shown below), shown here, where they could be adjusted if required. (Hence, the results above will appear differently for you.)
Open these settings by hitting type region, and hit ᴇɴᴛᴇʀ.
(Click to enlarge)
The DateTime is actually stored in Excel as a number where 0 = December 30, 1899 and +1 = +1 day, so your example date of 01 February 2018 12:25:00 PM is actually only a formatted representation of the DateTime serial number, in this case, 43132.5173611.
More Information:
MSDN : DateSerial function (VBA)
MSDN : Format function (VBA)
MSDN : TimeValue function (VBA)
Microsoft.com : How to use dates and times in Excel

Failed conversion from nvarchar to datetime

I am trying to convert an nvarchar date into a date time, but this error occurs: I have tried multiple ways including CAST and Convert (as code below) with no avail. Any suggestions ?
Date Format : Wed, 19 Jul 2017 16:23:38 +0000
Code:
INSERT INTO feed.article(title,link,sourceID,[date])
SELECT title,link,s.sourceID,
CONVERT(DATETIME,[date],121)
FROM feed.tempXML t
JOIN feed.[source] s ON s.sourceName = t.[source]
Error given:
Conversion failed when converting date and/or time from character string.
If you have MS SQL Server 2012 or higher, you may use TRY_PARSE.
SELECT CAST(TRY_PARSE ('Wed, 19 Jul 2017 16:23:38 +0000' AS datetimeoffset) AS datetime)
I would do this in two parts, one for the date and one for the time:
SELECT title,link,s.sourceID,
(CONVERT(DATETIME, SUBSTRING([date], 5, 10), 106) +
CONVERT(DATETIME, SUBSTRING([date], 18, 8))
)
FROM feed.tempXML t JOIN
feed.[source] s
ON s.sourceName = t.[source];
This minimizes the string operations, so it seems like a pretty simple approach.
EDIT: Check out the solutions by Oleg and Gordon. I actually prefer them both to my own (as it's quite convoluted).
You need to get your date format from this...
'Wed, 19 Jul 2017 16:23:38 +0000'
...to this...
'19 Jul 2017 16:23:38'
You can remove chars from the beginning and end using LEFT and RIGHT. Removing the last 6 from the end would look like this:
LEFT([date], LEN[date] - 6)
We can use the same syntax for our RIGHT() to remove the first 5, but [date] must now be replaced with the entire string from above:
-- RIGHT([date], LEN([date]) - 5) becomes...
RIGHT(LEFT(#d, LEN(#d) - 6), LEN(LEFT(#d, LEN(#d) - 6)) - 5)
All in all, it's ugly, but works:
INSERT INTO feed.article(title,link,sourceID,[date])
SELECT title,link,s.sourceID,
CONVERT(DATETIME,RIGHT(LEFT([date], LEN([date]) - 6), LEN(LEFT([date], LEN([date]) - 6)) - 5),121)
FROM feed.tempXML t
JOIN feed.[source] s ON s.sourceName = t.[source]
IMPORTANT NOTE: This is under the assumption that the format of your date will always have 5 unnecessary characters at the beginning, and that your timezone offset (the +0000 at the end) will always be 0 (so we can simply ignore it).
If you'll have values that make use of the timezone offset, you'll need to account for that.
Based on the format, we should be able make a few "safe assumptions"...
1) The weekday will always be expressed as a 3 char abbreviation.
2) The 3 char abbreviation will be followed by a comma and a space.
3) The portion of code we're interested in will be either 19 or 20 characters.
(10 for single digit dates and 20 for double digit dates)
4) There will be a space following the date.
Based on these assumptions, you should be safe to use the following...
CREATE TABLE #TestData (
StringDate NVARCHAR(40) NOT NULL
);
INSERT #TestData (StringDate) VALUES
(N'Wed, 19 Jul 2017 16:23:38 +0000'),
(N'Wed, 9 Jul 2017 16:23:38 +0000');
SELECT
DateTimeDate = CAST(SUBSTRING(td.StringDate, 6, 20) AS DATETIME)
FROM
#TestData td;

How to convert “Thu Jun 11 00:49:35 IST 2015” to “YYYY-MM-DD hh:mm:ss” in SQL Server?

How to convert “Thu Jun 11 00:49:35 IST 2015” to “YYYY-MM-DD hh:mm:ss” in SQL Server?
I tried to convert and casting but it's throwing an error
A few things to note about this before I posit a solution:
The source of this information should be modified, if at all possible, to conform to some ISO-standard date.
SQL Server pre-2016 doesn't handle time zone names well, so I'm going to presume that all of your strings contain "IST". If they don't, you'll have to adjust accordingly by searching for them and creating some kind of switch case to modify the resulting datetimeoffset.
I have no idea whether your day representation is two-digit or variable-digit. I've assumed that it's variable, but you can probably simplify this answer with fixed-string parsing if it's two-digit.
Since you have a timezone, the target representation of YYYY-MM-DD hh:mm:ss is ambiguous. I've converted it here to UTC.
If your string is in a table called Dates with column HorribleString, then:
select
convert(varchar(100), convert(datetime2, convert(datetimeoffset,
substring(HorribleString, len(HorribleString) - 3, 4) -- Year
+ substring(HorribleString, 4, len(HorribleString) - 12) -- len(Thu ) + len( IST 2015) = 12
+ case substring(HorribleString, len(HorribleString) - 7, 3) -- Timezone
when 'IST' then ' +05:30'
else ''
end
, 109), 1), 20)
from
Dates

SQL UPDATE syntax error

I am trying to update some data within a table in ms-access using vb.net. However I am getting an error when trying to run the code. My Query is:
Query = "UPDATE tabDatafiveMinutely SET PAR0050=308.3925 WHERE TimeValue = #16 May 2012 14:30:00#"
The error I get is:
No value given for one or more required parameters.
Does anyone have anyone have any ideas on how to solve this?
EDIT:
The Syntax is Correct I had a problem with the Database
Promoted to answer as requested :)
This error means that some field names, specified in query, are not present in table (or are reserved words).
just dont make use of # instead of that us ' might resole your issue
UPDATE tabDatafiveMinutely SET PAR0050=308.3925
WHERE TimeValue = '16 May 2012 14:30:00'
use this
Query = "UPDATE tabDatafiveMinutely SET PAR0050=308.3925
WHERE TimeValue = CONVERT(DATETIME, '2012-05-16', 102)
for other date format
Date -100- MMM DD YYYY' -- Feb 5 2010
Date -101- MM/DDYYYY'
Date -102- YYYY.MM.DD'
Date -103- DD/MM/YYYY'
Date -104- DD.MM.YYYY'
Date -105- DD-MM-YYYY'
Date -106- DD MMM YYYY' --ex: 03 Jan 2007
Date -107- MMM DD,YYYY' --ex: Jan 03, 2007
Date -108- MMM DD YYYY' -- Feb 5 2010
Date -110- MM-DD-YYYY' --02-05-2010
Date -111- YYYY/MM/DD'
Date -112- YYYYMMDD' -- 20100205
Date -113- DD MMM YYYY' -- 05 Feb 2010
to convert date to text use like this
CONVERT(VARCHAR(12),getdate(),113)
The date must be between quotes, I think:
Query = "UPDATE tabDatafiveMinutely SET PAR0050=308.3925 WHERE TimeValue = '#16 May 2012 14:30:00#'"

Convert text to date format

How do I convert a text format e.g.
Thursday, 1 January 2009
to a date in sql?
Thank you
For SQL Server you can use:
SELECT CAST(
SUBSTRING('Thursday, 1 January 2009',
CHARINDEX(',', 'Thursday, 1 January 2009')+1, LEN('Thursday, 1 January 2009'))
AS DATETIME)
What type of text do you mean?
if you mean any type of text .... that isn't exist
if you mean text like '19900101' or like '1/1/2005' ... this will be converted to date automatic in sql server