sql字符串转换成日期

如题所述

sql字符串转换成日期语句:日期=convert(datetime,字符串)。

CONVERT ()语句的用途是将一种数据类型的表达式转换为另一种数据类型的表达式。格式是CONVERT ( data_type [ ( length ) ] , expression [ , style ] )。

expression:任何有效的表达式。

data_type:目标数据类型。这包括 xml、bigint 和 sql_variant。不能使用别名数据类型。

length:指定目标数据类型长度的可选整数。默认值为 30。

style:指定 CONVERT 函数如何转换 expression 的整数表达式。如果样式为 NULL,则返回 NULL。该范围是由 data_type 确定的。

返回类型:返回转换为 data_type 的 expression。

扩展资料:

如果 expression 为 date 或 datetime 数据类型,则其他值作为 0 进行处理。SQL Server 使用科威特算法来支持阿拉伯样式的日期格式。

style 将datetime 和smalldatetime数据转换为字符串时所选用的由SQL Server 系统提供的转换样式编号,不同的样式编号有不同的输出格式;一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,char,varchar)相互转换的时候才用到。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-08-25
您查看一下convert的帮助就知道了。
120或者121的格式是YYYY-MM-DD
而您这个格式是日月年的格式,使用105
SELECT convert(datetime,'30-03-2011',105)
第2个回答  2011-12-11
import java.util.Date;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;

public class test{
public static void main(String arg[]){
Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
String date_str=df.format(date);
int isUpdate=0;
Connection conn=null;
PreparedStatement pstmt=null;
String sql="insert into test_A(table_time) values('"+date_str+"')";
System.out.println(date_str);
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test","sa","sa");
pstmt=conn.prepareStatement(sql);
isUpdate=pstmt.executeUpdate();
if(isUpdate==1){
System.out.println("ok");
}
else{
System.out.println("no");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
java.util.Date类型所取出来的是一种当前时间的国际表示格式,要放在数据库里需要用simpledateformat这个类把他格式化一下然后插入.记得运行上面类的时候把类名还有你自己数据库的配置改掉~
第3个回答  2011-12-12
select convert(datetime,你的字符串或者列名) from 表名
第4个回答  2011-12-15
Date date = new Date();
DateFormat dateformat = DateFormat.getDateInstance();
String newDate = dateformat.format(date);
System.out.println(newDate);

数据库的日期大概都是DateFormat.getDateInstance()后的格式:yyyy-mm-dd

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网