CONVERT issue in sqlserver with Msg 529 error - sql

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))

Related

CASTing and converting functions in SQL Server

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

Convert datetime string in datetime format in SQL server

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)

How do I convert a string of format dd/mm/yy into datetime in SQL Server 2008?

I have table named PTBV with two columns: Dateptbv varchar(50) and [Column 1] varchar(50).
I'm trying to use the CONVERT() function to get actual datetime values from the string data stored in the Dateptbv column.
Here are examples of the data in that column:
"10/01/13"
"11/01/13"
"14/01/13"
"15/01/13"
"16/01/13"
"17/01/13"
"18/01/13"
"21/01/13"
"22/01/13"
"24/01/13"
"25/01/13"
"28/01/13"
"29/01/13"
"30/01/13"
"01/02/13"
"04/02/13"
Note that the quotation marks in this sample are part of the data and are stored in the column. Otherwise, data is stored in dd/mm/yy format.
Unfortunately, every date style I've tried has resulted in an error message. How can I convert this data into DateTime values?
If I understand your picture correctly, your column Dateptbv is VARCHAR(50) and stores the date as yy/mm/dd wrapped in " characters. First of all: Change this, if ever possible!. Try to store values in appropriately typed columns!
Try
SELECT CONVERT(DATE,SUBSTRING(Dateptbv,2,8)),3) --3 for dd/mm/yy
Read this link to find more details about the abilities of CONVERT.
Try the below script
SELECT CAST(SUBSTRING(Dateptbv,2,8) AS DATE)
FROM PTBV

Convert varchar which represents DateTime in universal format (dd/MM/yy hh:mm) to DateTime

I have a table with a varchar column which represent date and time in the next format:
dd/MM/yy hh:mm
For example: 27/01/13 07:57
I need to convert this column to DateTime type.
I tried to do it without success.
Assume that #varcharDT contains the VarChar DateTime and I want insert the converted value to #dateTimeDT variable.
DECLARE #varcharDT varchar(1000)
SELECT #varcharDT = dateTimeColumn from dbo.tempTable
--Trying to convert
DECLARE #dateTimeDT DateTime
SET #dateTimeDT= CONVERT (DATETIME, #varcharDT )
The last line raise the next exception:
Conversion failed when converting date and/or time from character string.
I think the conversion fails because my VarChar DateTime is in custom format.
How can I solve it?
I am working with SQL Server 2008 R2
Thanks
Try:
CONVERT(datetime, #varcharDT, 3)
The last number is the style for the convert. The list can be found in the MSDN documentation article CAST and CONVERT (Transact-SQL).

Convert varchar in format mon-yy to datetime in SQL server

Is there a simple way to convert a date stored in a varchar in the format mon-yy (e.g. "Feb-09") to a datetime which will convert any invalid values to null.
I'm currently using string manipulation combined with a case statement but it's rather cumbersome.
For valid values you can just put a day on it and it's a complete date:
convert(datetime,'01-' + 'Feb-09',120)
This will do it.
SELECT convert(datetime,'01-' + 'Feb-09',120)
CONVERT( DATETIME, "Feb-09", 64, 7 )