简体   繁体   中英

Convert a string to a date in sql

how do I convert this:

DECLARE @FromDate varchar = 'Feb 2020'

to a Date like this

DD/MM/YYYY or DDMMYYYY

I need to sort some dates and from the combobox you can just choose those three : Dez 2019, Jan 2020 or Feb 2020. So I thought I could do something like that:

DECLARE @FromDate varchar = 'Feb 2020';


SELECT RIGHT(convert(varchar, CONVERT(date, CONVERT(varchar, PIN_DATE)), 106),8)
FROM PIN
WHERE RIGHT(CONVERT(varchar, CONVERT(date, CONVERT(varchar, PIN_DATE)), 106),8) 
= CONVERT(datetime, @FromDate,106)

The date in PIN_DATE looks like this : YYYYMMDD aka. 20201231

This is an ERROR:

DECLARE @FromDate varchar = 'Feb 2020'

This assigns @FromDate as a SINGLE character, so the value is 'F' .

When using the character types in SQL Server, always use a length:

DECLARE @FromDate varchar(255) = 'Feb 2020'

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