Extract the year from the given date? - mysql

I have a date field create_at in Y-m-d format in my table. I want to extract only the year from that field.
I tried YEAR(create_at) which gives result in comma separated value.
Eg.
2017-5-12 outputs 2,017. I need without the comma.

The YEAR() will provide you integer output only.
SELECT YEAR("2017-06-15");
2017
Ref https://www.w3schools.com/sql/trymysql.asp?filename=trysql_func_mysql_year

Related

Extract Numeric Date Value from short Date Format in SQL

I am trying to convert short date format to numeric date format in SQL.
Here are the dates I am trying to convert:
16-Mar-20
18-Mar-20
12-Mar-20
I have seen many methods to re-format dates based on numeric numbers but I am working with a dataset which has varied date-types. To ensure smoothness, I wanted to know what's the way to convert these numbers to numeric date. Note that even if I extract the day month and year individually I could multiply them with respective values to get the dates. Here's what I have done: (Although I have tried multiple things but this is the one which has yielded me the closest result, Note that my end goal is to convert the date into numeric value)
FROM_UNIXTIME(`Start Date`, 'dd-MMM-yy') AS 'Date Numeric'
Here Start Date is formatted in the way I have mentioned above. (14-Mar-20).
UNIX_TIMESTAMP is what you need, but first you have do transform your data to a Date
UNIX_TIMESTAMP(STR_TO_DATE(`Start Date`, '%d-%b-%y'))
With
SELECT UNIX_TIMESTAMP(STR_TO_DATE( "14-Mar-20", "%d-%b-%y"))
You get
UNIX_TIMESTAMP(STR_TO_DATE( "14-Mar-20", "%d-%b-%y"))
1584144000
If you want the Unix timestamp, use UNIX_TIMESTAMP():
UNIX_TIMESTAMP(STR_TO_DATE(`Start Date`, 'dd-MMM-yy'))

Convert String Field to Date value in a different format with an SQL select query

I have a table with a column start_date. Unfortunately, the datatype of the column is VARCHAR. Values in this column are either null or a string date in any format.
I have a requirement to extract the data from this table to a CSV file using BCP command. But while extracting, start_date value is expected to be in YYYYMMDD format in the output file.
The problem here is for some records, start_Date will be in DD/MM/YYYY format and for some others it may be in another format. For example : YYYY-MM-DD.
Is there any way so that I can convert the start_date value to the format YYYYMMDD irrespective of the actual format using the select query itself.
select convert(DATE,'13/12/2019',112) as 'AliasName'
This function will help u in fetching the data in the format of YYYYMMDD irrespective of how the format of ur input column is..This function uses 3 arguments 1.THE DATA TYPE WHICH U WANT TO CONVERT 2. YOUR INPUT COLUMN WHICH U WANT TO CONVERT 3.STYLE (style number is: Each format has its own style number for example if you give style number as 103 it will convert to dd/mm/yyyy or if 112 it will convert to YYYYMMDD ..there are many style You will find it if u search about convert function) . Hope this will help!
Hope this helps!!
select
*,
(CASE WHEN [Date] like '%/%' then TRY_CONVERT(datetime,[Date],103)
else [Date] END) as [New_DATE]
from TableName;

SSRS how to sort date in MM/YY format?

I'm stuck at this point, I get my data from a stored procedure with the date format as MM/YY, but SSRS sorted my date in a wrong way: 01/2019, 02/2019,..., 12/2019;01/2018, 02/2018,...
So I'd like to do to have my data in the right order.
Since your date is converted to MM/YYYY format, the data is text and not numerical so it's sorted one character at a time rather than by the value.
If you want to sort by year and then by month, you would need a separate SORT option for each that parses the text into separate month and year values.
=RIGHT(Fields!DOB.Value, 4)
This gets the 4 characters from the right of the text which is the year in the data.
=LEFT(Fields!DOB.Value, 2)
LEFT , 2 gets the first two characters of the string - the month in the field.
Create a new column in the query where you convert the string date to a date data-type and sort on that e.g.
select
convert(date, '01/'+ MyDateStringColumn) MyDateColumn
from MyTable
order by MyDateColumn

How can i do a count of two columns in mysql?

i want to do a count of two columns in mysql. One of the columns is a string but another is a date like 06/08/2017 and when i do my query i get 0 results.
SELECT count(*) FROM `castigos` WHERE inicio_normal=05/06/2017 AND cod_emplazamiento=1
I have entries of that data but its dont show me anything. Maybe the type of data in the date is wrong?
What should i do?
Add the date field to your select and group by it. Otherwise mysql extensions doesn't recognize you want to group by the date and will aggregrate all the results into 1 column. And since you are getting 0 count, you're where clause must not be working.
Your date format seems malformed. usually YYYY/MM/DD format (standard format);
or specify a format using SELECT STR_TO_DATE('17/09/2010','%d/%m/%Y');
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.
the below uses the implicit casting and default date format to convert the string date to a valid date.
SELECT inicio_normal, count(*)
FROM `castigos`
WHERE inicio_normal='2017/05/06'
AND cod_emplazamiento=1
GROUP BY inicio_normal
Otherwise its doing math and comparing that date to the number stored for the date.
Understand dates should be stored in a date datatype and when you query dates you're passing in a string that is being cast to a date datatype for comparison. So you need to use the standard format, or cast your string to a date so the db engine knows how to convert your format to a date.
Try this :
SELECT count(*) FROM `castigos` WHERE inicio_normal="05/06/2017" AND cod_emplazamiento=1 GROUP BY inicio_normal
WHERE inicio_normal=05/06/2017
If you divide 3 by 6 then by 2017 you get a very small value indeed. OTOH if you reformat this as a date (e.g. 20170605, if you gave us a European formatted date - dd/mm/yyyy) then your query will find the rows you showed us.

SQL Date_Format Month Number to Month Name

I have an issue in regards to trying to run a sql statement that returns the values of a column called month(of which I have defined as a varchar type, but only has integer values 1-12) as the associated month name. So, for example, the query would return a value of 1 as january. The issue I have is I am trying to use date_format
select date_format(month,'%M')from db.table name
but the values return as null. I was informed that the month values have to be a 'date' type in order for date_format to work. However, the values in this column 'month' are simply integers. So I run into the issue of not being able to assign the date type to the month columns because they're just integers and not correct format for dates? How could I take these single integers and return the month then?
Syntax
DATE_FORMAT(date,format)
Requires date as first param
Check out MySQL date function here:
http://www.w3schools.com/sql/sql_dates.asp
For this you can use this,
SELECT col as MonthNumber,
MONTHNAME(STR_TO_DATE(col, '%m')) as MonthName
FROM table_name
WHERE col <= 12