Changing positions of an integer

Asked

Viewed 44 times

0

Citation

I am building an sql where I have the following information yyyymmdd 20190327 is in the database as integer, I need to convert into ddmmyyyy 27032019 continuing to be an integer in select. Does it have a function to do that?

Solved:

CONVERT(INT,REPLACE(CONVERT(varchar,CONVERT(date, convert(varchar(10), 20190327)),105),'-',''))

2 answers

2

You can use the convert:

select CONVERT(datetime, convert(varchar(10), 20190327));

To convert the date to number you can do so:

select CAST(CONVERT(varchar(8),getdate(),112) as int)

That is, it converts to varchar first and deopis to int. Why the 112?
Because in function convert, this is the format for date yyyymmdd:

SQL-Server Cast and Convert

Here the fiddle running: Sqlfiddle

  • That way I convert into date, now I have to take the date "2019-03-27T00:00:00Z" and convert into an integer 27032019

  • I edited the question with an example to convert the date to number.

0

Staying in the purely numerical context, here’s another solution:

-- código #1
SELECT ((coluna % 100) * 1000000) +           -- dia
       (((coluna / 100) % 100) * 10000) +     -- mês
       (coluna / 10000)                       -- ano
  from ...
  where ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.