In SQL Server, I queried a column called DelDate which contains all the date as character string like
"Thu 1/9/2022"
"Wed 12/9/2022"
It impossible to compare manipulate this column with other columns contain standard format dates. I tried to use the command like
Convert(DATE, DelDate)
but got the error message:
Conversion failed when converting date and/or time from character string.
Any suggestions?
Thanks
I would do something like this:
SELECT CONVERT(Date, RIGHT('Wed 12/9/2022', LEN('Wed 12/9/2022') - 4), 103) DelDate2
Related
How can I convert varchar string to date?
select CONVERT(date,'23/12/2019')
avoiding the following error:
Conversion failed when converting date and/or time from character string
Thanks
If you are using SQL Server (as suggested by the use of CONVERT() for this purpose), you can use CONVERT() with a third argument to specify DD/MM/YYYY format. That argument is 'DD/MM/YYYY'. No. Wait. That would be too sensible. You want:
select CONVERT(date, '23/12/2019', 103)
You are doing right except one thing which is missing from your statement and that is date_style similant.
select CONVERT(date, '23/12/2019', [put the similant here])
Example
select CONVERT(date, '23/12/2019', 110)
Result
2019-12-23
for full understanding
Click Here
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)
I currently have dates stored in a general attribute field in the database as a string.
They are all stored in the format DD/MM/YYYY for example 01/01/2000
I am able to convert them them to datetime successfully by using the following in my select statement. For example CONVERT(DATETIME, attribute.field_value, 103) where attribute.field_value contains a date.
The SELECT statement works fine and returns the whole table with them correctly.
I can also return a column with todays date in the same format as follows CAST(getdate() AS datetime)
The problem occurs when I try to compare, now I only want to return everything that is newer than today in pseudo code that would dateNewerThanToday > dateToday
Therefore I have tried
WHERE CONVERT(DATETIME, attribute.field_value, 103) > CAST(getdate() AS datetime)
this gives me the error
Conversion failed when converting datetime from character string.
I have tried a multitude of cast/converts to get it to work. I have also wrapped by select so I am only doing it on dataset with the correct data.
Any help would be super useful! Many thanks in advance!!
A couple of things ..
You do not need to convert to GETDATE() to DATETIME data type as it already returns datetime data type.
Instead of CONVERT(DATETIME, attribute.field_value, 103)
use
CONVERT(DATETIME, attribute.field_value) or CAST(attribute.field_value AS DATETIME)
Add a where clause in your select to get only valid DATETIME values. something like
WHERE ISDATE(attribute.field_value) = 1
This will filter out any values which appears to be a date value but sql server doesnt see them as valid date values.
Important Not
Use appropriate data types. If this column is storing date values why not use the DATE or DATETIME data types.
I ran into this exact problem.
Values from a VARCHAR(50) column returned in the SELECT could be cast as date without issue. However when cast in a comparison in the WHERE clause the error occurred.
Of note, the error only occurred when I had other restrictions in the WHERE clause.
When I added ISDATE() to the WHERE clause, the error no longer occurred.
e.g. Shortened example of what worked:
SELECT CONVERT(DATE, mat.myAttributeColumn), mdct.myDateComparisonColumn
FROM myAttributeTable mat
JOIN myDateComparisonTable mdct ON mdct.key = mat.key
WHERE ISDATE(mat.myAttributeColumn) = 1
and mdct.myDateComparisonColumn < convert(DATE, mat.myAttributeColumn)
I can't seem to figure this one out and tried searching stackoverflow.
I have two lines where I want to convert the date. I didn't create these datatypes or tables and don't want to change the date types in case it messes something up.
I have one table where one of the columns is of type (datetime2(7), not null).
I have another table where the column is of type (datetime, null).
when I do a query they return a time (respectively) that looks like
2015-01-25 10:11:50.9050000
2015-01-19 10:24:42.323
The above is before I do any conversion or casting.
I'd like to convert the format of those to
dd/mm/yy hh:mi:ss:mmmAM
So I used the code below (respectively)
SELECT TagName, CONVERT(DateTime, EventStamp, 131) AS ConvertedTime
FROM ALMDB.dbo.Hist
and
SELECT TagName, CONVERT(DateTime, DateTime, 131) AS ConvertedTime
FROM ALMDB.dbo.Hist
But it doesn't seem to work. It drops the ss (seconds) part and the month/day/year portion does't come out according to style 131. Anyone know why? It looks like below
25/01/2015 10:11 AM
19/01/2015 10:24 AM
I tried a bunch of way and it doesn't seem to work. I'm running MS SQL server 2012 and just doing the query in SQL Server Manager Studio.
If you want to convert to an output format, you want to convert from a datetime to a string.
Try this:
SELECT TagName, CONVERT(varchar(255), EventStamp, 131) AS ConvertedTime
FROM ALMDB.dbo.Hist
Your code tries to convert from datetime to datetime, which isn't what CONVERT is meant to do. If you wish to control the string representation of a date in your output, convert to nvarchar instead: CONVERT(NVARCHAR(25), DateField, 131). If you'd like to change the default string representation of a datetime field on a per-session basis, use the SET DATEFORMAT command. https://msdn.microsoft.com/en-us/library/ms189491.aspx
I am using SQL server 2008 R2. I know I can use CONVERT with different format code as the third parameter to do the conversion to DATETIME first and CONVERT again to VARCHAR with another format code to change the display format.
The real problem now is I have mixed raw data in a single column. So my question is how do you write a single SELECT statement to display from mixed YYYY/MM/DD, DD/MM/YYYY all to DD/MM/YYYY?
I tried to use ISDATE() but it think 31/01/2013 is not a date while 01/01/2013 is a date. Now I could only think of to see if the YYYY is on the left or on the right to determine the correct input format, but I dont know how to write it out in a single SELECT statement.
Any procedure to change the format first then do a simple SELECT is not an option. I am not allowed to change the source.
Thank you
Why not just use string manipulations? Something like:
select (case when substring(d, 5, 1) = '/' -- YYYY/MM/DD
then right(d, 2)+'/'+substring(6, 2)+'/'+left(d, 4)
else d
end)
By the way, if you are choosing formats for dates when represented as strings, I highly recommend YYYY-MM-DD (or YYYY/MM/DD) because comparison operators work on them.
If you are sure that only those 2 formats (yyyy/mm/dd and dd/mm/yyyy) exist in the data, then you could probably get away with a CASE statement along the lines of:
CASE
WHEN (SUBSTRING(dateColumn, 5, 1) = '/') THEN CONVERT(datetime, dateColumn, 111)
ELSE CONVERT(datetime, dateColumn, 103)
END