How do I convert/cast the string April 04, 2016 12:00:00 to DATETIME in sql server 2005?
UPDATE I would like to compare dates like this :
SELECT *
from ZeroAndLackingRequest
WHERE request_date BETWEEN CONVERT(DATETIME,'April 04, 2016 12:00:00',103)
AND CONVERT(DATETIME,'April 04, 2016 11:59:59',103)
but it is not working. Any Ideas why?
SELECT cast('April 04, 2016 12:00:00' AS datetime)
OR
SELECT CONVERT(DATETIME,'April 04, 2016 12:00:00')
For more details
Select * from [ZeroAndLackingRequest] Z where Z.request_date = CONVERT(datetime, 'April 04, 2016 12:00:00', 120)
remember I choose 120 style you can chooose 103 or any other please see below link.
For a full discussion of CAST and CONVERT, including the different date formatting options, see the MSDN Library Link below:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
If you want to compare
Select * from [ZeroAndLackingRequest] Z
where cast(Z.request_date as datetime) >= cast('April 04, 2016 12:00:00' as datetime)
and cast(Z.request_date as datetime) < cast('April 04, 2016 11:59:59' as datetime)
Related
Here is my query:
SELECT ID AS 'securityid'
, date
, DATEADD(DW, 1, adate) AS 'lagged_date_v2'
, DATEADD(DAY, 1, adate) AS 'lagged_date_v1'
, aclose AS 'previous_close'
FROM mytable
WHERE adate BETWEEN '20170101 09:00' AND '20170630 18:00' AND
ID = 100056;
Here are the output from the code above:
I compared the results to DATEADDby day and by weekday and it returns the same results.
Column adate is just typical date, we can tell Jan 28, 2017 is Saturday. However, on this row, both lagged_date_v1 and lagged_date_v2 return Jan 28. If I use DATEADD by weekday correctly, I should see Jan 30 instead Jan 28, right?
My sql server version is 2008.
If I use DATEADD by weekday correctly, I should see Jan 30 instead Jan
28, right?
Wrong. In SQL Server, weekday doesn't mean days that aren't weekend days. It means which day of the week (1-7) is the day in question based on the current datefirst setting.
I have a table which includes the columns Season and Academic_Year. Academic_Year is type int. Season can be Spring, Summer or Fall.
Fall 2016 is from 1 Sep 2016 to 31 Dec 2016.
Spring 2016 is from 1 Jan 2017 to 31 Mar 2017 (not a typo - the Academic Year 2016 is from Sep 2016 to Aug 2017)
Summer 2016 is from 1 Apr 2017 to 31 Aug 2017
I need to select rows when getdate() is between a number of days before and after the start of the Season, in pseudo code
WHERE
( getdate() > 31/3/16 AND getdate() < 31/9/16 AND Season = 'Autumn' AND Academic_Year = 2016 )
OR ( getdate() > 31/9/16 AND getdate() < 31/1/17 AND Season = 'Spring' AND Academic_Year = 2016 )
OR ( getdate() > 31/12/16 AND getdate() < 31/4/17 AND Season = 'Summer' AND Academic_Year = 2016 )
My problem is creating the date for comparison with getdate(). I need to combine a string - '31/3/' - with an int - Academic_Year, and create a date.
The solution does not need to be efficient but it does need to work on 2008 and later sql server versions.
SQL will allow this conversion if you just concatenate the varchars and tell it to convert to date. For example:
declare #year int = 2016
declare #date varchar(10) = '3/31'
select cast(#date + '/' + cast(#year as varchar(4)) as date)
You have to cast the integer year to varchar to combine it with the other varchar, but you can then cast the entire thing to a date (or datetime) once you have a recognized date format.
You can form your date by doing by using concatenation of the string and year.
CONVERT(VARCHAR(4), Academic_Year) + '-03-31'
Once you have the date string you could use the date functions to manipulate it further if necessary.
WHERE (GETDATE() > CONVERT(VARCHAR(4),Academic_Year) + '-03-31'
AND GETDATE() < CONVERT(VARCHAR(4),Academic_Year) + '-09-30'
AND Season = 'Autumn') etc
worked for me. The trick was adding '-03-31' rather than preceeding with '31-03-'. Thanks #kicken
I have a SQL Server 2012 query that converts date to VARCHAR
SELECT
CONVERT(VARCHAR(12), dbo.Download.Date_of_Download, 107) as Date_to_Display,
dbo.Download.Number_of_Computers
FROM dbo.Download
ORDER BY dbo.Download.Date_of_Download DESC
Below are results
Date_to_Display Number_of_Computers
-----------------------------------
Aug 14, 2014 240
Aug 13, 2014 519
Aug 12, 2014 622
Aug 11, 2014 2132
Aug 10, 2014 1255
Aug 09, 2014 3240
How do I include day of week, i.e. Saturday, Aug 09, 2014 ?
try this:
select datename(dw,getdate())
output:
------------------------------
Thursday
(1 row(s) affected)
using your query:
SELECT
Datename(dw, dbo.Download.Date_of_Download)+', '+CONVERT(VARCHAR(12), dbo.Download.Date_of_Download, 107) as Date_to_Display,
dbo.Download.Number_of_Computers
FROM dbo.Download
ORDER BY dbo.Download.Date_of_Download DESC
In SQL Server version 2012 and later, there is the FORMAT TSQL function that can do what you want. The "dddd" portion does day of the week:
SELECT
FORMAT(dbo.Download.Date_of_Download, 'dddd MMM dd, yyyy') as Date_to_Display,
dbo.Download.Number_of_Computers
FROM dbo.Download
ORDER BY dbo.Download.Date_of_Download DESC
MS docs: https://learn.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql
I have an issue retrieving the data that I would like. I have a Varchar column that consist of various information. I would like to extract the date from that column only. However, I have been unsuccessful. I used the following SQL(2008) to get the data below, But I can't seem to just get the date only without the time. Obviously, the date and time are in different position. Hope you can help.
SELECT substring(Data, 8, 17)
from mastInfo
9/25/2013 12:36:5
Jul 8 2013 11:40
9/25/2013 12:43:5
SELECT convert(date, Case When IsDate(substring(Data, 8, 17)) = 1
Then substring(Data, 8, 17)
Else NULL END)
from mastInfo
Since you are starting off with a string, it is possible that it does not represent a valid date. Using the IsDate function will return 1 when the string can be converted to a date. Basically, this code will convert to date those values that can be converted, and will return NULL for those values that cannot be converted to a valid date.
If you are using SQL Server 2012 or newer, then you can use Try_Convert().
Here's the SQL Fiddle.
Here's an example (from the above SQLFiddle):
Select Try_Convert(datetime, DateAsString) As DateAsDateTime
,DateAsString
From SomeTable
Here's the result:
DATEASDATETIME DATEASSTRING
September, 25 2013 12:36:05+0000 9/25/2013 12:36:5
September, 25 2013 12:43:05+0000 9/25/2013 12:43:5
July, 08 2013 11:40:00+0000 Jul 8 2013 11:40
if you want convert it to date time use :
SELECT CONVERT(Datetime, '2011-09-28 18:01:00', 120) -- to convert it to Datetime
if you want get time :
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), '2011-09-28 18:01:00', 100), 7))
if you want date part :
SELECT CONVERT (DATE, GETDATE())
what version of sql server?
can you try this:
SELECT convert(date, substring(Data, 8, 17))
from mastInfo
First convert Varchar into DateTime and after that again convert it in varchar in desired date format like 101,102,103
Try this:
select convert(varchar(50),convert(datetime,'9/25/2013 12:43:5'),101)
I have two dates say '2011-01-23' and '2015-11-29',
'2011-01-23' falls in first quarter of 2011 so 'Jan 2011'
'2015-11-29' falls in fourth quarter of 2015 so 'Oct 2015'
In SQL Server I want get all quarters in a select list.
e.g.
Input: #StartDate='2011-01-23' , #EndDate='2015-11-29'
Output:
Jan 2011
Apr 2011
Jul 2011
Oct 2011
Jan 2012
Apr 2012
Jul 2013
Oct 2013
Jan 2014
......
......
......
Jul 2015
Oct 2015
You can use a recursive CTE to generate the dates as follows:
declare #StartDate datetime
declare #EndDate datetime
select #StartDate='2011-01-23' , #EndDate='2015-11-29'
;With Quarters as (
select DATEADD(quarter,DATEDIFF(quarter,0,#StartDate),0) as dt
union all
select DATEADD(quarter,1,dt)
from Quarters
where dt < DATEADD(quarter,DATEDIFF(quarter,0,#EndDate),0)
)
select
--I'd usually keep them as dates at this point, but to match your requirement
CONVERT(varchar(3),dt,109) + ' ' + CONVERT(varchar(4),dt,120)
from Quarters order by dt
This also uses a couple of other tricks - it uses CONVERT with far too short target datatypes to quickly truncate the strings to just the parts that we want to keep - and it uses a DATEADD/DATEDIFF pair to quickly round a datetime value down to it's nearest interval boundary.
For SQL Server 2012, you could instead use FORMAT to produce the output string, but I've not experimented with that much so I'll leave that as an exercise...
Finally, I found the solution of my question..
WITH mycte AS
(
SELECT CAST('2011-01-01' AS DATE) DateValue
UNION ALL
SELECT DATEADD(Q,1,DateValue) FROM mycte WHERE DATEADD(Q,1,DateValue) < '2012-12-31'
)
SELECT CONVERT(varchar(3), DATENAME(MONTH,DateValue))+ ' ' +Convert(varchar(4),DATEPART(YYYY,DateValue)) FROM mycte OPTION (MAXRECURSION 0)