Please suggest how to format date into this format '05/23/2014 09:56:07 AM' in SQL server.
Is there a style code to be passed to CONVERT function ?
From Sql Server 2012 onwards you can use following query:-
SELECT FORMAT(GETDATE(), 'G');
Otherwise convert function is used to coverts the date into desired fpormat.
try this
select convert(varchar(50), getdate(), 131)
for detail please read http://msdn.microsoft.com/en-us/library/ms187928.aspx
Check this
declare #d datetime = getdate()
select
#d,
CONVERT ( varchar(30), #d, 101 ) ,
convert( varchar(12), #d, 101) + stuff( right( convert( varchar(26),#d, 109 ), 15 ), 7, 7, ' ' ) as formatedDate
Related
I have old DBF database that contains date in old 6 digit formats like 291292 or 150793 or even 010302.
I import tables into SQL Server and now I need to convert it to datetime format.
Of course must be converted to similar strings 29.12.1992, 15.07.1993, 01.03.2002 first.
You may try this.
GO
declare #date date
set #date = '901124'
select CONVERT(varchar(10), #date, 102) as [Date] --- for yyyy.MM.dd format
TRY THIS
declare #dd varchar(10)
set #dd = '201292'
select CONVERT(VARCHAR(10), #dd, 2), CONVERT(VARCHAR(10), GETDATE(), 2)
, (SUBSTRING(#dd, 5,2) + SUBSTRING(#dd, 3,2) + SUBSTRING(#dd, 1,2))
, CONVERT(varchar(10), cast((SUBSTRING(#dd, 5,2) + SUBSTRING(#dd, 3,2) + SUBSTRING(#dd, 1,2)) as DATE) , 102)
Ok it pretty simple:
GO
declare #date varchar(6)
set #date = '241190'
select CONVERT(date, concat(SUBSTRING(#date, 5,2),SUBSTRING(#date, 3,2),SUBSTRING(#date, 1,2)), 101) as [Date]
result is 1990-11-24
declare #date date = '901124'
select CONVERT(date, #date) as [Date]
Working in SQL Server, I have a column that contains a year in numeric format. I need to make that year into a January 1st date of that 'year'. I've tried a few commands and the latest attempt is:
cast('01/01/' + X.[YEAR] as datetime)
What am I missing?
DECLARE #Year INT = 2010
SELECT CAST(CAST(#Year AS varchar) + '-1-1' AS DATETIME) -- 2010-01-01
Another way:
select GETDATE(),
DATEADD (day, - DATEPART(dayofyear, GETDATE()) + 1, CONVERT(date, GETDATE()))
I'm converting varchar data to date in SQL server.
Table having data like below,
So it can have NULL value, proper formatted date and can have like 19900411and 04221995.
So I have tried something like below, but getting error.
SELECT CASE
WHEN ISNULL(CAST(Dob AS VARCHAR), '') = '' THEN NULL
WHEN LEN(CAST(Dob AS VARCHAR)) = '8' THEN CONVERT(
VARCHAR(10),
CONVERT(date, RIGHT(Dob, 4) + LEFT(Dob, 2) + SUBSTRING(Dob, 3, 2)),
103
)
ELSE CONVERT(VARCHAR, CAST(Dob AS CHAR(8)), 103)
END
FROM TableName
WHERE Dob IS NOT NULL
Msg 241, Level 16, State 1, Line 3 Conversion failed when converting
date and/or time from character string.
I wanted to get output as date format MM-dd-yyyy
Please help me! Thanks!
Can you check this answer. This is work for Sqlserver 2012 and above.
Based on your discussion I created a sample data.
CREATE TABLE #A
( COL VARCHAR(10)
)
INSERT INTO #A VALUES( NULL),('19900411'),('19900411'),('04-04-1976'),('10-30-1952')
insert into #A values ('04221995')
insert into #A values ('02222009 ')
select * from #a
SELECT
case isdate(col ) when 1 then CONVERT(VARCHAR, CONVERT(DATE, COL) , 105)
when 0 then SUBSTRING(COL,1,2) + '-'+SUBSTRING(COL,3,2) + '-'+SUBSTRING(COL,5,4)
end
FROM #A
You can use this query for your problem. It simply convert your varchar column to formatted MM-dd-yyyy
select convert(varchar(10), cast('2011-09-28' as date), 101)
I think, you need this:
SELECT CASE
WHEN ISNULL(CAST(Dob AS VARCHAR), '') = '' THEN NULL
WHEN LEN(CAST(Dob AS VARCHAR)) = '8' THEN CONVERT(VARCHAR(10),
CONVERT(date, RIGHT(Dob, 2) + '-' + SUBSTRING(Dob, 5, 2) +'-' + LEFT(Dob, 4)),
103)
ELSE CONVERT(VARCHAR, CAST(Dob AS CHAR(10)), 103)
END
FROM tbl
WHERE Dob IS NOT NULL
OUTPUT: 04/11/1990
SQL Fiddle DEMO: SQL DEMO
CREATE TABLE #A
( COL VARCHAR(10)
)
INSERT INTO #A VALUES( NULL),('19900411'),('19900411'),('04-04-1976'),('10-30-1952'),('19950422')
SELECT DATESTRINGFIELD = CONVERT(VARCHAR, CONVERT(DATE, COL) , 101) FROM #A
output
04/11/1990
04/11/1990
04/04/1976
10/30/1952
04/22/1995
I have datetime format in varchar yyyymmdd_hhmiss format. I need to convert it to SQL Server datetime .
Is there any easier way ?
for example, 20150622_012030 into '2015-06-22 01:20:30.000' datetime
I got my code below but not sure how to convert time part
SELECT CONVERT(DATEtime,LEFT('20150617_015814',8)), RIGHT('20150617_015814',6)
-- Change the format to yyyyMMdd HH:mm:ss then do the convert
SELECT CONVERT(datetime,
STUFF(
STUFF(
REPLACE('20150622_012030', '_', ' ')
, 14, 0, ':')
, 12, 0, ':')
)
-- Result
2015-06-22 01:20:30.000
Try this
declare #datetime varchar(6) = right('20150622_012030',6)
declare #date varchar(10) = left('20150622_012030',8)
SELECT convert(datetime,#date+' '+left(#datetime,2)+':'+RIGHT(left(#datetime,4),2)+':'+RIGHT(#datetime,2))
I am inserting a datetime field into my SQL database with a format of 2014-10-29 12:05:24.927
My interface needs to display this as 29/10/2014 12:05:24
my tsql statement is :
SELECT Convert(VARCHAR(20), MessageAudit.AuditDate, 120) As AuditDate FROM tableA
Which, produces yyyy-MM-dd hh:mm:ss how, can i format the date portion of this to be dd/MM/yyyy?
You can use something like:
declare #Date datetime
select #Date = cast('20141029 12:05:24.927' as datetime)
select convert(nvarchar(10), #Date, 103) + ' ' + convert(nvarchar(10), #Date, 108)
It will give you precisely 29/10/2014 12:05:24
Concatenate two format dd/mm/yy & hh:mm:ss
e.g
SELECT CONVERT(NVARCHAR(10), GETDATE(), 103) + ' ' +
CONVERT(NVARCHAR(10), GETDATE(), 108) AS [Date]
Here's a pretty extensive list of date formats in sql server:
http://www.sql-server-helper.com/tips/date-formats.aspx
for your specific problem you could do:
SELECT CONVERT(VARCHAR(20), AuditDate, 103) + ' ' +
CONVERT(VARCHAR(20), AuditDate, 108) AS AuditDate
FROM tableA
Though this will return the date as a string/varchar - which has it's own set of issues. You're really better off keeping the date in a date format and formatting it on the front end. This will allow you (depending on how the date is displayed) to still be "date" sortable rather than string sortable.
As http://msdn.microsoft.com/it-it/library/ms187928.aspx, you have to specify 103 as the third parameter of the CONVERT() function.
SELECT Convert(VARCHAR(20), MessageAudit.AuditDate, 103) As AuditDate FROM tableA