Using SQL Server 2005
Table1
Time
12/05/2009 08:30:49
12/05/2009 17:00:00
13/05/2009 21:00:00
...,
Above Table Both values are same column, Here I want to separate column like Date and Time
Expected Output
Date Time
12/05/2009 08:30:49
12/05/2009 17:00:00
13/05/2009 21:00:00
You can retrieve separate date and time parts like:
SELECT
CONVERT(VARCHAR(10),DateField,101) as DatePart,
CONVERT(VARCHAR(10),DateField,108) as TimePart
FROM YourTable
For more information, see the CONVERT documentation.
The code snippet above returns the DataPart and TimePart as a varchar. If you're interested in retrieving them as a datetime field, try:
SELECT
DATEADD(D, 0, DATEDIFF(D, 0, DateField)) as DatePart,
DATEADD(day, -DATEDIFF(D, 0, DateField), DateField) as TimePart
FROM YourTable
The following query returns the date and time respectively for the current system date/time:
SELECT
CONVERT(VARCHAR(8) , GETDATE() , 108) AS [time],
CONVERT(VARCHAR(10) , GETDATE() , 101) AS [date]
select
CONVERT(VARCHAR(8), GETDATE(), 108) as [time],
CONVERT(VARCHAR(8), GETDATE(), 111) as [date]
See this reference for all formats
Just a slight modification to pmarflee's answer, because you appear to be wanting the date as dd/mm/yy rather than mm/dd/yy:
SELECT CONVERT(VARCHAR(8) , GETDATE() , 108) AS [time], CONVERT(VARCHAR(10) , GETDATE() , 103) AS [date]
Related
I have very simple code, but the issue is that when I convert a date column to Varchar(10), then I am not fetching distinct dates and sort is not working as desired; when I use DATE format, then I am fetching distinct dates and date sort is working, but I get datetime - 2017-02-01 12:00:00 AM (I don't want the time part)
Here is the code:
SELECT DISTINCT
--CONVERT(varchar(10), DATEADD(month, DATEDIFF(month, 0, (MyDate)), 0), 101) AS MonthValue,
CONVERT(DATE, DATEADD(month, DATEDIFF(month, 0, (MyDate)), 0)) AS MonthValue
FROM dbo.Mytable
WHERE MyDate >= GETDATE()-366
ORDER BY MonthValue DESC
Using a CAST function in a select statement, how do I return a date column with just the month and day?
SELECT CAST(DateAdded as date) -- returns column with date format
, CAST(DateAdded as time) -- returns only time
, CAST(DateAdded as ...) --return only month and day
Something like this(replacing the getdate function with your date column):
select
cast(getdate() as date) as FullDate
, right(cast(getdate() as date),5) as MonthDay
, cast(getdate() as time) as TimeofDay
Warning: this may depend on which version of SQL Server you are using.
SELECT CONVERT(VARCHAR(5), GETDATE(), 4)
21.10
OR
SELECT CONVERT(VARCHAR(5), GETDATE(), 5)
21-10
OR
SELECT CONVERT(VARCHAR(5), GETDATE(), 3)
21/10
OR
SELECT CONVERT(VARCHAR(2),MONTH(GETDATE())) + '-'+ CONVERT(VARCHAR(2),DAY(GETDATE()))
10-21
I am trying to filter out the values based on the date which should be within today's date. I am using the below query, however the query is not working as expected.
This is what I am using in SQL Server.
select values
from table
where date between DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
and GETDATE()
I am expecting the date to be filtered for values between '2015-03-18 00:00:00' AND '2015-03-18 23:59:59' if I am executing the query on 18th March 2015.
Please guide.
It's much better to use a >= and < than BETWEEN:
SELECT *
FROM Table
WHERE
date >= CAST(CAST(GETDATE() AS DATE) AS DATETIME)
AND date < DATEADD(DAY, 1, CAST(CAST(GETDATE() AS DATE) AS DATETIME))
CAST(CAST(GETDATE() AS DATE) AS DATETIME) will get you the current date without the time part that is 2015-03-18 00:00:00
DATEADD(DAY, 1, CAST(CAST(GETDATE() AS DATE) AS DATETIME)) will get you the next day, again without the time part.
So in turn, your WHERE condition would be:
WHERE
date >= '2015-03-18 00:00:00'
AND date < '2015-03-19 00:00:00'
Alternatively, you can use this:
SELECT *
FROM Table
WHERE
date >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) -- beginning of this day
AND date < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + 1, 0) -- beginning of next day
For more date calculations, refer to this article.
As You are Expecting value between '2015-03-18 00:00:00' AND
'2015-03-18 23:59:59. so, you can simply compare only date part.
you can Use Cast()/Convert():
SELECT *
FROM Table
WHERE
cast(date as date) =cast(getdate() as date)
It included 2015-03-18 00:00:00' AND '2015-03-18 23:59:59'
I'm trying to retrieve orders that were created this year using this condition:
WHERE ORDER_DATE BETWEEN YEAR(GETDATE()) AND GETDATE()
But I still get results that have order_date values before 2013.
The field type is of datetime format, I thought it was varchar.
When I do 2013-01-01 instead of YEAR(GETDATE()) it works accordingly.
here is my two cents
Select * from Order1 where ORDER_DATE between DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0) and GETDATE()
Why not
WHERE YEAR(ORDER_DATE) = YEAR(GETDATE())
Raj
Let's say that GETDATE() returns 1/07/2013.
This...
WHERE ORDER_DATE BETWEEN YEAR(GETDATE()) AND GETDATE()
Would look like this...
WHERE ORDER_DATE BETWEEN '1905-07-07 00:00:00.000' AND '2013-07-01 00:00:00.000'
This is because the value 2013 returned from YEAR(GETDATE()) represents the 7th July 1905 when cast into a datetime.
I would just make the first of January for the current year - something like:
where order_date >= convert( datetime,
convert( varchar(4), getdate() ,120 ) + '-01-01',
120
)
or if like Panagiotis Kanavos you prefer to use year(getdate())
where order_date >= convert( datetime,
convert( varchar(4), year(getdate()) ) + '-01-01',
120
)
I would make that into a function if I was using in more than one place
or (sql server 2012)
where order_date >= DATEFROMPARTS ( year(getdate()), 1, 1 )
In SQL Server 2012 you can use the DATEFROMPARTS or DATETIMEFROMPARTS functions to create a date from its parts. Your WHERE can be written as:
where order_date >=DATEFROMPARTS(YEAR(GETDATE()),1,1)
This avoids any conversions and allows the query optimizer to use any existing indexes that include ORDER_DATE.
Try this,
WHERE ORDER_DATE BETWEEN YEAR(ORDER_DATE) AND YEAR(GETDATE())
I have a table with two fields - datetime and int. I want to do a group by on the datetime only on the date ignoring the hour and minute. The SELECT statement should return a date that maps to the sum of the int of a single day.
SELECT CAST(Datetimefield AS DATE) as DateField, SUM(intfield) as SumField
FROM MyTable
GROUP BY CAST(Datetimefield AS DATE)
As he didn't specify which version of SQL server he uses (date type isn't available in 2005), one could also use
SELECT CONVERT(VARCHAR(10),date_column,112),SUM(num_col) AS summed
FROM table_name
GROUP BY CONVERT(VARCHAR(10),date_column,112)
I came researching the options that I would have to do this, however, I believe the method I use is the simplest:
SELECT COUNT(*),
DATEADD(dd, DATEDIFF(dd, 0, date_field),0) as dtgroup
FROM TABLE
GROUP BY DATEADD(dd, DATEDIFF(dd, 0, date_field),0)
ORDER BY dtgroup ASC;
-- I like this as the data type and the format remains consistent with a date time data type
;with cte as(
select
cast(utcdate as date) UtcDay, DATEPART(hour, utcdate) UtcHour, count(*) as Counts
from dbo.mytable cd
where utcdate between '2014-01-14' and '2014-01-15'
group by
cast(utcdate as date), DATEPART(hour, utcdate)
)
select dateadd(hour, utchour, cast(utcday as datetime)) as UTCDateHour, Counts
from cte
Personally i prefer the format function, allows you to simply change the date part very easily.
declare #format varchar(100) = 'yyyy/MM/dd'
select
format(the_date,#format),
sum(myfield)
from mytable
group by format(the_date,#format)
order by format(the_date,#format) desc;