How to select only year from a Date Time TSQL - sql

I'm right now creating View and I need to display only the YEAR from a normal DateTime
How can I do that to return just a Year as a value from ?
I have already tried it with EXTRACT but somehow it not working ..
Thanks for help and fast answer

Use YEAR() function
SELECT YEAR(MyDateCol) FROM MyTable
See this SQLFiddle

You can use DATEPART() to extract the year from datetime value.
DATEPART(YEAR,GETDATE());
Check out SQLFIDDLE

Year function
SELECT YEAR(getdate())
SELECT YEAR(columnname) from yourtablename

Related

SQL - convert yyyy-MM-ddThh:mm:ssZ to simple date with no time

I need to convert date/time with this format yyyy-MM-ddThh:mm:ssZ to simple date with no time.
Example : 2020-10-21T07:28:48.021Z
to : 2020-10-21
CAST(XX AS TIMESTAMP) works fine, but it doesn't drop the time in my sql query as i would like.
Any idea ? Thanks !
Below is for BigQuery Standard SQL
select date('2020-10-21T07:28:48.021Z')
with output
cast(field as date) OR try_cast( field as date)
/*
Try cast will not return error if it is not convertable it will return null*/
It's done differently in different databases, but in DB2, it's super simple:
select date(thatTimestamp)
from sysibm.sysdummy1;
HTH !
you want cast( 2020-10-21T07:28:48.021Z as date)
or
select cast(dateField as date) as [DateFieldName]

SQL: How To Select Earliest Date

I have a date column & I am simply trying to know the earliest date. I use the command:
select Min(Install_date) From PocketGemsSchema.pocketgemstable2;
This returns 1-Dec-17
But the minimum date from my sample data is actually 1-Nov-17.
Can anyone help please?
Try this:
If your Install_date contain datatype varchar than
SELECT MIN(CAST(Install_date AS DATE))
FROM PocketGemsSchema.pocketgemstable2
SELECT FORMAT(MIN(CAST(Install_dateAS DATE)), 'dd-MMM-yy ')
FROM PocketGemsSchema.pocketgemstable2
If your Install_date contain datatype date or datetime than your query will work
I think its the data type issue , you can try two approach
convert the field to datatime and your query should work
cast it on the run time like below
Mysql
SELECT Min(Str_to_date(Install_date, '%m/%d/%Y'))
FROM pocketgemsschema.pocketgemstable2;
SQL server
SELECT Min(Cast(Install_date as datetime))
FROM pocketgemsschema.pocketgemstable2;
I would change the column to a date or a datetime type and sort out any bugs that arise.

Last day of month with H2

How can I get the last day of a month in H2 SQL? In MySQL the following would work:
SELECT LAST_DAY(GETDATE())
Thank you.
EDIT:
Ended up with using the following:
SELECT TIMESTAMPADD(DAY, -DAY(TIMESTAMPADD(MONTH,1,GETDATE())), TIMESTAMPADD(MONTH,1,GETDATE()));
The reason for this is that it also support MySQL. Just replaced the functions from Vijaykumar's answer.
please try :
SELECT DATEADD(dd, -DAY(DATEADD(m,1,#Today)), DATEADD(m,1,#Today))
SELECT day(dateadd(dd,-day(ym_next),ym_next)) last_day_of_month
FROM (SELECT DATEADD(m,1,ym) ym_next
FROM (SELECT parsedatetime(concat(2016,'-',2,'-1'),'yyyy-MM-dd') ym)ym)ym

Getting only day and month from a date field

I have a set of dates that are in the format DD-MMM-YYYY. I need to be able to compare dates by using only the DD-MMM part of the date, since the year isn't important.
How would I achieve this?
I have tried reading up on the DATEPART function (edit: which evidently wouldn't work) but I can only theoretically get that to return either the DD or the MMM parts, not both of them at once.
Edit: added oracle tag. Sorry.
Example of date field: 01-MAR-1994
If your column is of type DATE then it doesn't have a format.
If I understand you right, then you want to view the mon-dd part only, so you need to convert it with TO_CHAR function,
i.e.:
select to_char(your_date_column, 'mon-dd') from your_table
Convert your dates using the following format, it will only month and the date part. You have to replace getdate() with you date fields.:
select convert(varchar(5),getdate(),110)
Assuming that you are using SQL Server or Oracle since you attempted using DATEPART, you can just get the day and month using the DAY() and MONTH() functions. Assuming, again, that the dates you are comparing are in two different tables, it would look similar to this:
SELECT MONTH(t1.date), DAY(t2.date)
FROM table AS t1
INNER JOIN table2 AS t2
ON t1.key = t2.key
WHERE MONTH(t1.date) = MONTH(t2.date)
AND DAY(t1.date) = DAY(t2.date)
EDIT: If you are just comparing rows in the same table, you only need a very simple query.
SQLFiddle
select id, TO_CHAR(most_recent, 'mon-dd')
from (
select id, MAX(date1) AS most_recent
from table1
group by id
)
You can also combine month and day into one integer:
EXTRACT(MONTH FROM datecol) * 100 + EXTRACT(DAY FROM datecol) AS MonthDay
Then it's easier to sort and compare.
select FORMAT(yourcoulmn_name, 'dd/MM') from table
This should do the trick
`select CONVERT(varchar(7),datetime_column,100) from your_table`
date_default_timezone_set("Asia/Kolkata");
$m = date("m");//Month
$d = date("d");//Day
$sql = "SELECT * FROM contactdata WHERE MONTH(date) = '$m' AND DAY(date) = '$d' ";
only checks day and month and returns today, day and month from database
SELECT LEFT(REPLACE(CONVERT(varchar(10),GETDATE()-1,3),'/',''),4)
WOuld this work for you?
FROMAT(DATETIME, 'dd-MMM') = FROMAT(DATETIME, 'dd-MMM') use any format you want

How to extract the YEAR in SQL Server 2008?

SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+0,0))
The output give me the Last Date of the Previous Month of the current date.
How can I extract Year from DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+0,0))?
Select DATEPART(YYYY,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+0,0)))
Will the above Select statement extract the Year?
The answer to your question is "yes, you can use datepart(yyyy, ....). It turns out that "yyyy" is a synonym for the more commonly used "year" in this context.
However, most people would just use year(<whatever>) for this purpose.
SQL Server has a function YEAR() so to extract year from DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+0,0))
just do SELECT YEAR(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+0,0)))
http://sqlfiddle.com/#!3/1fa93/5208