Convert decimal to date format SQL - sql

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.

Related

How to have a good format of date?

I have this kind of date in my sql table : 1/24/2018 10:34:23 PM
And I want to extract the date with this format : YYYYMMDD that is to say I would like to have this 20180124
How could I do this ?
I tried this :
try_parse([date] as date using 'en-US')
but the format is wrong ...
I precise the name of my column is date
use the below to format dates in the YYYYMMDD format;
SELECT
CONVERT(VARCHAR(8), [DateField],112) AS DateField
Added as answer as requested...
Dates don't have formats in SQL Server, how you display them is the format. Strings have formats.
You can use FORMAT or CONVERT depending on your version.
convert(varchar(8),yourColumn,112)
format(yourCOlumn,'yyyyMMdd')
DEMO
declare #table table (yourColumn datetime)
insert into #table
values
(getdate())
select
yourColumn
,format(yourColumn,'yyyyMMdd')
,convert(varchar(8),yourColumn,112)
from #table
If your column is a string, then you need to convert that to a date first or just parse it a different way.
select
convert(varchar(8),cast('1/24/2018 10:34:23 PM' as datetime),112)
,format(cast('1/24/2018 10:34:23 PM' as datetime),'yyyyMMdd')
I usually refer to a question already answered on Stack Overflow in the below link.
How to convert DateTime to VarChar
The answer by Colin really works for me and a source of reference eveytime I have confusions around datetimes and formats in SQL Server.

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)

Convert from varchar into date in SQL Server

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

Converting string to date in sql

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

How to convert varchar to date

I am working in SQL Server 2008 R2
I need to convert a varchar(50) field in a view to a date format.
I have a view that uses the following to create the field delivered_date:
convert(varchar(50),[delvd_dt],110) as [delivered_date]
The formatted field looks like : 2012-03-11 16:24:42.0000000
I need the results to be a date so that I can check for dates within a range. I would prefer to do it within the view so that I can use SSRS to create the range for the report.
Have you tried cast()? The following returns the right date for me:
select CAST('2012-03-11 16:24:42.0000000' as DATE)
Perhaps you can simply truncate the millis and use CONVERT:
SELECT Convert(datetime, LEFT('2012-03-11 16:24:42.0000000', 19), 102) AS [Date]
Demo
This works even in MS SQL-Server 2005.
CAST and CONVERT (Transact-SQL)
Try this link website shows several formatting options.
Example:
SELECT CONVERT(VARCHAR(10), GETDATE(), 105)