1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SQL Severで日付フォーマットを変更

Last updated at Posted at 2017-04-21

■unixtimeからdatetimeに

--UTC
dateadd(s,1490972400,'1970-01-01')
--JST(msがない場合)
dateadd(s,1490972400,'1970-01-01 9:00:00')
--JST(msまである場合1)
dateadd(s,cast(substring(cast(1508203163081 as varchar),1,10) as bigint),'1970-01-01 9:00:00')
--JST(msまである場合2。やっていることは1と同じでms以下の部分を切り捨てている)
dateadd(s,1508203163081/1000,'1970-01-01 9:00:00')
--上記で「expression をデータ型 int に変換中に、算術オーバーフロー エラーが発生しました。」みたいなエラーが発生する場合は、intの範囲を超えている行があるので、bigintか何かにCASTしてやればOK
dateadd(s,cast(1508203163081 as bigint)/1000,'1970-01-01 9:00:00')

■datetimeからunixtimeに

UNIX_TIMESTAMP('2017-04-01 00:00:00')

■datetimeからdateに

CONVERT(date, '2017-04-01 00:00:00')
1
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?