How would I make a single date field using these two fields?
So if I had a table with this:
MONTH YEAR
2 2013
4 2012
2 2012
How would I combine the month and the year to make a field in that looks like this...
DATE
2013-02
2012-04
2012-02
In Oracle, I would just use concatenation and lpad():
select (year || '-' || lpad(month, 2, '0') ) as date
Related
base_table
month year
5 2021
10 2020
I want to combine the "month" and "year" column into a proper date column.
month_year
2021-05-01
2020-10-01
This seems to be a duplicate question for this:
Concatenate in PostgreSQL
I tried both:
to_date(CONCAT(year::text, '/'::text, month::text), 'YYYY/MM') as month_year
and
to_date(CONCAT(year, '/', month), 'YYYY/MM') as month_year
but maybe the solution does not work in Redshift.
Try:
(year::text || '-' || month::text || '-01')::date
This will use ISO format (2021-11-19) for the date.
I have a database table which is named as table and there is a column Col which is present in table with datatype varchar. Column Col contains dates in the format MMM-YY.
For example Col has values as :
Col
DEC-21
NOV-21
SEP-20
OCT-19
DEC-21
As, we can see data can be duplicated like DEC-21. I want to extract last 6 months data based on the recent month present in Col. For example, If the DEC-21 is the most recent date(consider, day is not present) and so, I want data from DEC-21 to JUN-21 i.e. 12-21 to 06-21 if we map DEC to 12 and JUN to 06. This Table has many columns and one of the columns is Col which I mentioned above and I have to extract data based on the column Col by using SQL query.
I have written a query as:
SELECT *
FROM table
WHERE CAST(RIGHT(Col,4) AS INT) Between 2020 and 2021
But here I get data between 2020 and 2021. So, By doing some modification in the above query or Is there any other way to get the past 6 months data from the recent date which is in MMM-YYYY format from Col column.
I was writing code in R and I was using dbGetQuery() where I have to pass the SQL query. I have already done this thing after storing it in a dataframe but is there any way to do it directly by sql query ?
Any help would be appreciated.
with data as (
select *,
convert(date, '01-' + dt, 105) as converted_dt,
max(convert(date, '01-' + dt, 105)) over () as last_converted_dt
from T
)
select * from data
where converted_dt >= dateadd(month, -6, last_converted_dt);
The 105 comes from the list of date formats which can be found in the documentation for cast/convert. SQL Server can convert strings like Apr 2021 so a cast like below might also work (if you actually have four-digit years) but it's best to be explicit about the format as I did above.
cast(replace(dt, '-', ' ' as date)
Something like this should work.
SELECT * FROM table where CONVERT(DATE,'01-'+Col) BETWEEN '01-Jun-2021' and '31-Dec-2021'
I'm super new to athena, so bear with me. I have data stored as integers in three separate columns for year, month and day, as such:
year month day
2020 7 10
2020 7 11
2020 7 12
I'd like to turn these three fields into one date. How do I do that?
Thanks in advance!
One method is:
select date_parse(cast(year * 10000 + month * 100 + day as varchar(255)), '%Y%m%d')
This should also work:
select date(year || '-' || month || '-' || day)
You have to use the concat() function. You can see the documentation here.
Depending of the format that you want to use, this can change.
concat(year, '-' , month , '-', day)
I want to filter values from a table, between two weeks, like this:
select * from SalesWeekly
where SalesWeek BETWEEN '50' and '02'
Problem is, i have no idea how to specify week 50 is from year 2019,
and week 02 is from year 2020.
Assuming that you are storing the year in your table, say in table SalesYear, you could concatenate it with the week number and do string comparisons:
select *
from SalesWeekly
where SalesYear || '-' || SalesWeek BETWEEN '2019-50' and '2020-02'
For this to work, SalesWeek must be a 2-characters long string, left padded with 0 (so the 1st week should be '01', not '1').
This question already has answers here:
Convert month name to month number in SQL Server
(14 answers)
Closed 9 years ago.
A table exists which stores month name in the form of string..
I want to order by year and month so as to get proper result.
year(string) Month(string) data
------------ ------------ ----
2012 August bigbox
2012 December samllbox
2013 April samll box
2012 September larg
I want to order by year and month. as in 2012,2013...
Jan,feb,march....
I am aware of the method of
case statement when
Month = 'january' THEN 1
Month - FEB THEN 2
But i do'nt want to use this as the procedure will be too big..
Your best option is to use the proper date type. Otherwise, create a table (inline or physical) to map your string months.
SELECT 1 AS month, 'January' AS strMonth
UNION ALL
SELECT 2, 'February'
UNION ALL
SELECT 3, 'March'
...
SELECT 12, 'December'
Then map this your table. See a demo
SELECT *
FROM TABLE
ORDER BY [year] DESC,
DATEPART(month,[Month] + ' 01 ' + CONVERT(VARCHAR(4),[YEAR]))
The above code will give you what you want , but i would strongly suggest you reconsider your design.
Right now you are reserving a string type field which would be at least 15 characters long. This field does not have any value than for display reasons. You could have a DATETIME field that would be much easier to short by (not having to do calculations there) and if you would like to display the name of the month you could use:
DATENAME ( month, DateField )