strip 0 from all rows in single column MySQL - sql

I want to TRIM(LEADING 0 FROM month) FROM table1
where table1 has column month with data formatted like so: 01, 02, 03
I would like to update the data so it is formatted as: 1, 2, 3, ...
Thanks!

Simplest way might be to just cast it to an int:
SELECT CAST(month as int) from table

From the MySQL docs:
mysql> SELECT MONTH('2008-02-03');
-> 2
So, if you only have the month (and not the rest of the date), you could use:
SELECT MONTH(CONCAT('2010-', month, '-', '01')) FROM table1;
Source: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_month

Related

Extracting data from only the year

I have data in a table in SQL with dates, but how do I select only those that happen in 2021. (The dates look like 31-oct-2020) in the table. The dates are the actual date variable, not just text.
You should avoid storing your dates as text, but rather should use a proper date column. That being said, you may check the right 4 characters of the date string:
SELECT *
FROM yourTable
WHERE RIGHT(date_col, 4) = '2021';
If the column be an actual date type then use:
SELECT *
FROM yourTable
WHERE date_col >= '2021-01-01' AND date_col < '2022-01-01';
I suspect that your DB is Oracle after checking out your previous post. Then you can use
SELECT *
FROM yourTable
WHERE EXTRACT(year FROM dt) = 2021
or
SELECT *
FROM yourTable
WHERE TRUNC(dt,'YYYY') = date'2021-01-01'
or
SELECT *
FROM yourTable
WHERE dt BETWEEN date'2021-01-01' AND date'2021-12-31'
You can benefit the index if there's one on the date column(namely dt) by using the last SELECT statement
If using MSSQL, you can leverage the YEAR(...) to extract the year from a date.
Replace and , with the table name and date column name respectively.
select * from <tablename> where year(<datecolumn>) = 2021

Using CONCAT in combination with SUBSTR

I want to write a select that would return me a distinct years only (2018, 2017,2016).
I have column AS_OF_DATE in table HISTORY.
Here are some example values of AS_OF_DATE:
31-05-18,
31-04-17,
31-07-16,
...
This is what I tried, but it doesn't work:
SELECT CONCAT('20',DISTINCT SUBSTR(AS_OF_DATE, 7, 2) FROM HISTORY
I used CONCAT to add 20 in front of the result and SUBSTR that would start at the 7th string and would be 2 strings long (so I get 18,17,16...)
try like below
SELECT DISTINCT '20' || SUBSTR(AS_OF_DATE, 7, 2) FROM HISTORY
Normally, you would do this with extract() or to_char():
select extract(year from as_of_date) as yyyy
or
select to_char(as_of_date, 'YYYY') as yyyy
This assumes that as_of_date is a date, which is should be.
You can add select distinct if you want a result set with the distinct years.
You can use
SELECT CONCAT('20', SUBSTR(AS_OF_DATE, -2) ) as "Years"
FROM HISTORY
GROUP BY SUBSTR(AS_OF_DATE, -2)
ORDER BY "Years"
Demo
this will work:
select distinct extract(year from as_of_date) from History;
the extract function takes off the as_of_date provided it is a date datatype and distinct select only one for dates which are multiple times in the extract.

Fiscal Period Name from varchar column

I fairly new to SQL and learning so much from this forum. Thank All!
Have another one for which I seek expert advice.
I have a column which basically is fiscal period. However, its not a date column, but instead of type varchar.
Intention is to manipulate the column value to get the name of the fiscal month out of it.
So in the example below, I have column Fiscal_Period available and I need column FiscalMonth.
Fiscal_Period FiscalMonth
----------------------------
2018001 Jan
2018002 Feb
2018003 Mar
2018004 Apr
2018005 May
2018006 Jun
2018007 Jul
2018008 Aug
2018009 Sep
2018010 Oct
2018011 Nov
2018012 Dec
Is there a straightforward way of achieving this then the way I am then trying?
My current hack approach is:
Convert Fiscal_Period column filed into YYYY-DD-MM format by using substring and concat function
Use Datetime to convert into date and then extract month out of it
Use Month and Datename function
My query:
SELECT
DATENAME(MONTH, (CONVERT(DATETIME, (CONCAT(SUBSTRING(FISCAL_PERIOD, 1, 4), '-', SUBSTRING(FISCAL_PERIOD, 6, LEN(FISCAL_PERIOD)), '-', '11'))))) AS PeriodName
FROM
table 1
Cheers!
this is slightly shorter
datename(month, stuff(FISCAL_PERIOD, 5, 1, '-') + '-01')
if you only wanted the abbreviated month name then apply LEFT ( ) on the above expression
As you only need a month name neither the year or day need to vary, hence you can use constants for those. In T-SQL a literal that equates to YYYYMMDD is safe to use as a date, so:
select datename(month,'2018' + right(fiscal_period,2) + '01')
This is basically the answers already given, just figured id finish typing it. It relies on filling in a problem with the date format as given, but otherwise it works the same.
;with src (dt) as
(
select convert(date, '20180101', 120) union all
select '20180201' union all
select '20180301' union all
select '20180401' union all
select '20180501' union all
select '20180601' union all
select '20180701' union all
select '20180801' union all
select '20180901' union all
select '20181001' union all
select '20181101' union all
select '20181201'
)
select
Dt,
left(datename(month, dt), 3)
from src

In SQL how to return records matching date and month only (ignore year)

Using SQL, I want to return all records where the date is between 1st March and 31st June (for example), but the records should cover all years. Is there a simple way I can achieve this?
Here is what you would do if you are using PL/SQL or oracle SQL+
SELECT * FROM table
WHERE TO_CHAR(MONTH_COLUMN,'MM/DD') = '06/21'
this will give you all the rows that have a date of June 21 regardless of the year.
For SQL Server use:
select *
from table
where month(dtgCol) between 3 and 6
Use the date functions to get the Month and the Day of Month from the date field and use in the where clause.
Depending on your DB, the function names may vary. But it will be in general like
SELECT * FROM table
WHERE Month(dateField) = 6
AND (DayOfMonth(dateField) >= 1 AND DayOfMonth(dateField) <= 30)
in SQL Server:
SELECT * FROM table
WHERE Month(dateField) = 6
AND (Day(dateField) >= 1 AND Day(dateField) <= 30)
Try this, definitely work
SELECT *
FROM Table
WHERE Month(DateColumn) IN (3, 4, 5, 6)
For SQL Server I'll use following.
eg:between 1st March and 31st June
select * from (
select *,DATEFROMPARTS(2011,MONTH(CreateDate),DAY(CreateDate)) as dt from tblAction
) as x
where x.dt between
DATEFROMPARTS(2011,3,1) and
DATEFROMPARTS(2011,6,31)
See if it helps..:)

sort distinct date column

I need distinct year and month from date column which would be sorted by same column.
I have date coulmn with values like (YYYY/MM/DD)
2007/11/7
2007/1/8
2007/11/4
2007/12/3
2008/10/4
2009/11/5
2008/5/16
after having query, it should be
2007/1/1
2007/11/1
2007/12/1
2008/5/1
2008/10/1
2009/11/1
This doesn't seems to be working
SELECT distinct (cast(year(datecol) as nvarchar(20) ) +
'/'+ cast(month(datecol) as nvarchar(20) ) + '/1') as dt1
FROM Table
ORDER BY dt1
Soemthing like this would work on MS SQL Server:
select
distinct
dateadd(day, -1 * DAY(datefield) + 1, datefield)
From
datetable
order by
dateadd(day, -1 * DAY(datefield) + 1, datefield)
The DATEADD function call basically subtracts (day-1) DAYS from the current date --> you always get the first of whatever month that date is in.
Sort by it and you're done! :-)
ADditionally, you could also add this functionality to your table as a "computed column" and then use that for easy acccess:
alter table yourTable
add FirstOfMonth As DATEADD(day, -1 * DAY(datefield) + 1, datefield) persisted
Then your query would be even simpler:
SELECT DISTINCT FirstOfMonth
FROM YourTable
ORDER BY FirstOfMonth
Marc
When dealing with dates in SqlServer avoid using cast like this - the resulting format will change depending on server config.
Instead use convert and choose a format (for instance 112) that adds leading zeros to the month.
Anil,
Do you also have time part in the dates ? What dataType are you using for the column ? Are you
using DateTime dataType or Char ?
This works for me
SELECT DISTINCT (DateField) AS Date FROM DateTable ORDER BY 1