我是一个研究SQL Server项目的MySQL人,试图获取一个datetime字段来显示当前时间.在MySQL中我会使用NOW(),但它不接受.
INSERT INTO timelog (datetime_filed) VALUES (NOW())
Run Code Online (Sandbox Code Playgroud) 我想SELECT GETDATE()在SQL Server 2008中打印,我需要毫秒的时间(这是为了调试目的 - 找到sp的执行时间)
我发现了这个差异
print GETDATE()返回2011-03-15 18:43:44.100 2011-03-15 18:43:44.100返回2011年3月15日下午6:44我认为SQL Server会自动对打印功能进行类型转换.
我需要打印这样的日期 SELECT GETDATE()
谢谢你的帮助.
我有按datetime列过滤的问题.
我尝试了这两种方法:
datefield < '2013-03-15 17:17:55.179'
datefield < CAST('2013-03-15 17:17:55.179' AS datetime)
Run Code Online (Sandbox Code Playgroud)
我有一个超过3.000.000主要对象的大型数据库.
所以我需要提高datetime过滤的性能.我正在阅读有关UNIX时间戳的信息(将所有内容转换datetime为UNIX时间戳,然后按此UNIX字段进行过滤).
我认为这比过滤更好datetime.但如果有人知道其他方式,我会很感激.
我的查询是:
SELECT TOP (100) ev.Title as Event_name, po.Name as POI_name,
po.Address, po.City, po.Region, po.Country, po.Latitude, po.Longitude, ev.Start_time,
(Select ID_Category FROM SubCategory s where ev.ID_SubCategory = s.ID_SubCategory) as ID_Category,
ev.ID_SubCategory, ev.ID_Event, ev.ID_Channel, IDChanelEvent,
ev.FavoriteCount, po.gmtOffset, v.IsFavorite, v1.IsFavorite
FROM Events ev
JOIN POI po ON ev.ID_POI = po.ID_POI
JOIN (SELECT et.id_event as joinIdEv FROM EventTagLink et, tags t
WHERE …Run Code Online (Sandbox Code Playgroud) 在SQL Server 2008中,为什么以下查询返回相同的值?
-- These all return 2011-01-01 23:59:59.997
SELECT CAST('2011-01-01 23:59:59.997' as datetime)
SELECT CAST('2011-01-01 23:59:59.998' as datetime)
Run Code Online (Sandbox Code Playgroud)
为什么下面的查询会在第二天进行查询?
-- Returns 2011-01-02 00:00:00.000
SELECT CAST('2011-01-01 23:59:59.999' as datetime)
Run Code Online (Sandbox Code Playgroud) 最近我尝试做的时候有很奇怪的错误db.SubmitChanges():
SqlDateTime溢出.必须在1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM之间.
关键是,我只用于DateTime.Now在我的对象中设置属性,并在调用后Response.Write(DateTime.Now.ToString());显示17-04-2013 18:03:13它应该是.
它没有发生在早期,现在功能总是打破.我完全无能为力 - 我的SQL服务器上的日期似乎没问题.
可能导致什么呢?
编辑
我不认为这会有所帮助(IMO的任何错误太简单了),但我的功能是:
public bool ReportLogIn(int UserID, string IP, int Succeed ... ) {
A_UserLoginHistory Report = new A_UserLoginHistory();
Report.IP = IP;
Report.UserID = UserID;
Report.Status = Succeed;
Report.Date = DateTime.Now; //the only DateTime field
...
try {
db.A_UserLoginRegistry.InsertOnSubmit(Report);
db.SubmitChanges();
return true;
} catch (Exception e) {
ErrorLog.AddError(e.ToString());
return false;
}
}
Run Code Online (Sandbox Code Playgroud) DbType.DateTime和DbType.DateTime2.NET有什么区别?
如果我正在使用DbType.Date,我想更改为DateTime类型,我应该使用DateTime或DateTime2?
SQL Server中postgresql"timestamp without time zone"的等价类型是什么?
可以使用DateTime吗?
实体框架在异常中给出了通用消息,而没有告诉我确切的实体和导致错误的属性.如何获取有关错误的更多信息?
在许多情况下都会发生这种情况
操作失败:无法更改关系,因为一个或多个外键属性不可为空.当对关系进行更改时,相关的外键属性将设置为空值.如果外键不支持空值,则必须定义新关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象.
和
将datetime2数据类型转换为日期时间数据类型会导致超出范围的值.该语句已终止.
例外细节:
[SqlException(0x80131904):将datetime2数据类型转换为日期时间数据类型导致超出范围的值.语句已终止.] System.Data.SqlClient.SqlConnection.OnError(SqlException异常,Boolean breakConnection)+404 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()+ 412 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior ,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)+2660 System.Data.SqlClient.SqlDataReader.ConsumeMetaData()+59 System.Data.SqlClient.SqlDataReader.get_MetaData()+118 System.Data.SqlClient.SqlCommand.FinishExecuteReader (SqlDataReader的DS,RunBehavior runBehavior,字符串resetOptionsString)6431425个System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,布尔异步)6432994 System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method,DbAsyncResult result)+538 System.Data.SqlClient.SqlCommand.RunExecuteRead er(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method)+28 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior,String method)+256 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)+19 System .Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator,EntityConnection connection,Dictionary
2 identifierValues, List1 generatedValues)+270 System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager,IEntityAdapter adapter)+391[UpdateException:更新条目时发生错误.有关详细信息,请参阅内部异常.] System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager,IEntityAdapter adapter)+11223976 System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)+833 System.Data.Entity .Internal.InternalContext.SaveChanges()+ 218
[DbUpdateException:更新条目时发生错误.有关详细信息,请参阅内部异常.] System.Data.Entity.Internal.InternalContext.SaveChanges()+ 291
我有一台运行PHP的CentOS 7机器,安装了webtatic PHP 5.6和php56w-mssql软件包.我的应用程序位于连接到MSSQL 2012数据库的Laravel框架中.
我已经修改了locales.conf文件,以便它拥有它
[default]
date format = %Y-%m-%d %I:%M:%S.000
Run Code Online (Sandbox Code Playgroud)
当从tsql中运行查询时,我会返回正确格式化的日期,包括最后的毫秒数.但是,当以PHP交互模式或通过工匠修补程序运行查询时,我得到一个没有尾随毫秒的日期格式.
我有一台运行完全相同配置的Ubuntu机器,并且tsql,PHP和artisan tinker都给了我正确格式化的日期,这只是这个CentOS机器的一个问题.
我freetds.conf为它的全局设置设置了tds版本7.4并且具有主机条目(这是Laravel中当前配置的),即
[fancy]
host = 10.0.0.12
port = 1433
database = fancy
tds version = 7.4
client charset = UTF-8
Run Code Online (Sandbox Code Playgroud) 我有一个DateTime列作为主键的表:
USE [idatest]
GO
CREATE TABLE [dbo].[DatesTbl](
[creationDate] [datetime] NOT NULL
CONSTRAINT [PK_DatesTbl] PRIMARY KEY CLUSTERED
(
[creationDate] ASC
))
GO
Run Code Online (Sandbox Code Playgroud)
当我在做entityManager.merge时,我得到重复的PK违规,因为datetime为milisec保存了3位数,但是hibernet将它转换为datetime2,它为milisec保存7位数.在java代码中,我使用LocaDatetime,它为milsec保存10位数.
我已经尝试过在Hibernate MSSQL datetime2映射中解释的解决方案, 但它不起作用:java代码如下所示:pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<groupId>com.example</groupId>
<artifactId>spring-jap-test</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.0.0.jre8</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
DatesTbl类
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class DatesTbl {
@Column(columnDefinition = "DATETIME", nullable = …Run Code Online (Sandbox Code Playgroud)