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
Related
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
How to get following output ?
[1],[2],[3],[4],[5],[6],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17]
,[18],[19],[20]
,[21],[22],[23],[24],[25],[26],[27],[28],[29],[30]
Need to fetch all dates from given month in sql server
I want to display daily Date Wise report
Example :
If I pass date as '11/01/2012' then it should return above result
and if I pass December than 31 days.
Very unclear question but try
DECLARE #epoch DATETIME = '20130101'
;WITH cte AS
(
SELECT #epoch DateKey
UNION ALL
SELECT DATEADD(D, 1, DateKey)
FROM cte
WHERE MONTH(DATEADD(D, 1, DateKey))=MONTH(#epoch)
)
SELECT * FROM cte
try below code
declare #date datetime = '09/10/2012'
declare #noofdays int
select #noofdays =datediff(day, #date, dateadd(month, 1, #date))
DECLARE #startDate DATETIME=CAST(MONTH(#date) AS VARCHAR) + '/' + '01/' + + CAST(YEAR(#date) AS VARCHAR) -- mm/dd/yyyy
DECLARE #endDate DATETIME= CAST(MONTH(#date) AS VARCHAR) + '/' + cast(#noofdays as varchar) + '/' + CAST(YEAR(#date) AS VARCHAR)
;WITH Calender AS
(
SELECT #startDate AS CalanderDate
UNION ALL
SELECT CalanderDate + 1 FROM Calender
WHERE CalanderDate + 1 <= #endDate
)
SELECT [Date] = CONVERT(VARCHAR(10),CalanderDate,25)
FROM Calender
OPTION (MAXRECURSION 0)
I am able to find out answer my self....
Thanks Buddy for trying to help me...
DECLARE #COLSPIVOT AS NVARCHAR(MAX)=''
declare #MaxDate int
set #MaxDate=(SELECT day(DATEADD(ms,-2,DATEADD(MONTH, DATEDIFF(MONTH,0,'8/1/2013')+1,0)))
AS [Current Month])
declare #i int =1
while (#i<=#MaxDate)
begin
set #COLSPIVOT=#COLSPIVOT+'['+convert(varchar(10),#i)+']'
if(#i!=#MaxDate)
begin
set #COLSPIVOT=#COLSPIVOT+','
end
set #i=#i+1
end
select #COLSPIVOT
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.
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)