How to get min time from datetime in sql server 2008 r2 - sql-server-2008

How to write below code in in SQL server 2008 r2
DECLARE #date date
DECLARE #datetime datetime
SELECT #datetime = GETDATE()
select #date = GETDATE()
select format(cast(min(#date + #datetime) as datetime), 'hh:mm:ss tt')
this piece of code doesn't work in SQL Server 2008 r

Your code is confusing. You are trying to add two dates which doesn't make sense. Anyway, here's how to convert a datetime to output the hh:mm:ss format.
DECLARE #datetime datetime
SELECT #datetime = GETDATE()
select
DT = convert(char(10),#datetime,120)
,TimeOnly = left(cast(#datetime as time),8)

Related

SQL Server 2008 Datetime conversion

Looking to convert datetime format
20150416 12:29:20:845
to
20150416 12:29:20:84.
I have tried a number of date time conversion but no luck.
DECLARE #Date varchar(22)
SET #Date = CONVERT(Varchar(23), GETDATE(),121) --replace getdate with your value
SELECT #DATE --2015-04-17 10:56:55.29 format you need
SELECT GETDATE() --2015-04-17 10:56:55.297 actual value
SELECT CAST(#DATE as datetime) --2015-04-17 10:56:55.290 if u convert it back
Assuming that you already converted your date as varchar, you can use this query:
DECLARE #V VARCHAR(30) = '20150416 12:29:20:845'
SELECT LEFT(#V,20),STUFF(#V,21,1,'.')

Convert an ISO formatted date to DATETIME

I am writing a SQL query using SQL Server Management Studio and there are some NVARCHAR type values in ISO date format (example: 20130302T164800). I need to convert them to a DATETIME
I tried the Convert() function but it causes an exception:
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value
Is there a way I can do this?
The problem is that your string is not an accepted SQL Server datetime format. SQL Server recognises the ISO8601 format, which is:
yyyy-mm-ddThh:mi:ss.mmm
Which would be 2013-03-02T16:48:00 for your date above.
See Date And Time Styles section.
So the following statement will fail:
declare #date nvarchar(max) = '20130302T164800'
select convertedDate = cast(#date as datetime)
If you convert the string to the ISO8601 format, the statement will work:
declare #date nvarchar(max) = '2013-03-02T16:48:00'
select convertedDate = cast(#date as datetime)
SQL Fiddle with demo.
You can update your format to one SQL Server recognises and cast the string to a datetime in one statement:
declare #date nvarchar(max) = '20130302T164800'
select cast(left(#date, 4)
+ '-' + substring(#date,5,2)
+ '-' + substring(#date,7,5)
+ ':' + substring(#date,12,2)
+ ':' + substring(#date,14,2) as datetime)
SQL Fiddle with demo.
This is just an example, you could convert it to any format recognised by SQL Server, but this converts it to ISO8601. Basically, convert it to a different format to allow the conversion to work.

How to give date range in a month & Year format in where clause sql server

I am trying to give date range in where clause like below.. however its not giving correct o/p
declare #StartDate DATETIME, #EndDate DATETIME
set #StartDate='9/01/2011'
set #EndDate='1/30/2012'
Select * from mytable Where MONTH(WT.ToDate) >= MONTH(#StartDate) AND MONTH(WT.ToDate) <=MONTH(#Enddate)
AND YEAR(WT.ToDate)>= YEAR(#StartDate) AND YEAR(WT.ToDate) <=YEAR(#Enddate)
Please help
What is WT.ToDate, What WT is referring to?
it should be.
declare #StartDate DATETIME, #EndDate DATETIME
set #StartDate='9/01/2011'
set #EndDate='1/30/2012'
Select * from mytable as WT Where MONTH(WT.ToDate) >= MONTH(#StartDate) AND MONTH(WT.ToDate) <=MONTH(#Enddate)
AND YEAR(WT.ToDate)>= YEAR(#StartDate) AND YEAR(WT.ToDate) <=YEAR(#Enddate)
Firstly, note I've changed the format you've specified the dates in to avoid any risk of misinterpretation (now yyyyMMdd).
Secondly, try a clause like this:
declare #StartDate DATETIME, #EndDate DATETIME
set #StartDate='20110901'
set #EndDate='20120130'
Select *
from mytable
Where WT.ToDate >= #StartDate AND WT.ToDate < #EndDate
Note, this will not return rows for 30 Jan 2012, so just tweak your #EndDate as appropriate

Set DateTimePicker Value from a retrieved Time in SQL Server

I'm having a hard time in assigning values that came from my database,
Value to be retrieved :
AppointmentTime
Value = 11:00:00
Data Type = time(0)
I use VS 2010 to assign that value from SQL Server 2008 with a DateTimePicker..
DateTimePicker1
Format = HH:mm
MinDate = 1/1/1770
DateTimePicker1.Value = Appointment Time
It gives me an error of 'Value of 1/1/0001 11:00:00AM is not valid for Value. Value should be between MinDate and MaxDate..
I wonder why SQL Server 2008 gives a value of Date, in my Time(0) Column?
I try this query
declare #x time(0);
set #x = getdate();
print cast(#x as datetime);
It will be give you the time with the date 01/01/1900.
So, try to cast Appointment Time to DateTime before set the DateTimePicker1 value.

Datetime conversion in SQL Server 2008

I am trying to convert varchar to datetime.
SELECT
CONVERT(DATETIME, CONVERT(VARCHAR, YEAR(GETDATE())) + '/' +
CONVERT(VARCHAR, MONTH(GETDATE())) + '/' +
CONVERT(VARCHAR, DAY(GETDATE()) + 27), 120)
I am expecting the result
2012-07-02 00:00:00.000
But my script is throwing an error.
Please anyone help me.
Thanks
Gurej
Why are you starting with a datetime, munging it to varchar and then casting back to datetime?
Is your real question, "How do I remove the time portion of a datetime?"
If so, you do it like this:
select DATEADD(day, DATEDIFF(day, 0, getdate()), 0)
Or SQL Server 2008 onwards, simply:
select cast(CAST(getdate() as date) as datetime)
Or even, declare the underlying variable/column as date, and use
select CAST(getdate() as date)