简体   繁体   中英

Extract date from datetime column - SQL Server Compact

I'm using SQL Server Compact 4.0 version, and although it might seem a simple thing to find in google, the examples I've tried none of them work.

My column signup_date is a DateTime with a value 04-09-2016 09:05:00 .

What I've tried so far without success:

SELECT FORMAT(signup_date, 'Y-m-d') AS signup_date;
SELECT CONVERT(signup_date, GETDATE()) AS signup_date
SELECT CAST(data_registo, date) AS signup_date

I found that I could use DATEPART function, but that would force me to concat the values, is this the right path to follow? If so, how do I concat as Ymd?

SELECT DATEPART(month, signup_date)  

SQL Server Compact has no date type.

If you don't want to see the time, convert the datetime value to a string:

SELECT CONVERT(nvarchar(10), GETDATE(), 120)

(This has been tested and actually works against SQL Server Compact)

Most of the answers seek to achieve same thing but the explanation to the codes is not enough

CONVERT(date, Date_Updated, 120)

this code does the conversion with mssql. The first item 'date' is the datatype to return. it could be 'datetime', 'varchar', etc. The second item 'Date_Updated' is the name of the column to be converted. the last item '120' is the date style to be returned. There are various styles and the code entered will determine the output. '120' represent YYYY-MM-DD. Hope this helps

The old fashioned way of doing this in SQL Server might work for your purposes:

select dateadd(day, datediff(day, 0, signup_date), 0)

The datediff() gets the number of days (as an integer) since time 0. The dateadd() adds this number back.

If you don't like 0 as a date, you can put any valid date in its place:

select dateadd(day, datediff(day, '2000-01-01', signup_date), '2000-01-01')

EDIT:

If you simply don't want to see the time, convert the date to a string:

select convert(nvarchar(10), signup_date, 120)

(I recommend the YYYY-MM-DD format, but others are available .)

I have tried this and many other solutions. I wanted a generic solution that would work with any LCID. My solution is a bit of convoluted code, but it works perfectly for me. It's a booking system where I needed to find out who was arriving on a particular date. ArriveDate is the column, d is the DATE I want.

SQL = "SELECT * FROM tablename WHERE dateadd(day, datediff(day, 0, 
ArriveDate), 0)=' " & Format(d, "yyyy-MM-dd") & " ' "

This will return only date value in original datetime type. So you can do any comparison using the output

SELECT  convert(datetime, CONVERT(nvarchar(10), GETDATE(), 120)) 

今天刚看到这个问题,我知道有点晚了:)但也许这会有所帮助..,

select  convert(date,(convert(varchar(20),'04-09-2016 09:05:00')))

select convert(nvarchar, getdate(), 1) = 09/25/19

select convert(nvarchar, getdate(), 2) = 19.09.25

select convert(nvarchar, getdate(), 3) = 25/09/19

select convert(nvarchar, getdate(), 4) = 25.09.19

select convert(nvarchar, getdate(), 5) = 25-09-19

select convert(nvarchar, getdate(), 6) = 25 Sep 19

select convert(nvarchar, getdate(), 7) = Sep 25, 19

select convert(nvarchar, getdate(), 10) = 09-25-19

select convert(nvarchar, getdate(), 11) = 19/09/25

select convert(nvarchar, getdate(), 12) = 190925

select convert(nvarchar, getdate(), 23) = 2019-09-25

select convert(nvarchar, getdate(), 101) = 09/25/2019

select convert(nvarchar, getdate(), 102) = 2019.09.25

select convert(nvarchar, getdate(), 103) = 25/09/2019

select convert(nvarchar, getdate(), 104) = 25.09.2019

select convert(nvarchar, getdate(), 105) = 25-09-2019

select convert(nvarchar, getdate(), 106) = 25 Sep 2019

select convert(nvarchar, getdate(), 107) = Sep 25, 2019

select convert(nvarchar, getdate(), 110) = 09-25-2019

select convert(nvarchar, getdate(), 111) = 2019/09/25

select convert(nvarchar, getdate(), 112) = 20190925

select convert(nvarchar, getdate(), 8) = 13:48:36

select convert(nvarchar, getdate(), 14) = 13:49:48:713

select convert(nvarchar, getdate(), 24) = 13:49:57

select convert(nvarchar, getdate(), 108) = 13:50:07

select convert(nvarchar, getdate(), 114) = 13:50:14:490

select convert(nvarchar, getdate(), 0) = Sep 25 2019 1:50PM

select convert(nvarchar, getdate(), 9) = Sep 25 2019 1:50:31:813PM

select convert(nvarchar, getdate(), 13) = 25 Sep 2019 13:50:39:307

select convert(nvarchar, getdate(), 20) = 2019-09-25 13:50:49

select convert(nvarchar, getdate(), 21) = 2019-09-25 13:50:58.923

select convert(nvarchar, getdate(), 22) = 09/25/19 1:51:07 PM

select convert(nvarchar, getdate(), 25) = 2019-09-25 13:51:14.473

select convert(nvarchar, getdate(), 100) = Sep 25 2019 1:51PM

select convert(nvarchar, getdate(), 109) = Sep 25 2019 1:51:32:227PM

select convert(nvarchar, getdate(), 113) = 25 Sep 2019 13:51:38:740

select convert(nvarchar, getdate(), 120) = 2019-09-25 13:51:50

select convert(nvarchar, getdate(), 121) = 2019-09-25 13:51:57.153

select convert(nvarchar, getdate(), 126) = 2019-09-25T13:52:03.627

使用这个,我有同样的问题

SELECT CAST(data_registo as date) AS "signup_date"

To get a string value, use CONVERT

select convert(varchar(10), signup_date, 11)

Check here for various formats:

https://msdn.microsoft.com/en-us/library/ms187928.aspx

To get a DATE, and just strip out the time, do this

Select Cast (signup_date as DATE)

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