Converting Integer values to Date in Presto SQL - date

Below is a script i am trying to run in Presto; Subtracting today's date from an integer field I am attempting to convert to date. To get the exacts days between. Unfortunately, it seems the highlighted block does not always convert the date correctly and my final answer is not correct. Please does anyone know another way around this or a standard method on presto of converting integer values to date.
Interger value in the column is in the format '20191123' for year-month-date
select ms, activ_dt, current_date, date_diff('day',act_dt,current_date) from
(
select ms,activ_dt, **CAST(parse_datetime(CAST(activ_dt AS varchar), 'YYYYMMDD') AS date) as act_dt**, nov19
from h.A_Subs_1 where msisdn_key=23480320012
) limit 19

You can convert "date as a number" (eg. 20180527 for May 27, 2018) using the following:
cast to varchar
parse_datetime with appropriate format
cast to date (since parse_datetime returns a timestamp)
Example:
presto> SELECT CAST(parse_datetime(CAST(20180527 AS varchar), 'yyyyMMdd') AS date);
_col0
------------
2018-05-27

You can use below sample query for your requirement:
select date_diff('day', date_parse('20191209', '%Y%m%d'), current_timestamp);

Related

Converting Varchar() to Date?

Querying a table and one of the filters I want to use is a date between i.e. 02/08/19 and 02/10/19. For whatever reason the table I was working with for this filter has a Varchar(50) column made to look like a DateTime 120 style (yyyy-dd-mm hh:mm:ss.fff). How do I convert this to a Date (mm/dd/yy) format? If it were a real DateTime I would typically just cast it As Date.
So far I've tried:
select convert(varchar(50), a.alpha, 1), a.bravo, sum(a.charlie)
from table a
where convert(varchar(50), a.alpha, 1) between '02/08/19' and '02/10/19'
group by convert(varchar(50), a.alpha, 1), a.bravo
order by sum(a.charlie)
This doesn't seem to pull any records. I then switched it to 120 instead of 1 and switched the filters to '2019-02-08 00:00:00.000' and '2019-02-10 00:00:00.000' and it worked, but this isn't the format I need. I tried casting these converts as date i.e. cast(convert(varchar(50), a.alpha, 1) as date) and that didn't work either.
What should I do now?
Thanks,
You're trying to convert a varchar to a varchar while you should do it to a date. Even in that case, conversion will fail so you can do the conversion twice to let it filter with no error. This should work for you:
SELECT CONVERT(CONVERT(date, a.alpha),1), a.bravo, sum(a.charlie)
FROM table a
WHERE CONVERT(CONVERT(date, a.alpha),1) BETWEEN '02/08/19' and '02/10/19'
GROUP BY convert(convert(date, a.alpha),1) , a.bravo
ORDER BY sum(a.charlie)

Converting date format in denodo database

I'm trying to convert value for DIM_DT_ID to MMddYY. I'm successful in doinf that. However, query fails because ultimately I'm comparing a character value to date here. Is there a way by which I can get value for DIM_DT_ID in MMddyy format and its data type still remains DATE ?
Here DIM_DT_ID
SELECT DIM_DT_ID
DIM_DT_ID >= FORMATDATE('MMddyy',ADDDAY(TO_date('yyyy-MM-dd','2016-12-21'), -25)); from abc;
Regards,
Ajay
In Denodo, to convert a string to a date field, use "to_date()" (which returns a date).
Then, don't convert back to a string, leave that field as a date (so don't use "Formatdate()", which returns a string).
So:
SELECT *
FROM MyTable
WHERE now() >= to_date('yyyy-MM-dd',myStringFieldThatLooksLikeADate)
In my example, "now()" is a date, and so is the output of the to_date function... so you can do a comparison.
If you try to convert the date back to a string using formatdate, it won't work:
#This doesn't work:
SELECT *
FROM MyTable
WHERE now() >= formatdate('MMddyy',to_date('yyyy-MM-dd',myStringFieldThatLooksLikeADate))
It doesn't work because we are comparing a date ("now()") to a string.

Correct Syntax for a TSQL query to Compare Dates

I am looking for the correct syntax to test in a TSQL WHERE clause if a
datetime2(7) type is equal to another.
WHERE (CAST(modifiedDate AS DATETIME) = '9/29/2016 3:24:24 PM')
I also tried
WHERE (CAST(modifiedDate AS DATETIME) LIKE '9/29/2016 3:24:24 PM')
And
WHERE (CAST(modifiedDate AS datetime2) = CAST('09/29/2016 3:24:24 PM' AS datetime2))
I believe I have the right hand side stated incorrectly, but that is the exact value in the database.
I am looking for all records that match that datetimestamp.
To be clear I did try to search for other results..'
I thought this was a little flaky for a search result on this site.
DB field type...
To compare DATETIME values, you need DATETIME values to compare. You can use the TSQL CONVERT function to convert a string to the DATETIME datatype. For example:
CONVERT(DATETIME, '2016-09-28 15:34:00', 20)
Note that the third argument is the "style". Example above uses style 20, the ODBC Canonical style YYYY-MM-DD HH:MI:SS (24 hour clock). There are several other styles available, maybe you find one that matches your string format. (If you can't find a match, then you will need to use some string manipulation functions to reformat your string into a format where a style is available.)
As the second argument, you can use a string literal (as shown in the example above), or you can use a reference to a CHAR or VARCHAR column.
Reference: CAST and CONVERT (Transact-SQL)
On a different, but related, note: Why are date time values being stored as strings in the database, and not DATETIME datatype?
If the datatype of the column is DATETIME2(7), then I think you would want to compare DATETIME2(7) datatypes.
If we do an "equal to" comparison, that's going to be an exact match, including the fractional seconds. If you want to match DATETIME2(7) values from a given second, you could use a range comparison:
WHERE t.my_col_datetime2_7 >= '2016-09-29 15:24:24'
AND t.my_col_datetime2_7 < '2016-09-29 15:24:25'
Note the format of the string literals allowed for comparison to DATETIME2 are YYYY-MM-DD HH:MI:SS (24 hour clock) with optional fractional seconds .nnnnnnn.
Reference: Supported String Literal Formats for datetime2
Not sure where you are having issues. I guess it depends how you are storing your date value. If it's datetime2, this works fine.
DECLARE #testtable TABLE(
ID INT
, modifieddate DATETIME2)
INSERT INTO #testtable
(id, modifieddate)
VALUES
(1, '9/29/2016 3:24:24 PM'),
(2, '2/01/2016 3:24:24 PM'),
(3, '6/25/2016 3:24:24 PM')
SELECT *
FROM #testtable
SELECT *
FROM #testtable
WHERE CAST(modifieddate AS DATETIME2) = '09/29/2016 3:24:24 PM'
SELECT *
FROM #testtable
WHERE modifieddate = '9/29/2016 3:24:24 PM'
SELECT *
FROM #testtable
WHERE modifieddate = CAST('09/29/2016 3:24:24 PM' AS DATETIME2)

Crystal Report-Convert date string (with day of week) to date format

I'm new to crystal report. I have a date in string format like 2015-03-25 (Wed) and I want to convert it to date format like 03/25/2015. I tried with CDate and DateValue but it returned bad date string format. Any suggestions to convert such date string to proper date format?
If you have a DateTime field in Crystal Reports, you will see Date and Time tab option on the Format Editor when you right click on the field and select Format Field menu item. From the Date and Time tab, you may select the desired format and select OK.
It would be recommended to use the formats you want to use.
For eg : if you are giving string format for money or decimal you may not be able to use it at its full,like you may not be able to auto sum and other properties related to the datatype you intend to use
Not to do any thing in the code, Crystal Report have facility to this type of simple format.
#utility, you are near to answer.
As above image, in last Custom Format option, where you just go in Date tab and give format as
http://www.c-sharpcorner.com/UploadFile/mahesh/DateFormatInCR06132007092248AM/DateFormatInCR.aspx
Updated : sorry for above answer, that will work if you have valid date string.
In your case, where any arbitrary string need to convert into other date format. There is 2 option. In both case you have to extract the date and then format as you need and again combined with other sub-string.
Second you already done ie. crsytal report side, grab the date , format it and concatenate. this will slow down as need to process for each row.
SqlServer side - This option is faster from first option.
declare #t nvarchar(16) = '2015-03-25 (Wed)'
--get the acual date select SUBSTRING ( #t, 1, charindex('(' , #t ) -1 )
--above result give the charter datatype, so you first convert into date and then convert into other format select cast( SUBSTRING ( #t,
1, charindex('(' , #t ) -1 ) as date) --convert into date select
convert (varchar(15) , cast( SUBSTRING ( #t, 1, charindex('(' , #t )
-1 ) as date) , 103) --convert into dd/mm/yyyy format
--Above is for your understand, this is the actual execution of your code (Only write the below line) select convert (varchar(15) , cast(
SUBSTRING ( #t, 1, charindex('(' , #t ) -1 ) as date) , 103) + ' ' +
datename(dw, getdate() )
I suggest, go with Sqlserver side.

RedShift: How to cast integer with year into a date?

Assume a user tries to cast a number (year of birth) into a date.
select year_of_birth, cast(year_of_birth as date) from visit_occurrence
And gets this error :
cannot cast type smallint to date
What is the proper way?
e.g., cast(cast(YOB as string)+'-01-01' as date) does not work either.
use
select year_of_birth, to_date(cast(year_of_birth as text), 'YYYY') from visit_occurrence ;