I need to get all rows from a table that have a date of the last 7 days or greater. My issue is that when the DB was originally setup, someone set it up as VARCHAR. So now I need to CONVERT the String to a DateTime.
The issue is, the format of the Date/Time isn't recognized by SQL. The format is:
2023-01-01T00:00:00.000+0000
If I can trim off the last 8 characters from the string, SQL will recognize it. But I've had no luck thus far. The statement I was attempting was:
SELECT CONVERT(datetime, TRIM('.000+0000' FROM date_col), 127) FROM table_name;
But that resulted in the error:
Conversion failed when converting date and/or time from character string.
Try this
SELECT CAST(LEFT(REPLACE('2023-01-01T00:00:00.000+0000', 'T', ' '), 19) AS DATETIME)
No need for the replace with datetime2(n)
Select WithMS = try_convert(datetime2(3),left('2023-01-01T00:00:00.100+0000',23))
,SansMS = try_convert(datetime2(0),left('2023-01-01T00:00:00.100+0000',23))
Results
WithMS SansMS
2023-01-01 00:00:00.100 2023-01-01 00:00:00
Related
I'm using MS SQL server and I have a date field of type text. The dates stored there are in this format
2017-03-01T18:23:02+0700
I'm trying to convert this field in a datetime field but I fail. I have tried
CONVERT(datetimeoffset,date, 127)
CONVERT(datetime,date, 127)
CONVERT(datetime2,date, 127)
but I keep getting
Conversion failed when converting date and/or time from character
string.
I think the problem is that according to ISO8601 the time offset must be in the format hh:mm while mine is hhmm. I don't mind keeping only the date (yyyy-mm-dd) if it is more easy.
I have read similar question but none matches exactly my case and I can't figure out the solution.
Try this
Declare #dt varchar(50)
set #dt = '2017-03-01T18:23:02+0700'
select convert(datetime, replace(LEFT(#dt, LEN(#dt) - 1), '+', '.'), 126)
If you need only date part then you can use below query
SELECT CAST(LEFT('2017-03-01T18:23:02+0700',10) as DATE)
Use Below query to convert datetime :
SELECT CONVERT(DATETIME,REPLACE(REPLACE('2017-03-01T18:23:02+070','T','
'),'+','.'),103)
For DATE only use below query :
SELECT CONVERT(DATE,REPLACE(REPLACE('2017-03-01T18:23:02+010','T','
'),'+','.'),102)
It doesn't seem to work in both ways (both raise error):
SELECT CONVERT(datetime,'2017-03-01T18:23:02+0700',127)
SELECT CONVERT(datetime,'2017-03-01T18:23:02+07:00',127)
It seems that it works only specifying Z as time zone:
SELECT CONVERT(datetime,'2017-03-01T18:23:02Z',127)
This looks easy solution but I can't seem to figure out as to why this is not working for me. I have a column that has data like this:
DateField
----------
12/16/2016
11/06/2016
All I want to do is to convert from varchar into a date column, but I am getting this error:
Conversion failed when converting date and/or time from character string.
Here is my simple query:
select convert (date, DateField) as convertedField
from myTable
Nothing wrong with the two examples you have given. There are some bad dates in your table which cannot be converted to date.
Use TRY_CONVERT function for bad dates it will return NULL
select TRY_Convert(date,DateField)
From myTable
You should always store dates in DATE/DATETIME datatype.
If you want to see the records which cannot be converted to date then
select DateField
From myTable
Where TRY_Convert(date,DateField) IS NULL
If working with a specific date format like mm/dd/yyyy You can specify it in Convert() function like the following
CONVERT(DATETIME,DATAFIELD,101)
If it still is not working, use TRY_CONVERT() to get which rows are throwing this exception:
SELECT *
FROM TBL
WHERE TRY_CONVERT(DATETIME, DATAFIELD, 101) IS NULL
This will return rows that cannot be converted
TRY_CONVERT() will return NULL if conversion failed
Read more about DateTime formats here:
SQL Server CONVERT() Function tutorial
Read TRY_CONVERT MSDN Article
You need to specify the format of date time while formatting. The date in your table is currently in U.S format so you should pass the third argument 101 in your convert function.
SELECT CONVERT(date,[DateField],101) FROM myTable;
Working Fiddle here http://rextester.com/NYKR49788
More info about date time style here: https://msdn.microsoft.com/en-us/library/ms187928.aspx
I'm trying to compare 2 people and see whose birthday falls earlier in the year. I'm trying to hack together a solution where I take the mm-dd from a datetime field using RIGHT(DOB, 5) and concatenating it with '1900' to create a new date field and use that for comparison.
CONVERT(DATE,'1900-' + RIGHT(DOB,5),126) DOB
However I'm getting the following error:
Conversion failed when converting date and/or time from character string.
Any suggestions for how to fix this?
Try this
right(convert (char(10), dob, 120), 5)
the 120 format is yyyy-mm-dd
You can use
format(date,'MMdd')
I need to convert 2014-11-18T14:08:43+00:00 which is in varchar in my sql developer to date format in order to filter a few entries.
I tried
to_date(LAST_UPDATE_DATE,'YYYY-MM-DD')
but it gives an error
ORA-01830: date format picture ends before converting entire input
string.
Kindly help..
Just in case you didn't mean to put up sql server but instead you need to use oracle (seeing as you are using to_date and you are getting an ora exception)
I added a quick datetime conversion for date and timestamp (no milliseconds) for your date format:
SELECT to_Date(concat
(substr
(myvar,0,10),
concat(' ',
substr(myvar,12,8)
)
),'YYYY-MM-DD HH24:mi:ss') AS mydate
FROM mytable
Fiddle
declare #varDate as nvarchar(50) = '2014-11-18T14:08:43+00:00'
select CAST(substring(#varDate,0,CHARINDEX('T',#varDate)) as date)
Either you can use it like this
declare #Date as nvarchar(100) = '2014-11-18T14:08:43+00:00'
SELECT CONVERT(DATE,#Date) AS Date
OR go for this answer which is accepted in this question
ORA-01830: date format picture ends before converting entire input string / Select sum where date query
ORA-01830: date format picture ends before converting entire input string.
to_date(LAST_UPDATE_DATE,'YYYY-MM-DD')
2014-11-18T14:08:43+00:00 is TIMESTAMP and not DATE.
First of all, you should never ever store DATE/TIMSTAMP as string. It is a database design flaw.
Anyway, you could convert it to TIMESTAMP WITH TIMEZONE.
For example,
SQL> SELECT to_timestamp_tz('2014-11-18T14:08:43+00:00',
2 'YYYY-MM-DD"T"HH24:MI:SS.FFTZH:TZM')
3 AS tm_stamp
4 FROM dual;
TM_STAMP
----------------------------------------------------------------
18-NOV-14 02.08.43.000000000 PM +00:00
SQL>
You could try this;
Select CAST ('2014-11-18T14:08:43+00:00' as date)
The assumption is you are in SQL Server 2012
SELECT DISTINCT YEAR(convert(varchar(max),OrderCreatedDate)) from webshop
The above sql query is producing this error:
Conversion failed when converting date and/or time from character string.
The value in the database is NVARCHAR and in the following format: DD/MM/YYYY 00:00:00 AM (/PM)
I would like to select the unique values for the year only!
thanks for any help
To avoid this sort of issues, date should be saved in DateTime type field Not in a string field.
Year() function parameter is a DateTime type not a String. So you should make sure that the string you are passing is convertible to a DateTime type. In this case you could trim out the time part with Left() function and use 103 style as below.
Fiddle demo:
--Example
declare #val nvarchar(50) = '28/10/2013 11:25:45 AM (/PM)'
select year(convert(date,left(#val,10),103)) myYear
--Applying to your query
SELECT DISTINCT Year(Convert(Date,Left(OrderCreatedDate,10),103)) FROM Webshop
UPDATE:
If you are getting errors, it could be due to your date format. i.e. Format of the string you have saved may not be as you have described (DD/MM/YYYY 00:00:00 AM (/PM)) in the question.
Please check with ISDATE() function before converting to date and identify which records are causing the problem and correct them.
Try this query to get all of them with invalid strings. Following query will return 0 for values with invalid formatting.
SELECT DISTINCT CASE WHEN IsDate(eft(OrderCreatedDate,10))=1 THEN
Year(Convert(Date,Left(OrderCreatedDate,10),103))
ELSE 0 END as myDate,
OrderCreatedDate
FROM Webshop
Or you could get only the records which are causing the problem as;
SELECT OrderCreatedDate
FROM Webshop
WHERE IsDate(Left(OrderCreatedDate,10)) = 0
select distinct year(CONVERT(NVARCHAR(255),CONVERT(SMALLDATETIME, columnName,105)))
Try this
SELECT DISTINCT YEAR(Convert(datetime,OrderCreatedDate,103)) from webshop
DD/MM/YYYY 00:00:00 AM (/PM) is the British format. See Convert