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 )
Related
I am trying to convert a column FC_FROM_DATE containing decimal values (ex. 20,200,721) into a date format 2020/07/21.
I have tried this code
SELECT TO_DATE(CHAR(CAST(FC_FROM_DATE AS DECIMAL(8,0))), 'YYYY/MM/DD')
FROM MARKETS.FORECAST
I get an error
Argument for chr should be between 0 and 127
Would much appreciate your help!
If you are using Oracle then you can use the following query:
SELECT TO_DATE(CAST(FC_FROM_DATE AS VARCHAR(8), 'YYYY/MM/DD') FROM Table
Seeing as the persisted format is YYYYMMDD you could convert the value to a varchar(8) and then use CONVERT to get a Date instance.
SELECT CONVERT(DATE, CAST(FC_FROM_DATE AS VARCHAR(8))) FROM MARKETS.FORECAST
Ideally Dates are stored as Date and a Date with a time component is stored as DATETIME2 (or equivalent if not Sql Server).
Test of the code above
DECLARE #FC_FROM_DATE decimal(8,0) = 20200721
SELECT CONVERT(DATE, CAST(#FC_FROM_DATE AS VARCHAR(8)))
2020-07-21
Disclaimer: This works in MS 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)
I am trying to convert varchar field which is in format dd.mm.yyyy to date type field in SQL SERVER 2008.
I used approach like convert(datetime, left(yourDateField, 2) + '01 20' + right(yourDateField, 4), 100) but unable to so.
Is there any way to convert Varchar string which is in format dd.mm.yyyy to Date type in SQL?
select convert(date,dateField,104)
from tab
Try this Datetime to Varchar
SELECT CONVERT(VARCHAR(20),GETDATE(),104)
Varchar to Datetime
SELECT CONVERT(DATETIME,yourfeild,104)
Refer this link
Try this.
DECLARE #String VARCHAR(100)= '21.11.2016';
SELECT CONVERT(DATE,#String,103)
If Your date field is not in proper datetime format then this type of Out of range value error will occurred.
If your date field is in proper datetime format then you can easily convert varchar field to datetime format.
see example:
select CONVERT(datetime,'2016-11-21 01:02:12')
select CONVERT(datetime,'2016-11-31 01:02:12')
select CONVERT(datetime,'2016-11-21 00:00:01 PM')
In above Example,First query get easily converted to datetime
but second and third query will give error as out of range value. because 31 Nov is invalid date. and 00:00:00 PM is also wrong date format.
IF your field is in correct format
then simply try any one of the below query
DECLARE #String VARCHAR(100)= '21.11.2016';
SELECT CONVERT(DATETIME,#String,104)
SELECT CONVERT(DATETIME,#String,103)
Hope so, you get what I am trying to say.
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).
I am currently using:
SELECT DATEPART(TZ, SYSDATETIMEOFFSET())
However, it returns the offset in minutes. I would like to preserve the format of '-05:00' instead of '-300'
Thank you.
In MS SQL Server you can also use
SELECT DATENAME(tz, SYSDATETIMEOFFSET())
which will return a nvarchar with the offset
screenshot of the excecution result
If you want to extract the exact string '-05:00' at the end of a datetimeoffset variable, you can use SQL Server string manipulation. I do not think it is possible to do this using the built-in SQL DateTime functions. You can use the CAST function, which I believe defaults to ISO 8601 format:
declare #timeStr nvarchar(50) = CAST(SYSDATETIMEOFFSET() as nvarchar(50))
select right(#timeStr, 6)
If you want to be more explicit, you can use the CONVERT function with a format type of 126, explicitly telling SQL Server to use ISO 8601:
declare #timeStr nvarchar(50) = CONVERT(nvarchar(50), SYSDATETIMEOFFSET(), 126)
select right(#timeStr, 6)
Both of these approaches in my time zone return:
-06:00
For more information about CAST and CONVERT see here.