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)
Related
I'm trying to convert server date time to IST
I have tried the query below:
SELECT Convert(varchar(100), getdate())
But am unable to convert to IST Time Format.
The only other option, assuming you aren't meaning SYSUTCDATETIME() could be this...
SELECT
CAST(DATEPART(DAY,GETDATE()) AS VARCHAR(2))
+ '-'
+ CAST(DATENAME(MONTH,GETDATE()) AS VARCHAR(3))
+ '-'
+CAST(DATEPART(YEAR,GETDATE()) AS VARCHAR(4))
RETURNS
23-Aug-2017
OR...
SELECT REPLACE(CONVERT(CHAR(11),GETDATE(), 106),' ','-') + ' ' + CAST(CAST(GETDATE() as TIME) AS VARCHAR(8))
RETURNS
23-Aug-2017 08:10:55
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 have let's say
'2015-01-16 13:50:00.000'
in my database.
How do I get the hours and minute only ?
13:50
in the end I want to make query like this
Update A set status = 1 WHERE endTime = current_time()
*endTime = field name of my sample above.
SELECT DATEPART(HOUR, GETDATE());
SELECT DATEPART(MINUTE, GETDATE());
MSDN: DATEPART
I want to get 13:50. is it the way
SELECT CAST(DATEPART(HOUR, GETDATE())AS VARCHAR(2)) + ':' +
CAST(DATEPART(MINUTE, GETDATE())AS VARCHAR(2))
I'm on SQL-Server 2005 which has no TIME datatype, but this should also work for you:
SELECT LEFT(DATEADD(MINUTE, 10, (CONVERT(TIME(0),GETDATE()))),5)
If you want to filter your records by the hour+minute part:
WHERE DATEPART(HOUR, endTime)= DATEPART(HOUR, GETDATE())
AND DATEPART(MINUTE, endTime)= DATEPART(HOUR, GETDATE())
Try the following
SQL Server 2000/2005
SELECT
CONVERT(VARCHAR(8), GETDATE(), 108) AS HourMinuteSecond,
CONVERT(VARCHAR(10), GETDATE(), 101) AS DateOnly;
GO
SQL Server 2008 Onwards
SELECT
CONVERT(TIME, GETDATE()) AS HourMinuteSeconds;
SELECT
CONVERT(DATE, GETDATE()) AS DateOnly;
GO
you can learn about it more here
I have int value with YYYYMM. I want to:
1. convert it into datetime
2. add one day DATEADD(Day, +1, #date)
3. convert it back into int
What's the easiest way to do this?
Here is a nice exercise and I hope it works out for you...
declare #date date
declare #newDate date
set #date = convert(date, '20100101')
set #newdate = DateAdd(dd, 1, #Date)
select #date
select #newdate
select convert(int, convert(varchar, #newdate, 112)) -- this is your final conversion back to int
If your initial int is, say, 201310 (October 2013), then what I think you want is this:
select convert(datetime, rtrim(201310 * 100 + 1))
The function RTRIM is a trick to convert int to string type.
The result is this:
2013-10-01 00:00:00.000
If you don't want to use RTRIM, the command below will get you the same result:
select convert(datetime, convert(char, 201310 * 100 + 1))
I need to store the current time (format 'hi') as char(4) in a table (is created and used by another program, I can't change it) and now wondered what is a suitable way to retrieve it via SQL. I know MySQL is not that standard-orientated, but I thought it could be something similiar to DATE_FORMAT(NOW(), 'Hi'). The code below which I found works, but feels little intricately.
SELECT CAST(DATEPART(hour, GETDATE()) AS nvarchar)+CAST(DATEPART(minute, GETDATE()) AS nvarchar);
Is there a better way to achieve this?
Following gives the same result:
SELECT LEFT(STUFF(CONVERT(NCHAR(8), GETDATE(), 8), 3, 1,''), 4)
It seems to have approximately the same performance as the method from question. I tested it like this:
DECLARE #i INT = 0,
#dto1 DATETIME2,
#dto2 DATETIME2
SET #dto1 = SYSDATETIME()
WHILE #i < 100000
BEGIN
PRINT LEFT(STUFF(CONVERT(NCHAR(8), GETDATE(), 8), 3, 1,''), 4)
SET #i = #i + 1
END
SET #dto2 = SYSDATETIME()
SELECT DATEDIFF(MILLISECOND, #dto1, #dto2)
Sql Server 2008 has a time datatype:
select replace(left(cast(getdate() as time), 5), ':', '')