Hi I have a date format in one table in Text 'YYYY/MM'. example 2018/01, 2020/08 etc.
I need to join it with another table where the date is in Number type( and DATETIME20 format attached it it) and convert it to month and compare.
Is there any way to do it in PROC SQL as the rest of my query is in PROC SQL?
eg. Table 1: Month= 2018/01; Table 2: Date =20.01.2018 10:48:17 . They should be joined in the PROC SQL query.
I would also like to calculate difference in Months between these two dates.
Thank you in advance.
Convert both to the same DATE value. To convert a datetime value to a date use the DATEPART() function. To move to the first day of the month use the INTNX() function with the month interval. To convert a string like '2018/01' to a date you could use INPUT() function with YYMMDD informat by appending '/01'.
proc sql ;
create table want as
select *
from table1,table2
where input(cats(table1.month,'/01'),yymmdd10.)=intnx('month',datepart(table2.date),0)
;
quit;
Related
I have column 'jobstarttimeiso' and I want to create another column for Weeks of the year based on the date. How would I go about doing that? I am using Redash to query from redshift database. Please help! Thank you.
The extract function will return a week number e.g.
select extract(week from jobstarttimeiso) as weeknumber
In general:
EXTRACT ( datepart FROM { TIMESTAMP 'literal' | timestamp } )
See: https://docs.aws.amazon.com/redshift/latest/dg/r_EXTRACT_function.html
alternatively you can also use to_char()
TO_CHAR (timestamp_expression | numeric_expression , 'format')
with the parameter IYYY as the format for ISO 8601 week-numbering year (4 or more digits)
I'm completely new to Hive SQL and I need to do the following.
I have a column which includes a date and what I would like to do is to create a new one which will be the Sunday before this date.
In xls I would write the following:
my_date-WEEKDAY(my_date,1)+1
and in sql:
DATEADD(DD, -(DATEPART(DW, my_date)-1), my_date)
I tried the following in Hive SQL:
DATE_SUB (my_date, date_format(my_date,'u')-1)
but date_format returns a string.
Any ideas?
Cast the result of date_format to int and do the arithmetic.
DATE_SUB(my_date,cast(date_format(my_date,'u') as int)%7)
I wanted to get the only the hour of the time and concatenate it with the date.
here's my query
SELECT distinct TOTALIZER_METER_READINGS.date + to_char(TOTALIZER_METER_READINGS.time ,'HH')
FROM TOTALIZER_METER_READINGS
is there any other way to get the hour of the time without turning it into text?
+ is the operator to add numbers, dates or intervals.
The string concatenation operator in SQL is ||.
As you are storing date and time in two columns rather then using a single timestamp column, I would convert them to a single timestamp value, then apply to_char() on the "complete" timestamp:
Adding a time to a date returns a timestamp that can then be formatted as you want:
SELECT distinct to_char(TOTALIZER_METER_READINGS.date + TOTALIZER_METER_READINGS.time, 'yyyy-mm-dd HH')
FROM TOTALIZER_METER_READINGS
You can use EXTRACT (or the date_part function):
SELECT EXTRACT(hour FROM current_timestamp);
The result type is double precision.
I am trying to convert a character varying date field to the format YYYYMMDD and below is the select query I tried. The output format is 1999-04-27 (YYYY-MM-DD). Admindate is the field name in the table.
select to_date( admindate,'MMDDYYYY') from test;
Can someone please advice what I am doing wrong?
Use to_char to convert the converted date into the desired format.
select to_char(to_date(admindate, 'MMDDYYYY'), 'YYYYMMDD')
from test;
you can try this
SELECT TO_CHAR(NOW() :: DATE, 'dd/mm/yyyy');
I have a column saved as a character data type. This column is what I am going to be using as a date. The column goes "YYYY-MM-DD" in that format.
This is a problem because if I ever need to filter by date, I have to go
select col_1, col_2
from table
where date LIKE '2016-04%;
If I want to search for a date range, this turns into a giant complicated mess.
What is the easiest way to convert this to a "date" data type? I want it to continue to be in YYYY-MM-DD order (no timestamp).
My ultimate goal is to be able to search for dates in a format like this:
select col_1, col_2
from table
where date between 2016-01-01 AND 2016-05-31;
What do you guys recommend? I am terrified I am going to corrupt my date if I use an alter statement to convert my data type. (I have a copy of the data saved and can upload it again, but it will take forever.)
Edit: This is a VERY Large table.
Edit Part 2: I originally stored the data as a varchar data type because my dates were not uploading correctly and I got an error message when I tried to save as a date data type. The every date in this column is in the "YYYY-MM-DD" order. My solution was to save it as varchar to avoid the error message (I couldn't figure out what was wrong. I even got rid of leading and trailing spaces.)
Storing a date as a varchar was the wrong choice to begin with. It's very good that you want to change that.
The first step is to convert the columns using an ALTER TABLE statement:
alter table the_table
ALTER COLUMN col_1 TYPE date using col_1::date,
ALTER COLUMN col_2 TYPE date using col_2::date;
Note that this will fail if you have any value in those columns that cannot be convert to a correct date. If you get that you need to first fix those invalid strings before you can change the data type.
I want it to continue to be in YYYY-MM-DD order
This is a misconception. A DATE (or timestamp) does not have a "format". Once it's stored as a date you can display it in any format you want.
My ultimate goal is to be able to search for dates in a format like this:
2016-01-01 is not a valid date literal, a proper (i.e. correctly typed) date constant can be specified e.g. using date '2016-01-01' (note the single quotes!
So your query becomes:
select col_1, col_2
from table
where col_1 between date '2016-01-01' AND date '2016-05-31';
If you have a lot of queries like that you should consider creating an index on the date columns.
Regarding the date constant format:
Are you telling me that despite having the varchar data types, I can still (as of right now) search between specific dates by just typing the word date and putting single quotes between two dates
No, that's not the case. SQL is a strongly typed language and as such will only compare values of the same type.
Using an ANSI date literal (or e.g. to_date()) results in a type constant (i.e. a value with a specific data type).
The difference between date '2016-01-01' and '2016-01-01' is the same as between42(a number) and'42'` (a string).
If you compare a string with a date, you are comparing apples and oranges and the database will do an implicit data type conversion from one type to the other. This is something that should be avoided at all costs.
If you do not want to change the table, you should use the query sagi provided which explicitly converts the strings to dates and then does the comparison on (real) date values (not strings)
You can use POSTGRES TO_DATE() cast function :
SELECT col_1,col_2
FROM Your_Table
WHERE to_date(date_col,'yyyy-mm-dd') between to_date('2016-05-31','yyyy-mm-dd') and to_date('2016-01-01','yyyy-mm-dd')
What #a_horse said.
Plus, if you can't change the data type for some odd reason, to_date() is a safe option to convert the column on the column, but there is no point to use the same expression for provided constants. So:
SELECT col_1, col_2
FROM tbl
WHERE to_date(date, 'YYYY-DD-MM') BETWEEN date '2016-05-31' AND date '2016-01-01';
Or just use string literals without type. The type date is deferred from the context in this expression. And you don't even need to_date(). Since you are using ISO format already. A plain cast is safe:
WHERE date::date BETWEEN '2016-05-31' AND '2016-01-01';
Be sure to use ISO 8601 format for all date strings, so they are unambiguous and valid with any locale.
You can even have an expression index to support the query. Match the actual expression used in queries:
CREATE INDEX tbl_date_idx ON tbl ((date::date)); -- parentheses required!
But I wouldn't use the basic type name date as identifier to begin with.