Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
982 views
in Technique[技术] by (71.8m points)

sql - Date without the time

I'm working with SQL Server 2005.

I have a column called purchase_time of type datetime. How do I select this column with the time part - just the date.

Thanks,

Barry

EDIT: Would it be safe to get the datetime and split it via Python on the first space, or is this format locale dependant?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In versions < 2008 (which, based on other comments to some of the answers, I believe you are running), the most efficient way is to keep it as a datetime type and use date math to avoid string conversions.

SELECT DATEADD(DAY, DATEDIFF(DAY, '20000101', purchase_time), '20000101') 
  FROM dbo.table;

EDIT

If you want the date only for display purposes, not for calculations or grouping, that is probably best handled at the client. You can do it in SQL simply by saying:

SELECT dt = CONVERT(CHAR(10), purchase_time, 120)
  FROM dbo.table;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...