Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to spilt datetime into date and time in their separate column at the time of insert record.
so need guide for this thanks... 


What I have tried:

SELECT     CONVERT(VARCHAR(7), EndDate, 114) AS Time
FROM         Country
Posted
Updated 6-Dec-18 17:31pm

1 solution

Try casting to equivalent data types. For example
SQL
SELECT CAST(GETDATE() AS date)

SELECT CAST(GETDATE() AS time)

If you want to separate them on the C# side, read the data into a DateTime[^] variable and use either
- DateTime.Date Property (System) | Microsoft Docs[^]
- or DateTime.TimeOfDay Property (System) | Microsoft Docs[^]

As a side note, breaking the date and time into separate columns when inserting a row might not be a good idea. You can always split the information needed from a single column so adding two separate columns for a single point-in-time makes things unnecessary complicated.
 
Share this answer
 
v3
Comments
MadMyche 7-Dec-18 10:36am    
Agree 100%. Dates and times should always be of the DateTime type. How it appears to someone is a Presentation item. The maximum savings of 2 bytes per row is offset by the overhead of having double the columns for the one value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900