简体   繁体   中英

SQL Server Convert Varchar to Datetime

I have this date format: 2011-09-28 18:01:00 (in varchar), and I want to convert it to datetime changing to this format 28-09-2011 18:01:00. How can I do it?

SELECT CONVERT(Datetime, '2011-09-28 18:01:00', 120) -- to convert it to Datetime

SELECT CONVERT( VARCHAR(30), @date ,105) -- italian format [28-09-2011 18:01:00]
+ ' ' + SELECT CONVERT( VARCHAR(30), @date ,108 ) -- full date [with time/minutes/sec]

Like this

DECLARE @date DATETIME
SET @date = '2011-09-28 18:01:00'
select convert(varchar, @date,105) + ' ' + convert(varchar, @date,108)

this website shows several formatting options.

Example:

SELECT CONVERT(VARCHAR(10), GETDATE(), 105) 

You can have all the different styles to datetime conversion :

https://www.w3schools.com/sql/func_sqlserver_convert.asp

This has range of values :-

CONVERT(data_type(length),expression,style)

For style values,
Choose anyone you need like I needed 106.

You could do it this way but it leaves it as a varchar

declare @s varchar(50)

set @s = '2011-09-28 18:01:00'

select convert(varchar, cast(@s as datetime), 105) + RIGHT(@s, 9)

or

select convert(varchar(20), @s, 105)

As has been said, datetime has no format/string representational format.

You can change the string output with some formatting.

To convert your string to a datetime:

declare @date nvarchar(25) 
set @date = '2011-09-28 18:01:00' 

-- To datetime datatype
SELECT CONVERT(datetime, @date)

Gives:

-----------------------
2011-09-28 18:01:00.000

(1 row(s) affected)

To convert that to the string you want:

-- To VARCHAR of your desired format
SELECT CONVERT(VARCHAR(10), CONVERT(datetime, @date), 105) +' '+ CONVERT(VARCHAR(8), CONVERT(datetime, @date), 108)

Gives:

-------------------
28-09-2011 18:01:00

(1 row(s) affected)
SELECT CONVERT(VARCHAR(10), GETDATE(), 105) + ' ' + CONVERT(VARCHAR(10), GETDATE(), 108)

试试下面的

select Convert(Varchar(50),yourcolumn,103) as Converted_Date from yourtbl

该网站提供了一些很好的示例,可以将任何varchar转换为日期或日期时间

I got tired of all the different convert googling, ended up with this utility function that is expanded upon new variations.

CREATE FUNCTION [dbo].[ParseDatetime] (
    @INPUT nvarchar(255),
    @FORMAT nvarchar(255)
)
RETURNS datetime

AS 
BEGIN 


DECLARE @TimePart NVARCHAR(255)
DECLARE @DatePart NVARCHAR(255)
DECLARE @DayPart NVARCHAR(255)
DECLARE @ExDayPart NVARCHAR(255)
DECLARE @MonthPart NVARCHAR(255)
DECLARE @YearPart NVARCHAR(255)
DECLARE @Date DATETIME
--DECLARE @INPUTSTRING NVARCHAR(255)
--SET @INPUTSTRING = '20-10-2019 00:00:00'

--SET @FORMAT = 'D-M-YYYY HH:MM:SS'
IF(@FORMAT = 'D-M-YYYY HH:MM:SS')
BEGIN
    SET @TimePart = RIGHT(@INPUT,LEN(@INPUT)-CHARINDEX(' ',@INPUT))
    SET @DatePart = LEFT(@INPUT,CHARINDEX(' ',@INPUT))
    SET @DayPart = LEFT(@DatePart,charindex('-',@DatePart)-1)
    SET @ExDayPart = RIGHT(@DatePart,LEN(@DatePart)-charindex('-',@DatePart)+1)
    SET @MonthPart = left(@ExDayPart ,charindex('-',@ExDayPart )-1)
    SET @YearPart = right(@ExDayPart ,LEN(@ExDayPart )-charindex('-',@ExDayPart )+1)
    SET @Date = DATEFROMPARTS(@YearPart,@MonthPart,@DayPart) 

END

IF(@FORMAT = 'YYYY-MM-DD')
BEGIN
    SET @DayPart = RIGHT(@INPUT,2)
    SET @MonthPart = RIGHT(left(@INPUT,7),2)
    SET @YearPart = LEFT(@INPUT,4)
    SET @Date = DATEFROMPARTS(@YearPart,@MonthPart,@DayPart) 

END

IF(@FORMAT = 'EXCEL_DAYS_SINCE_1900')
BEGIN
--DECLARE @INPUT NVARCHAR(255)
--SET @INPUT = ' 43554' 
    SET @Date = Dateadd(day,CAST(@INPUT AS INT),datefromparts(1900,1,1))
END


RETURN @Date
END
GO

Use it as:

SELECT
[DateAsDate] = dbo.[ParseDateTime]([DateAsString], 'YYYY-MM-DD')
FROM [tablename]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM