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,'.')
Related
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)
I have a date stored as datetime2(7) and a time stored as varchar(5).
e.g.
Date = 2016-11-30 00:00:00.000000 (datetime2)
Time = 09:00 (varchar)
Output should be 2016-11-30 09:00:00.000000 (datetime).
How do I convert or cast these as a datetime. I have tried several ways but have been unsuccessful.
Thank you in advance for your help.
Maybe simple as this?
DECLARE #d DATETIME2(7)='2016-11-30 00:00:00.000000'
DECLARE #t VARCHAR(5)='09:00';
SELECT CAST(CAST(#d AS DATETIME) + CAST(#t+':00' AS DATETIME) AS datetime2(7))
Your time needs just :00 to it, than you can cast this to DATETIME. Two values of type DATETIMEcan be added.
The whole expression can be re-converted to DATETIME2.
How about something like this:
DECLARE #MYDATE DATETIME2;
DECLARE #MYTIME VARCHAR(5);
SET #MYDATE = '2016-11-30'; -- this is equal to 2016-11-30 00:00:00.000000
SET #MYTIME = '09:00';
-- for datetime2
SELECT CAST(CAST(LEFT(#MYDATE,10) AS VARCHAR) + ' ' + #MYTIME + ':00.000000' AS DATETIME2)
-- for datetime
SELECT CONVERT(DATETIME,CAST(LEFT(#MYDATE,10) AS VARCHAR) + ' ' + #MYTIME + ':00') -- for datetime
I want to check date in between two dates or can pass as a NULL
using stored proc
code
declare #fromDate date = null
declare #toDate date = null
select * from Mytable
where date betweeen #fromDate and #toDate OR NULL (how to check for both parameters)
I have other 2 more parameters so irrespective with date result should be displayed.
if #todate and #fromDate is NULL
please help.
Try with coalesce function as below
declare #fromDate date = null
declare #toDate date = null
select * from Mytable
where date between coalesce(#fromDate,date) and coalesce(#toDate,date)
Pretty much convert your English to SQL:
where (date is null or date betweeen #fromDate and #toDate)
When searching for a better solution than "between" with "coalesce" i found this thread:
firebird SQL
param is never NULL
createdatum is never NULL
offdatum can be NULL - for obvious reasons ;)
Initial solution:
where
X = :X
AND
cast(:QryDateTime as DATE) between
createdatum and coalesce(offdatum, cast(:QryDateTime as DATE))
problem there was a little tricky: when using "now" as value for QryDateTime there was (very rarely) no result found - even if there should exist one
Problem was that now is evaluated every time separately and obviously not in the left-to-right order.
I finally changed it to:
where
X = :X
AND
iif(offdatum is null, 1, iif(offdatum > cast(:QryDateTime as DATE), 1, 0)) = 1
AND
cast(:QryDateTime as DATE) > createdatum
I have a strange bug that I cannot resolve (with a one line function).
This code works:
DECLARE #TestDate datetime = '2013-05-01 23:15:11'
select IsNull(convert(varchar(max), #TestDate, 120), 'null') as 'test1'
Displays: 2013-05-01 23:15:11
CREATE FUNCTION [dbo].[DateOrNullToChar] (#InputDate date)
RETURNS VARCHAR(40)
BEGIN
return ISNULL(convert(varchar(40),#InputDate, 120),'null');
END
select dbo.DateOrNullToChar('2013-05-01 23:15:11') as 'result'
Returns: 2013-05-01 (no time)
I have also tried varchar(max).
The purpose of the function is for something like this:
Set #ErrorMessage = ' #ArrivalDate=' + dbo.DateOrNullToChar(#ArrivalDate) +
' #DepartureDate=' + dbo.DateOrNullToChar(#DepartureDate);
If any one value is null, the whole value becomes null. So I want to see the string 'null' when a date has a null value.
#InputDate should be datetime or datetime2 if you want time to be shown
The clues are in the code...
#TestDate datetime
#InputDate date
You need to make the parameter type to be datetime instead of date:
CREATE FUNCTION [dbo].[DateOrNullToChar] (#InputDate datetime)
It's silently converting the string to your date parameter type and thus dropping the time portion.
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