How can I convert yyyy-mm-dd:hh:mm:ss to yyyy-mm-dd:hh in SQL Server?
I want to convert 2013-04-30:10:34:23 to 2013-04-30:10:00:00?
Please help me to resolve this issue.
can you tried this
SELECT REPLACE(CONVERT(VARCHAR(13), GETDATE(), 120), ' ', ':')
for more help please refer this link
If both input and result are in datetime format, you could just use substring to get the desired output.
Read this, this will help you how to convert or short any date format in SQL sever
Example
The following script uses the CONVERT() function to display different formats. We will use the GETDATE() function to get the current date/time:
CONVERT(VARCHAR(19),GETDATE())
CONVERT(VARCHAR(10),GETDATE(),10)
CONVERT(VARCHAR(10),GETDATE(),110)
CONVERT(VARCHAR(11),GETDATE(),6)
CONVERT(VARCHAR(11),GETDATE(),106)
CONVERT(VARCHAR(24),GETDATE(),113)
The result would look something like this:
Nov 04 2011 11:45 PM
11-04-11
11-04-2011
04 Nov 11
04 Nov 2011
04 Nov 2011 11:45:34:243
Related
Using ASP classic, I want to convert a date Tue, 21 Apr 2020 12:23:00 GMT to the format AAAA-MM-DD HH:MM:SS to insert it into a SQL Server 2008 database.
My code:
CONVERT('Tue, 21 Apr 2020 12:23:00 GMT', GETDATE(), 20)
I get this error:
Incorrect syntax to 'Tue, 21 Apr 2020 12:23:00 GMT'.
Note that dates in SQL Server don't actually have any internal string format. Rather, they are stored as binary. One option here would be to take the following substring of the your input timestamp, and then use TRY_CONVERT to marshall it over to a bona fide datetime inside SQL Server:
Tue, 21 Apr 2020 12:23:00 GMT <-- start with this input
21 Apr 2020 12:23:00 <-- convert this string to datetime
Sample code:
WITH yourTable AS (
SELECT 'Tue, 21 Apr 2020 12:23:00 GMT' AS dt
)
SELECT
dt,
TRY_CONVERT(datetime, SUBSTRING(dt, 6, LEN(dt) - 9)) AS dt_out
FROM yourTable;
Demo
Edit:
If you are using an earlier version of SQL Server, then you can use CONVERT with format mask 106, using the same substring as above:
SELECT
dt,
CONVERT(datetime, SUBSTRING(dt, 6, LEN(dt) - 9), 106) AS dt_out
FROM yourTable;
I need to convert a string to a valid date format
I have tried using cast and convert functions to no avail.
Convert( mystring, mm/dd/yy)
I have got a string like: Tuesday, July 09, 2019 12:00 AM to get the output mm/dd/yy . I am not able to get this work.
Tuesday, July 09, 2019 12:00 AM---> mm/dd/yy
Try this:
SELECT TRY_PARSE('Tuesday, July 09, 2019 12:00 AM' AS DATETIME USING 'en-US')
to convert to valid date.
Then:
SELECT CONVERT(VARCHAR(10), TRY_PARSE('Tuesday, July 09, 2019 12:00 AM' AS DATETIME USING 'en-US'), 101)
to convert it to mm/dd/yy.
The most important hint was provided by Larnu as a comment: Do not store date as string, rather use the native date formats and think about the string format in output scenarios only.
So hopefully you are trying to fix this technical issue and therefore you need this conversion in order to store the values in neatly typed columns ;-)
The suggestion by gotqn is the best, as you obviously need to use a specific language/culture and TRY_PARSE is the only approach which allows to specify this parameter. TRY_PARSE will need at least v2012 and - this might be a draw back, it is really slow.
So, if this is a one-way-ticket (just one conversion in order to store your values correctly) and you have a very large count of rows, you might go this route:
DECLARE #yourstring VARCHAR(100)='Tuesday, July 09, 2019 12:00 AM';
SET LANGUAGE ENGLISH;
SELECT CAST(SUBSTRING(#yourstring,CHARINDEX(',',#yourstring)+1,100) AS datetime);
The simple idea is: Cut away the leading Weekday's name. The rest is working implitly.
I have this statement in my SQL Server Mgt Studio script. The Date output is Dec 28 2015 12:00AM etc. how to change it to 20151228 (YYYYMMDD format). Pls help. Thank you.
+'|'+ Cast(dateadd(wk,DATEDIFF(Wk,7,GETDATE()),0) as varchar) AS [FIRSTDAY|],
+'|'+ CAST(DATEADD(wk,DATEDIFF(wk,7,GETDATE()),6)AS varchar) as [LASTTDAY|]
Prior to Sql Server 2012:
CONVERT(varchar(8), dateadd(wk,DATEDIFF(Wk,7,GETDATE()),0), 112)
Sql Server 2012 and later:
FORMAT(dateadd(wk,DATEDIFF(Wk,7,GETDATE()),0), 'yyyyMMdd')
But you should really ask yourself if SQL is the correct place to do this at all. You'll often have better results by just returning a DateTime value and letting client code handle the formatting.
Use the convert function instead of cast. Use style 112. https://msdn.microsoft.com/en-us/library/ms187928.aspx
Please try this.
declare #dt nvarchar(50) = 'Dec 28 2015 12:00AM'
select convert(varchar(4),datepart(yy,#dt))+convert(varchar(2),datepart(mm,#dt))+convert(varchar(2),datepart(dd,#dt))
I have a date column in SQL Server and I want to get date in yyyy/mm/dd format.
My code is:
select
convert(date, Consumption_FDate, 111)
from
dbo.TblStuff_Consumption
Consumption_FDate is my date column and dbo.TblStuff_Consumption is my table.
My problem is this code is not working; the output is same every time, though I change '111' into other numbers.
SELECT CONVERT(VARCHAR(10), Consumption_FDate, 111)
FROM dbo.TblStuff_Consumption
In SQL serve 8 working fine
select convert(varchar,getDate(),112)
CONVERT(VARCHAR(19),GETDATE()) output Nov 04 2014 11:45 PM
CONVERT(VARCHAR(10),GETDATE(),10) output 11-04-14
CONVERT(VARCHAR(10),GETDATE(),110) output 11-04-2014
CONVERT(VARCHAR(11),GETDATE(),6) output 04 Nov 14
CONVERT(VARCHAR(11),GETDATE(),106) output04 Nov 2014
CONVERT(VARCHAR(24),GETDATE(),113) output04 Nov 2014 11:45:34:24
I am retrieving date and time using this:
SELECT convert(varchar, getdate())
and it shows me output as:
Jul 3 2012 9:34PM
but client want that there should be space between time and AM/PM, means output should be like this:
Jul 3 2012 9:34 PM
Thanks in advance.
For one, don't use varchar without length. And you can use STUFF here, e.g.
SELECT STUFF(CONVERT(VARCHAR(19), GETDATE()), 18, 0, ' ');
Results:
Jul 3 2012 12:48 PM
Note that this can look different depending on language and regional settings.
In SQL Server 2012 this is a little more intuitive, however the extra space between month and day when the day is < 10 is tricky to work around (or my understanding of .NET's format is lacking):
SELECT FORMAT(GETDATE(), 'MMM d yyyy hh:mm tt');