Extract Date in SQL - sql

I have a date format as : 2015-07-11T16:35:02.
I want only the date from the mentioned datetime.ie.(2015-07-11).
How would I convert the date.

Use convert to get result :
SELECT CONVERT(DATE,'2015-07-11T16:35:02',102)
For Comparison use below format :
IF CAST('2015-07-11T16:35:02' AS DATETIME) >= CAST('2015-07-11T16:35:02' AS
DATETIME)
SELECT CAST('2015-07-11T16:35:02' AS DATETIME)

You can use CONVERT :
SELECT CONVERT(DATE, '2015-07-11T16:35:02')
This will result in : 2015-07-11
For the column, you can use :
SELECT CONVERT(DATE, INSERT_DATE)
Hope this helps!!!

Do this. Just done Casting it to date datatype.
SELECT CAST('2015-07-11T16:35:02' AS DATE)
The result will be 2015-07-11

SELECT CONVERT(VARCHAR(10), '2015-07-11T16:35:02')
Checkout this link for more info: MSDN

SELECT CONVERT(varchar(10), INSERT_DATE,102)

Related

convert string to date and add days

I have a string in this format: yyyyMMdd
and i want to add a Year for every date in this format.
How can i do that?
For SQL Server it should be
select
DATEADD(day, your_number_of_day, convert(datetime, your_date_column, 102))
from
your_table

MMYYYY compare to YYYYMMDD

i have dates stored as chars in db (i know, but its not my db nor my idea..).
One of the dates is stored as MMYYYY (012016,022016...) and the second one is stored as YYYYMMDD (20160101,20160202...).
Is there a way to compare those dates? I need to take one date and select all of the second dates which are at least one year older then the first one...
Thank you for any help !
So for example i have Date1 field with values : 012014,012015,012016
and Date2 field with value: 20141005
And i need only Date1 which is at least one year older then Date2 so in this case it will return only 012016
I make two ctes to show how to work it out. I assume MMYYYY are stored as nvarchar or varchar and YYYYMMDD are the same type. If not - you will need one/few conversions on YYYYMMDD field.
;WITH cte AS (
SELECT '012016' as MMYYYY
UNION ALL
SELECT '022016'
), cte2 AS (
SELECT '20160101' AS YYYYMMDD
UNION ALL
SELECT '20160202'
)
SELECT *
FROM cte c
INNER JOIN cte2 c2
ON YYYYMMDD LIKE RIGHT(MMYYYY,4)+LEFT(MMYYYY,2) +'%'
Output:
MMYYYY YYYYMMDD
012016 20160101
022016 20160202
EDIT:
To find out differences in years use this:
DATEDIFF(year,CAST(YYYYMMDD as date), CAST(RIGHT(MMYYYY,4)+LEFT(MMYYYY,2)+'01' as date))
You can convert both fields into type DATE, then compare them.
For MMYYYY, you can use the function DATEFROMPARTS() to build a date. For example, convert 012014 into date:
-- DATEFROMPARTS(year, month, day)
DATEFROMPARTS(RIGHT('012014', 4), LEFT('012014', 2), 1)
For YYYYMMDD, it is easier, because this is the format ISO with Time Style 112. You can use the CONVERT() function to do the conversion :
CONVERT(DATE, '20141005', 112)
And here's how the final query looks like :
WITH casted AS (
SELECT
DATEFROMPARTS(RIGHT(date1, 4), LEFT(date1, 2), 1) AS d1,
CONVERT(DATE, date2, 112) AS d2,
-- ...
FROM yourTable
)
SELECT *
FROM casted
WHERE d1 <= DATEADD(YEAR, -1, d2)
try this
column1 is stored MMYYYY format and column2 is stored YYYYMMDD.
where column1= case when datepart(month,taskDuedate)>9 then convert(varchar,datepart(month,taskDuedate))else '0'+convert(varchar,datepart(month,taskDuedate))end+convert(varchar,datepart(year,taskDueDate))

DB2 Convert from YYYYMMDD to Date

I have a column that stores a date as char in the format 'YYYYMMDD'. Now I want to convert it to a real date.
I tried
select cast (DATEFIELD as DATE) as MyDate
But it only returns the old YYYYMMDD format labeled as 'DATE'. What am I doing wrong?
SELECT TIMESTAMP_FORMAT("DATEFIELD",'YYYYMMDD') as "MyDate"
Use the CONVERT function and the style 112 to get the output in YYYYMMDD
SELECT CONVERT(DATEFIELD, 112) as MyDate
FROM yourtable
Or style 100 for mon dd yyyy hh:mi
SELECT CONVERT(DATEFIELD, 100) as MyDate
FROM yourtable
Simply convert it.
SELECT TIMESTAMP_FORMAT("DATEFIELD",'YYYYMMDD') as MyDate
FROM <your_table>

How do I query for all dates greater than a certain date in SQL Server?

I'm trying:
SELECT *
FROM dbo.March2010 A
WHERE A.Date >= 2010-04-01;
A.Date looks like: 2010-03-04 00:00:00.000
However, this is not working.
Can anyone provide a reference for why?
select *
from dbo.March2010 A
where A.Date >= Convert(datetime, '2010-04-01' )
In your query, 2010-4-01 is treated as a mathematical expression, so in essence it read
select *
from dbo.March2010 A
where A.Date >= 2005;
(2010 minus 4 minus 1 is 2005
Converting it to a proper datetime, and using single quotes will fix this issue.)
Technically, the parser might allow you to get away with
select *
from dbo.March2010 A
where A.Date >= '2010-04-01'
it will do the conversion for you, but in my opinion it is less readable than explicitly converting to a DateTime for the maintenance programmer that will come after you.
Try enclosing your date into a character string.
select *
from dbo.March2010 A
where A.Date >= '2010-04-01';
We can use like below as well
SELECT *
FROM dbo.March2010 A
WHERE CAST(A.Date AS Date) >= '2017-03-22';
SELECT *
FROM dbo.March2010 A
WHERE CAST(A.Date AS Datetime) >= '2017-03-22 06:49:53.840';
In your query you didn't use single quote around date. That was the problem. However,
you can use any of the following query to compare date
SELECT *
FROM dbo.March2010 A
WHERE A.Date >= '2010-04-01';
SELECT *
FROM dbo.March2010 A
WHERE A.Date >= CAST('2010-04-01' as Date);
SELECT *
FROM dbo.March2010 A
WHERE A.Date >= Convert(datetime, '2010-04-01' )
To sum it all up, the correct answer is :
select * from db where Date >= '20100401' (Format of date yyyymmdd)
This will avoid any problem with other language systems and will use the index.
DateTime start1 = DateTime.Parse(txtDate.Text);
SELECT *
FROM dbo.March2010 A
WHERE A.Date >= start1;
First convert TexBox into the Datetime then....use that variable into the Query
The date format has no issue with me(Mydate's data type is datetime) :
Where Mydate>'10/25/2021' or Where Mydate>'2021-10-25'
but if add a time, above answers are not working.
Here is what I do:
where cast(Mydate as time)>'22:00:00'
If your query needs a date, please add date such as:
where cast(Mydate as time)>'22:00:00' and Mydate='10/25/2021'
First you need to convert both the dates in same format before conversion
SELECT *
FROM dbo.March2010 A
WHERE CONVERT(DATE, A.Date) >= 2010-04-01;

SQL BETWEEN Operator

Why am I getting '2009' data? What am i doing wrong with the WHERE Clause?
SELECT CONVERT(varchar, EventDate, 101) AS EVENTDATE,
CONVERT(varchar, ClosedDate, 101) AS CLOSEDDATED,
DATEDIFF(Day,EventDate,ClosedDate) AS DiffDate,
FROM mytable
WHERE (CONVERT(varchar, EventDate, 101) BETWEEN '04/01/2010' AND '04/30/2010')
You're doing a string comparison, which goes from left to right. '04/10/2009' is between '04/0' and '04/3'.
If the field you're comparing is a DATETIME, don't try to convert it. SQL server can convert the strings to dates and do the comparison properly.
If you use a supported date format, SQL Server will implicitly convert the string to a DATETIME:
SELECT CONVERT(varchar, EventDate, 101) AS EVENTDATE,
CONVERT(varchar, ClosedDate, 101) AS CLOSEDDATED,
DATEDIFF(Day,EventDate,ClosedDate) AS DiffDate,
FROM mytable
WHERE EventDate BETWEEN '2010-04-01' AND '2010-04-30'
Your query is just doing string comparison, which has no bearing on date spans.
Your WHERE clause may be doing string comparison instead of date comparison. If you want to do a date comparison you can change
CONVERT(varchar, EventDate, 101)
to
CAST (CONVERT(varchar, EventDate, 101) AS DATETIME)
You really don't need all the conversion. The dates from the calendar will have the right start and end times. You also want to consider events that might go past the end date or start before the date and end within the date range. Or finally start before and go past...
Here's some code we use
(EventStartDtTm >= startDt and EventStartDtTm <= endDt)
|| (EventStartDtTm <= startDt and EventEndDtTm >= startDt)
-- patrick