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
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 a SQL server query to convert date to Mon DD, YYYY HH:MM:SS AM/PM
for e.g. the date is :-
2016-11-21 16:58:57.797
I need the SQL output as
Nov 21 2016 4:58:57 PM
I tried --> CONVERT(VARCHAR(19),GETDATE(),109)
but it is displaying results like --> Nov 21 2016 4:58:5
Increase the length of VARCHAR in your statement like below
select CONVERT(VARCHAR(30),GETDATE(),109)
This is giving me result as Nov 21 2016 12:55:31:390PM
Hope this should work out for you.
For mysql you can do this:
SELECT DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p');
For ms sql you can do this:
SELECT CONVERT(VARCHAR(19),GETDATE())
Reference:
MySQL DATE_FORMAT() Function
SQL Server CONVERT() Function
Use SUBSTRING and CHARINDEX to get the left part of the string excluding the milliseconds part. Then concatenate the last AM/PM with that.
Query
declare #date as varchar(30) = convert(varchar(30),getdate(),109);
select substring(#date, 1, len(#date) - charindex(':', reverse(#date), 1))
+ ' ' + right(#date, 2) as [datetime];
you can use this
select FORMAT(CAST(GETDATE() AS DATETIME),'MMM dd yyyy hh:mm:ss tt') AS DateTimeAMPM
I am stuck in a weird requirement. I am just hoping that I might get some help.
My requirement is that the incoming data in table is like
'Thu Feb 18 11:03:18 GMT 2016' and it's output must be
2016-02-18 i.e. in YYYY-MM-DD format.
Any ideas!!
try this with some other sample data,
DECLARE #input varchar(50)='Thu Feb 18 11:03:18 GMT 2016'
select cast(replace(substring(#input,4,len(#input)),'GMT','') as datetime)
select cast(cast(replace(substring(#input,4,len(#input)),'GMT','') as datetime) as date)
Alongside with #KumarHarsh's answer, you can use this too:
DECLARE #input VARCHAR(MAX)= 'Thu Feb 18 11:03:18 GMT 2016';
SELECT FORMAT(CONVERT(DATETIME, REPLACE(SUBSTRING(#input, 4, LEN(#input)), 'GMT', ''), 20),'yyyy-MM-dd')
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
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');