I have this simple query
insert into my_table(date) values(getdate())
The result is 2017-01-05 12:41:37.273.
I want when I do
select *
from my_table
from my Windows Forms application to set the label1.text = 5 Thu 12:41
2017-01-05 12:41:37.273 ----> 5 Thu 12:41
How can I achieve that with C# code ?
Assuming that you read the data from SQL Server into your C# application I'm pretty sure, that a column of sql type DATETIME will be mapped to a C# type DateTime.
What you need is the textual format of this:
DateTime d = DateTime.Now;
MessageBox.Show(d.ToString("d ddd HH:mm"));
You can parse string to DateTime and wrtie your own format.
DateTime myDateTime = DateTime.Parse("2017-01-05 12:41:37");
string formatedDateTime = myDateTime.ToString("dd-mm-yyyy");
From SQL you can you the below format to get your expected result:
SELECT CAST(DATEPART(D, GETDATE()) AS VARCHAR(2)) + ' ' +
LEFT(DATENAME(WEEKDAY, GETDATE()), 3) + ' ' +
LEFT(CONVERT(VARCHAR(8), GETDATE(), 108), 5)
-- Output: 5 Thu 05:22
Related
I want to count total time in hh:mm:ss format. and I have minutes in int like (465).
I made:
CONVERT(varchar, CONVERT(datetime, cast(cast(TotalMin/60 as int) as nvarchar(50)) + ' : ' + cast(TotalMin%60 as nvarchar(50))),108) AS TotalMin
but it shows below error. Not in SQL Server but when I run code in c#:
Conversion failed when converting date and/or time from character
string.
You can use this code to get the string in SQL Server. It will convert hours and minutes and add 00 for seconds as you don't have them (you're using integer value for minutes):
declare #min int = 465
select right('0' + cast(#min/60 as varchar(2)),2)
+ ':'
+ right('0' + cast((#min - (#min/60)*60) as varchar(2)),2)
+ ':00'
It will work for up to 5999 minutes (99 hours and 59 minutes).
If you need a Unicode version:
declare #min int = 465
select right(N'0' + cast(#min/60 as nvarchar(2)),2)
+ N':'
+ right(N'0' + cast((#min - (#min/60)*60) as nvarchar(2)),2)
+ N':00'
Try this:
TimeSpan t = TimeSpan.FromMinutes( TotalMin);
and see this for more
UPDATE MyTable SET MyDate = CONVERT(datetime, '2009/07/16 08:28:01', 120)
For a full discussion of CAST and CONVERT, including the different date formatting options, see the MSDN Library Link below:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
This will help you
You want to multiply out to milliseconds as the fractional part is discarded.
SELECT DATEADD(ms, 121.25 * 1000, 0)
If you want it without the date portion you can use CONVERT, with style 114
SELECT CONVERT(VARCHAR, DATEADD(ms, 121.25 * 1000, 0), 114)
I am working with a canonical date in the format 2011-10-24 00:00:00.000.
There is a SQL Server statement that I can run to get the date in the format in that I need it in.
SELECT
CONVERT(VARCHAR(50), CONVERT(datetime, '2011-10-24 00:00:00.000', 120), 101)` as test
Returns
10/24/2011.
Here is my question.
Is there a way to do this, convert '2011-10-24 00:00:00.000' to 10/24/2011, in .Net (C# or VB) ?
Why don't you just select it as a date instead of converting to VARCHAR(50) ?
If That's not an option then cast it to a date (DateTime.Parse) and use the properties on the datetime.
In VB
Dim dt As Date = Date.Parse("2011-10-24 00:00:00.000")
Dim newDateString as string = dt.ToShortDateString()
In C#
System.DateTime dt = System.DateTime.Parse("2011-10-24 00:00:00.000");
string newDateString = dt.ToShortDateString();
VB.Net
Dim newDate As string = CDate(fromdb).ToShortDateString
c#
string newDate = DateTime.Parse(fromdb).toShortDateString();
pull it from db as a DateTime , let c# / vb.net convert for you
You can parse input string to DateTime object with DateTime.Parse and then format it as you wish with DateTime.ToString method.
I need to convert date time to nvarchar on sql server like 2011 Oct 24
but I'm not able to convert into this. Can anyone help?
There's no format that matches exactly. You can use substring to assemble the format manually:
select substring(convert(varchar(20),getdate(),106),8,4) + ' ' +
substring(convert(varchar(20),getdate(),106),4,3) + ' ' +
substring(convert(varchar(20),getdate(),106),1,3)
N.B. Doing formatting in SQL is much harder than client-side, in C# or Java or Ruby.
For, exact format you required, you need to get all parts(day,month and year) of date seperately like :
SELECT DATENAME(YYYY,GETDATE()) + ' ' + CAST(DATENAME(MM,GETDATE()) AS VARCHAR(3)) + ' ' + DATENAME(DD,GETDATE())
You can also user convert function for other formats like :
SELECT CONVERT(VARCHAR(20),GETDATE(),106)
SELECT CONVERT(nvarchar(10), getdate(), 102)
You can find all of the conversion codes here:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
WITH d (d) AS (SELECT GETDATE())
SELECT DATENAME(YEAR, d) + ' ' + CONVERT(NVARCHAR(6), d, 109)
FROM d
Output:
-------------------------------------
2011 Oct 24
The problem is that the dates are stored in the SQL database as nvarchar() and the times are stored in a separate column. I only have read access to this database, so I can't change the format. Besides, it would probably void our support if the manufacturer found out.
As I see I need to first combine the dates and times into one cell as a datetime and then subtract the previous row from the current.
ActualTime, ActualDate
5:30:26, 31-Dec-09
16:01:47, 31-Dec-09
17:35:50, 31-Dec-09
18:31:31, 31-Dec-09
18:51:03, 31-Dec-09
18:55:35, 31-Dec-09
19:26:53, 31-Dec-09
5:25:37, 1-Jan-10
5:38:36, 1-Jan-10
5:46:58, 1-Jan-10
6:27:00, 1-Jan-10
Several people have asked what language I was using. I was hoping to do all of this at the server. On the code side (C#) it's a trivial problem.
Like I said I am looking for an SQL Server server-side solution.
In Microsoft SQL Server, to convert your columns in a date you can
Select Cast( ActualDate + ' ' + ActualTime AS DateTime)
to compare between two dates
Select
Datediff(
second,
Cast('13-dec-2009 ' + '19:39:33' As DateTime),
Cast('13-dec-2009 ' + '19:26:33' As DateTime)
)
More on DATEDIFF (Transact-SQL) parameters.
And to get the difference from the current date/time use the GETDATE(),
Select
*,
oldness = DateDiff(
second,
GETDATE(),
Cast(ActualDate + ' ' + ActualTime AS DateTime)
)
From
your_table
Finally to do it between rows (for the whole table..),
Select *,
Cast(ActualDate + ' ' + ActualTime AS DateTime) as [fulldate],
DiffFromPrevious = Coalesce(
DateDiff(
second,
(
Select Top 1 Cast(ActualDate + ' ' + ActualTime AS DateTime) AS [fulldate]
From yourtable
Where Cast(ActualDate + ' ' + ActualTime AS DateTime) < Cast(t1.ActualDate + ' ' + t1.ActualTime AS DateTime)
Order By [fulldate] Desc
),
Cast(ActualDate + ' ' + ActualTime AS DateTime)
),
0)
From
yourtable t1
Order By
[fulldate] Asc
What language are you using, and what kind of Database is it? I'm not sure if the database has capabilities to do row manipulation within a query (subtracting one row from the other), so you would have to do this programmatically. I'm not sure what language you're using, but if it has a Date or Time API then you can use that to create a Date object. There should a function that returns the total number of seconds since a starting date (January 1, 1970 or something). You create your two Date objects, convert into number of seconds and then subtract them. You can then calculate the number of days between them.
If you're using PHP, I suggest you use the strtotime() function to convert it into a time object. Do this for both dates. The difference will give you the number of seconds between them.
This is my statement that throws Exception saying
Can't convert from string to smalldatetime value
How do I fix it or write the correct statement?
da = new SqlDataAdapter("SELECT name,[build-id],exitTime,enterTime,tagType FROM Employees,GateLogging WHERE GateLogging.tagType='Employee' AND enterTime=DATEDIFF(minute,GateLogging.enterTime,GETDATE())>10", MyConn);
In the WHERE clause, remove the single quotes around the DATEDIFF(minute,GateLogging.enterTime,GETDATE())>10 statement.
EDIT
You're also comparing a datetime field against what I'd call a boolean. Remove the enterTime=. Your statement should look like this:
da = new SqlDataAdapter("SELECT name,[build-id],exitTime,enterTime,tagType FROM Employees,GateLogging WHERE GateLogging.tagType='Employee' AND DATEDIFF(minute,GateLogging.enterTime,GETDATE())>10", MyConn);
EDIT 2
Your table definition is as follows:
tagID bigint
enterTime nchar(10)
exitTime nchar(10)
date nchar(10)
Of course, enterTime can not be used in DATEDIFF, as it is not a DATETIME.
Question: Why are you storing dates and times as NCHAR(10) instead of DATETIME? That's not good style!
Solution 1: Change enterTime and exitTime to DATETIME and you're fine.
Solution 2: Change your statement, so that you convert enterTime to a valid DATETIME. I assume that enterTime only contains the time of day, so you'd have to mix in the date part before converting.
EDIT 3
Assuming that date stores the day in format yyyymmdd and enterTime stores the time in format hh:mm:ss, you'll be able assemble a DATETIME:
CONVERT(DATETIME, SUBSTRING(date, 1, 4) + '-' + SUBSTRING(date, 5, 2) + '-' + SUBSTRING(date, 7,2) + ' ' + entryTime, 102)
So your statement from above would look like:
da = new SqlDataAdapter(
"SELECT name,[build-id],exitTime,enterTime,tagType
FROM Employees,GateLogging
WHERE GateLogging.tagType='Employee' AND
DATEDIFF(minute,CONVERT(DATETIME, SUBSTRING(date, 1, 4) + '-' + SUBSTRING(date, 5, 2) + '-' + SUBSTRING(date, 7,2) + ' ' + entryTime, 102),GETDATE())>10", MyConn);
In case the date/time format stored in the fields of your database are different, you'll have to adjust the SUBSTRING statements within the CONVERT() accordingly.
First thing you were putting DATEDIFF stuff in quotes (') and comparing it with enterTime which I suspect is smalldatetime type so you got the error. Correct SQL will be:
SELECT name,[build-id],exitTime,enterTime,tagType
FROM Employees,
GateLogging
WHERE
GateLogging.tagType='Employee'
AND
enterTime = DATEDIFF(minute,GateLogging.enterTime,GETDATE())
As for your >10 I think you should not compare it with enterTime but use this instead:
SELECT name,[build-id],exitTime,enterTime,tagType
FROM Employees,
GateLogging
WHERE
GateLogging.tagType='Employee'
AND
DATEDIFF(minute,GateLogging.enterTime,GETDATE()) >10
I'd also not for you that you're not using ANSI style join syntax - this might cause you a few problems in later life.