I am trying to convert dd.mm.yyyy to yyyy-mm-dd.
select convert(date,CAST(WEEK_DATE as nvarchar(220)), 120)
from z_fact
Error
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
How can I resolve this?
Since your date is actually text, you must first convert it to a bona fide date using CONVERT. Then, use CONVERT on that date a second time to generate the text output you want.
SELECT CONVERT(varchar(20), CONVERT(datetime, '15.03.18', 4), 120);
Demo
Note that it is generally bad practice to store your dates as text. Hopefully you can use my answer to tidy up your table. For example, you could add a new datetime column new_dt and then update it using:
UPDATE yourTable
SET new_dt = CONVERT(datetime, old_dt, 4);
Don't worry about the internal format used by SQL Server. If you still need to display yyyy-mm-dd output, then use CONVERT again, as I did in my first query.
You can try this:
declare #dt NVARCHAR(12) = '15.03.18'
SELECT CONVERT(DATE,#dt,3)
GO
Related
I am tying to make date comparison with the query
select *
from dbo.OH_Case
where dbo.OH_Case.CreatedDate between Convert(varchar(30),'24/04/2017', 102)
and Convert(varchar(30),'01/05/2017', 102)
but it throws this error:
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
My application passes data in '01/05/2017' format and the date information in SQL Server is stored in '2017-03-04 10:07:03.490' format. What should I do to make both of these in same format which allows comparison?
First, you're converting a string literal to a varchar. Convert it to a datetime instead.
Second, you're passing in 24/04/2017, but date format 102 is yyyy.mm.dd. Try date format 103 for dd/mm/yyyy:
convert(datetime, '24/04/2017', 103)
Here dbo.OH_Case.CreatedDate is of data type Datetime and you have to convert your values in Datetime format for filter.
convert columns in Datetime format and format will be same for all. In your case, convert values in Datetime instead of Varchar and it will work fine.
select * from
dbo.OH_Case
where dbo.OH_Case.CreatedDate between cast('24/04/2017' as datetime) and
cast('01/05/2017' as datetime)
Updates:
Siddharth, I have checked you date values and figured out that issue is in date "24/04/2017". This format is for "MM/dd/yyyy". and 24 is passed as month which is not valid. Therefore it is throwing exception. I have updated result
to handle such exceptions.
select * from
dbo.OH_Case
where dbo.OH_Case.CreatedDate between convert(datetime, '24/04/2017', 103) and
convert(datetime, '01/05/2017', 103)
Here dbo.OH_Case.CreatedDate is of data type Datetime and you have to convert your values in Datetime format for filter. convert columns in Datetime format and format will be same for all. In your case, convert values in Datetime instead of Varchar and it will work fine.
select * from dbo.OH_Case format(where dbo.OH_Case.CreatedDate, 'dd/mm/yyyy') as CreatedDate
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)
select CONVERT(datetime,value,103) from results
This statement is giving me a error
"Msg 529, Level 16, State 2, Line 1
Explicit conversion from data type text to datetime is not allowed."
VALUE column Datatype is TEXT in the RESULTS table.
How can i overcome the above problem. please help !
You can't convert from text to datetime
You must apply two convert operations.
The first: From text to varchar
The second: From varchar to datetime
So your query will become:
SELECT CONVERT(datetime,
CONVERT(varchar(30),value), 103)
FROM results
Plus: text datatype is deprecated for new versions of Sql Server, so I strongly advice you to change (if you can) your datatype text into varchar(max).
The DDL code to apply your change is:
ALTER TABLE results ALTER COLUMN value varchar(max)
You need first to convert text to nvarchar. Try:
select convert(datetime, convert(varchar(30),value), 103) from results
Remarks: See How to convert text column to datetime in SQL
To convert text to float:
CONVERT (float, CONVERT (varchar(20), text_column))
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 read a bunch of posts with similar problem but I am stuck. In my table I have a column with datatype date. I am trying to select all rows that are between these two dates that I'm going to pass in. The column with date value something like this: 2012-03-26. I have SQLEXPRESS version 10.0.5520.0
SELECT tblTransaction."Date"
FROM tblTransaction
where cast(tblTransaction."Date" as date) between cast('2012-03-00' as date) and cast('2012-03-28' as date)
First you don't need to cast string to data when you compare it with data type column. SQL Server will do it for you.
Second, use format 'yyyymmdd hh:mm:ss' if possible, which is always working in any culture.
tblTransaction."Date" BETWEEN '20120301' AND '20120328'
Note '20120328' equals to '20120328 00:00:00'. Use 20120328 23:59:59 if you use it against datetime column.
This is an invalid date
cast('2012-03-00' as date)
Perhaps you meant
between cast('2012-03-01' as date) and cast('2012-03-28' as date)