I need to (if its possible) convert a column that stores the date as a string in the following format Monday October 19, 2015 to an usable datetime for example 01/10/2015
Every cast and convert I have tried to run it through just returns
Conversion failed when converting date and/or time from character string.
Nothing I can do about the saved format as its part of a fixed process.
Chop off the day:
DECLARE #S VARCHAR(32) = 'Monday October 19, 2015'
SELECT CAST(SUBSTRING(#S, CHARINDEX(' ', #S) + 1, LEN(#S)) AS DATETIME)
> 2015-10-19 00:00:00.000
You should manipulate the string to this format - "DD/MON/YYYY", where DD is day, MON is the first three letters of the months name, and YYYY is the year.
You could do that easily do that using string functions, and from there you can insert the date into the DB using conventional conversions, such as the ones listed in this post - Convert DD-Mon-YYYY to DD/MM/YYYY.
Related
How can we insert date in in '17-DEC-80' format ?
How to find the last day of this month ?
like this I want to insert 10 dates in the given format and find the last day of the months specified in the date.
This can be acheived through the STR_TO_DATE and DATE_FORMAT internal functions.
You can insert a date so long as you accompany it with the format you are providing it in.
INSERT INTO DateFormats (DF_DATE_FIELD) VALUES (STR_TO_DATE('17-Dec-80', '%d-%b-%y'));
You can also SELECT a that same format date value with the DATE_FORMAT() function. To return the value you want from the table you would use
SELECT
DF_ID,
DF_DATE_FIELD,
DATE_FORMAT(DF_DATE_FIELD, '%d-%b-%y')
FROM DateFormats;
Assuming that DF_DATE_FIELD is in fact a DATE or DATETIME field. Also beware that this is case sensitive
The formatting is as follows
%d = Day of Month for 2 places (i.e 05, 12, 23)
%b = Abbreviated Month (i.e JAN, FEB, DEC)
%y = 2 digit year code (i.e 18, 80, 99)
Read more about formatting At this handy W3 Schools page
And use this DBFiddle for reference
To follow your desired format, use below;
select STR_TO_DATE('17-Dec-80', '%d-%b-%y');
To get last day of the month
select LAST_DAY(STR_TO_DATE('17-Dec-80', '%d-%b-%y'));
I have dates in a following form (text):
2016-07-05T13:09:35-06:00
It's ODBC format, so I was able to simply:
SELECT CONVERT(DateTime2, REPLACE(LEFT(TimeModifiedUnparsed, 19), 'T', ' '), 120)
That get's me DateTime2, but without time offset.
I can parse and convert Hour and Minute and Sign out of last 6 characters and then do DATEADD.. but I thought maybe there is a better way to convert such a strings to utc DateTime in SQL Server?
select convert(datetime2, cast('2016-07-05T13:09:35-06:00' as datetimeoffset), 1)
Result:
2016-07-05 19:09:35.0000000
How can I convert a date like 'Jan 1 2014 12:00AM' to 20140101?
when the month is less then 10 I need to put a zero.
I tryed with datepart but had problem adding '0' before it.
thank's.
SELECT YEAR(DateField) * 10000 + MONTH(DateField) * 100 + DAY(DateField)
To convert it to an integer. Or
SELECT convert(char(8), DateField, 112)
To convert it to a yyyymmdd string.
If your date is currently a string, you need to convert it to a real datetime value first:
SELECT CONVERT(datetime, 'Jan 1 2014 12:00AM')
(mon dd yyyy hh:miAM (or PM) is the SQL Server default format for datetime conversions, so there is no need for a format specifier here.)
If it's already a datetime, you can simply use formatting option 112, which represents YYYYMMDD:
SELECT CONVERT(nvarchar(8), myDateTimeField, 112)
(Nesting the two statements as required or converting the string result to a numeric data type is left as an exercise to the reader.)
The conversion of a date data type to a datetime data type resulted in an
out-of-range value.
The statement has been terminated.
I just need to convert a date field to a date time format.
Example of what the code would look like to convert from date to datetime2
DECLARE #d1 date;
SET #d1 = GETDATE()
-- When converting date to datetime the minutes portion becomes zero.
SELECT CAST (#d1 AS datetime2) AS [date as datetime2]
For more information about cast and convert, see the Microsoft reference.
You probably get out of range exception because Date supports a date range between
January 1, 1 and December 31, 9999 , while DateTime supports a date range only between
January 1, 1753 to December 31, 9999.
Want to know Why 1753? Read this. (Recommended reading for anyone that likes trivia items)
Try converting to Datetime2 instead of Datetime, this should be OK since Datetime2 supports a date range similar to date.
the conversion can be done simply by using CAST:
SELECT CAST(YourDateTypeColumn As Datetime2)
FROM YourTable
I'm importing a column of strings with partial dates into a csv file.
During import, is there a way to convert strings with the format "September 23" into a DATETIME format, using the partial date to populate month, day and year, and the time of insert as the time?
Edit: I clarified the question, so the below answers aren't quite relevant.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
I think you can try DATE_FORMAT().
mysql> SELECT STR_TO_DATE('OCT 1, 2013','%M %d,%Y'); // -> '2013-10-01'
STR_TO_DATE-returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts. If the date, time, or datetime value extracted from str is illegal, STR_TO_DATE() returns NULL and produces a warning.
Refer this link and this also.
This is ugly, but it works. The main problem was creating the year and time, and also dealing with single numeric values for the days of the month. CONCAT_WS is the glue in this scenario:
STR_TO_DATE(CONCAT_WS(',','September 5',YEAR(NOW()), TIME(NOW())), '%M %e, %Y, %T')
Returns:
2012-09-05 11:51:46