Convert varchar to date to use it into a where DB2 - date

I am trying to compare a date and a varchar from a database. Imagine I have a table like this
CREATE TABLE Sample (
mydate VARCHAR(20),
iddummy INTEGER
)
What I need to do is:
Select mydate, iddummy from Sample Where mydate > '01-30-2016';
However, as mydate is a varchar(20), it does not work. So I tried this way
SELECT date(to_date(mydate, '01-25-2016')) as newdate, iddummy
FROM Sample
WHERE newdate < '01-30-2016'
Any ideas how to do that? I am getting Error Code -206

The "-206" is because an alias ("AS newdate") defined in the SELECT clause is not yet visible in the WHERE clause; when parsing a SELECT statement, the clauses are taken in this order: FROM is first, then WHERE, then GROUP BY & HAVING, then SELECT, finally ORDER BY.
You would have to repeat the definition of newdate in the WHERE clause, or better: use a CTE:
WITH dt AS (
SELECT date(to_date(mydate, 'MM-DD-YYYY')) as newdate, iddummy
FROM Sample
)
SELECT * FROM dt WHERE newdate < '01-30-2016'

You can try this
SELECT mydate as newdate, iddummy
FROM Sample
WHERE date(mydate)< '01-30-2016'
But this would throw you an "ERROR: An invalid datetime format was detected; that is, an invalid
string representation or value was specified" if your mydate contains a string which cannot be converted to date ex: '01-30-2016aa1232' etc

Try using this
SELECT DATE(CHAR(trim(mydate),10) as newdate, iddummy
FROM Sample
WHERE DATE(CHAR(trim(mydate),10) < '2016-01-30'

Error code 206 states that "Column does not exist in any table of the SELECT". May be
SELECT mydate(to_date(mydate, '01-25-2016')) as newdate, iddummy
FROM Sample
WHERE newdate < '01-30-2016'

Related

BigQuery conditional date formatting based on the value

I need some help around BigQuery date formatting based on their values. The source data has STRING datatype and target datatype is DATE. I need to format the input dates based on their values as follow:
NULL value should remain NULL
Empty string("") should be convert to NULL
Date with YYYY-MM-DD format should remain as is
Date with MM/DD/YYYY format should be convert to YYYY-MM-DD format
Here's what I have done so far:
SELECT input_date, CASE WHEN input_date = '' THEN NULL ELSE PARSE_DATE('%m/%d/%Y', input_date) END AS output_date FROM mytable
The above case statement fails when try to parse the dates with YYYY-MM-DD format. Here's the error I am getting:
How do I solve for the YYYY-MM-DD date format? Any feedback is appreciated.
You may try and consider below approach wherein you will need to add another WHEN and then use regex to match the YYYY-MM-DD string and then parse it to your desired date format as shown below.
with sample_data as (
select NULL as input_date,
union all select '' as input_date,
union all select '2022-05-21' as input_date,
union all select '05/25/2022' as input_date
)
SELECT input_date,
CASE WHEN input_date = '' THEN NULL
WHEN REGEXP_CONTAINS(input_date, r'^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$') THEN PARSE_DATE('%F', input_date)
ELSE PARSE_DATE('%m/%d/%Y', input_date) END AS output_date FROM sample_data
Output:
Consider below query:
WITH sample_data AS (
SELECT * FROM UNNEST(['', null, '2022-05-21', '05/25/2022']) input_date
)
SELECT *,
COALESCE(SAFE.PARSE_DATE('%m/%d/%Y', input_date), SAFE.PARSE_DATE('%F', input_date)) output_date
FROM sample_data;
Output results

Converting Integer values to Date in Presto SQL

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

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 ;

sqlite date comparison

I have column of type date time and values are getting stored in format 10-29-2011 08:25.
I would like to find out the rows only which are less then current date-time. What will be the condition for date comparison for current date and this date-time column field?
Thanks.
you could use the datetime function
SELECT * FROM mytable
WHERE mydate > datetime('now')
you can even make date operations
SELECT * FROM mytable
WHERE mydate > datetime('now','-15 days')