Getting date string from getdate method - sql

I need date string using sql statement like..
select getDate()
this will return 2010-06-08 16:31:47.667
but I need in this format 201006081631 = yyyymmddhoursmin
How can I get this?
Thanks

One way
select left(replace(replace(replace(
convert(varchar(30),getDate(),120),' ',''),'-',''),':',''),12)
or like this
select replace(replace(replace(
convert(varchar(16),getDate(),120),' ',''),'-',''),':','')
or
select convert(varchar(8), getdate(),112) +
(replace(convert(varchar(5), getdate(),108),':',''))
See also: CAST and CONVERT (Transact-SQL)

Another way...
DECLARE #d DATETIME
SELECT #d = '2010-06-09 1:37:58.030'
Select Convert(BigInt, 100000000) * Year(#d)
+ Month(#d) * 1000000
+ Day(#d) * 10000
+ DatePart(Hour, #d) * 100
+ DatePart(Minute, #d)
The returned data type here is a BigInt.

Using DATEPART:
SELECT CAST(DATEPART(yyyy, x.dt) AS VARCHAR(4)) +
CASE WHEN DATEPART(mm, x.dt) < 10 THEN '0'+ CAST(DATEPART(mm, x.dt) AS VARCHAR(1)) ELSE CAST(DATEPART(mm, x.dt) AS VARCHAR(2)) END +
CASE WHEN DATEPART(dd, x.dt) < 10 THEN '0'+ CAST(DATEPART(dd, x.dt) AS VARCHAR(1)) ELSE CAST(DATEPART(dd, x.dt) AS VARCHAR(2)) END +
CASE WHEN DATEPART(hh, x.dt) < 10 THEN '0'+ CAST(DATEPART(hh, x.dt) AS VARCHAR(1)) ELSE CAST(DATEPART(hh, x.dt) AS VARCHAR(2)) END +
CASE WHEN DATEPART(mi, x.dt) < 10 THEN '0'+ CAST(DATEPART(mi, x.dt) AS VARCHAR(1)) ELSE CAST(DATEPART(mi, x.dt) AS VARCHAR(2)) END
FROM (SELECT '2010-06-08 16:31:47.667' dt) x
For SQL Server 2005+, I'd look at creating a CLR function for format a date -- the C# DateTime.ToString() supports providing a more normal means of formatting the date.

Related

SQL SERVER -- YEAR

Select (SELECT YEAR(GETDATE())+(Select('-'))+(SELECT YEAR(GETDATE())+1));
I want output as "2018-2019".
Anyone?
You can use + to append string since you are working with SQL 2008, higher version has CONCAT() function :
SELECT CAST(YEAR(GETDATE()) AS VARCHAR(4)) + ' - ' + CAST(YEAR(DATEADD(YEAR, 1, GETDATE())) AS VARCHAR(4))
Use datename():
select (datename(year, getdate()) + '-' + datename(year, dateadd(year, 1, getdate()))
That way, you don't have to deal with converting integers to strings.
SELECT CONCAT(YEAR(GETDATE()), '-', YEAR(GETDATE()) + 1)
CONCAT takes care of conversion to string, so you don't have to worry about that.
edit: Unfortunately I have been made aware that CONCAT is only available in sql server 2012 and higher.
You can also use
SELECT CAST(YEAR(GETDATE()) AS VARCHAR(4)) +
'-' +
CAST(YEAR(GETDATE()) + 1 AS VARCHAR(4))
OR
SELECT CAST(DATEPART(Year,GETDATE()) AS VARCHAR(4)) +
'-' +
CAST(DATEPART(Year, GETDATE()) + 1 AS VARCHAR(4))

How do you update Year field in the datetime column using another datetime column?

I have two columns (Birth_date and Purchase_date) and would like to create a column (Birth_date_update) using following conditions
Conditions:
When Birth_date is later than Purchase_date, take birth_date.
When Birth_date is earlier than Purchase_date, Birth_date_update will take the year from purchase_date and override year from birth_date.
If the final value from condition 2 is still earlier than purchase_date, than find next birth_date anniversary that is bigger than purchase date.
Note: Condition 2 and 3 can be summarized as find earliest anniversary of birth_date greater than purchase_date.
Birth_date purchase_date Birth_date_update
2002-02-21 2006-05-11 2007-02-21
2004-01-18 2004-01-25 2005-01-18
2011-07-24 2011-04-09 2011-07-24
2006-12-16 2007-10-08 2007-12-16
2007-04-30 2008-03-14 2008-04-30
I have no idea how to code this.. please help!
update my_table
set birth_date_update = case
when birth_date > purchase_date then birth_date -- Condition 1
when birth_date < purchase_date then dateadd(year, datediff(year, birth_date, purchase_date), birth_date) -- Condition 2
when dateadd(year, datediff(year, birth_date, purchase_date), purchase_date) < purchase_date
then dateadd(year, 1, dateadd(year, datediff(year, birth_date, purchase_date), purchase_date)) end -- Condition 3
Result from above answer doesn't seem to be correct. Looks like Condition 3 is not working consistent. So I added my answer
create table TAB1 (BD date, PD date, BDU date)
insert into TAB1 values
('2002-02-21', '2006-05-11', NULL),
('2004-01-18', '2004-01-25', NULL),
('2011-07-24', '2011-04-09', NULL),
('2006-12-16', '2007-10-08', NULL),
('2007-04-30', '2008-03-14', NULL)
;with cc as (
select BD, PD,
case
when BD > PD then BD
when BD < PD then cast(cast(year(PD) as varchar) + '-' + cast(month(BD) as varchar) + '-' + cast(day(BD) as varchar) as date)
end as BDU
from TAB1
)
, cc2 as (
select BD, PD,
case
when BDU < PD then dateadd(day, 365, BDU)
else BDU
end as BDU
from cc
)
update T1
set BDU = cc2.BDU
from TAB1 T1
inner join cc2 on cc2.BD = T1.BD and cc2.PD = T1.PD
select * from TAB1
I do not want to be left out of this one. Here is my answer
UPDATE dbo.BirthDateTable SET Birth_date_update =
CASE WHEN Birth_date >= purchase_date THEN Birth_date -- Condition 1
WHEN Birth_date < purchase_date
THEN CASE
WHEN CONVERT(Date , CAST( DATEPART(YEAR,purchase_date) AS Varchar(4)) + '-' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)) + '-' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2))) > purchase_date
THEN CONVERT(Date , CAST( DATEPART(YEAR,purchase_date) AS Varchar(4)) + '-' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)) + '-' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2))) -- Condition 2
ELSE DATEADD(YEAR,1,CONVERT(Date , CAST( DATEPART(YEAR,purchase_date ) AS Varchar(4)) + '-' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)) + '-' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2)))) -- Condition 3
END
END
Here is a select example.
SELECT
CASE WHEN Birth_date >= purchase_date THEN Birth_date -- Condition 1
WHEN Birth_date < purchase_date
THEN CASE
WHEN CONVERT(Date , CAST( DATEPART(YEAR,purchase_date) AS Varchar(4)) + '-' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)) + '-' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2))) > purchase_date
THEN CONVERT(Date , CAST( DATEPART(YEAR,purchase_date) AS Varchar(4)) + '-' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)) + '-' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2))) -- Condition 2
ELSE DATEADD(YEAR,1,CONVERT(Date , CAST( DATEPART(YEAR,purchase_date ) AS Varchar(4)) + '-' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)) + '-' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2)))) -- Condition 3
END
END
FROM dbo.BirthDateTable
Other post about above answer not being correct was written when the order of the answers was different on the page. I believe this answer meets the criteria.#JamieHong, could you see if this select works on your system? I padded the month and day with zeros.
SELECT
CASE WHEN Birth_date >= purchase_date THEN Birth_date
WHEN Birth_date < purchase_date
THEN CASE
WHEN CONVERT(Date , CAST( DATEPART(YEAR,purchase_date) AS Varchar(4)) + '-' + RIGHT('00' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)),2) + '-' + RIGHT('00' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2)),2)) > purchase_date
THEN CONVERT(Date , CAST( DATEPART(YEAR,purchase_date) AS Varchar(4)) + '-' + RIGHT('00' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)),2) + '-' + RIGHT('00' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2)),2))
ELSE DATEADD(YEAR,1,CONVERT(Date , CAST( DATEPART(YEAR,purchase_date ) AS Varchar(4)) + '-' + RIGHT('00' + CAST(DATEPART(MONTH,Birth_date) AS Varchar(2)),2) + '-' + RIGHT('00' + CAST(DATEPART(DAY, Birth_date) AS Varchar(2)),2)))
END
END AS CalcDate
FROM dbo.BirthDateTable

SQL - Run second Query if count result = 0

Both my SELECT queries work below and return values with as two separate results. What I would like to do is if the first SELECT statement returns a 0 and it will sometimes due to the nature of our work, I would like the second to run ignoring the first one that returns zero if possible. IF the first one returns a result greater than zero then I do not want the second to run.
I have been all over looking and tried different IF ELSE and such but because of the specific date/time formulas used I am thinking it may not be possible or I this may have to be re-written altogether. There is a purpose for this but I will not bore you. Thanks in advance!
Declare #StartDate as DateTime
Declare #EndDate as DateTime
Declare #TodaysDate as DateTime
Declare #Previous as DateTime
Declare #Previous2 as DateTime
set #TodaysDate = GETDATE()
set #Previous = DATEADD(day,-1,#TodaysDate)
set #Previous2 = DATEADD(day,-2,#TodaysDate)
-- SELECT Statetment one starts here
set #StartDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, #Previous)) + '-' +
convert(varchar(2), datepart(dd, #Previous)) + ' ' +
'05:00' as datetime)
set #EndDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, #Previous)) + '-' +
convert(varchar(2), datepart(dd, #Previous)) + ' ' +
'16:59' as datetime)
SELECT Count(*) as FirstShfitPrevious
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component]
WHERE Close_Time_Stamp between #StartDate and #EndDate
-- Query 2 Starts, Declarations already made at beginning
set #StartDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, #Previous2)) + '-' +
convert(varchar(2), datepart(dd, #Previous2)) + ' ' +
'05:00' as datetime)
set #EndDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, #Previous2)) + '-' +
convert(varchar(2), datepart(dd, #Previous2)) + ' ' +
'16:59' as datetime)
SELECT Count(*) as FirstShfitPrevious2
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component]
WHERE Close_Time_Stamp between #StartDate and #EndDate
You can try to store the value of your first query result in a variable and then then check the value like
declare #myVar int
SELECT #myVar = Count(*) as FirstShfitPrevious
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component]
WHERE Close_Time_Stamp between #StartDate and #EndDate)
if(#myVar = 0)
//Here your second query logic
begin
set #StartDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, #Previous2)) + '-' +
convert(varchar(2), datepart(dd, #Previous2)) + ' ' +
'05:00' as datetime)
set #EndDate = cast(convert(varchar(4), datepart(yyyy, getdate())) + '-' +
convert(varchar(2), datepart(mm, #Previous2)) + '-' +
convert(varchar(2), datepart(dd, #Previous2)) + ' ' +
'16:59' as datetime)
SELECT Count(*) as FirstShfitPrevious2
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component]
WHERE Close_Time_Stamp between #StartDate and #EndDate
end
use NOT Exists..
if NOT Exists
(Select 1 as FirstShfitPrevious--first query
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component]
WHERE Close_Time_Stamp between #StartDate and #EndDate
)
Begin
SELECT Count(*) as FirstShfitPrevious2--second query
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component]
WHERE Close_Time_Stamp between #StartDate and #EndDate
END
Else
Begin
--First query
End
or even that way:
SELECT TOP 1 Count(*) as FirstShfitPrevious2
FROM [TOL_PROD_DB].[dbo].[tblLOT_CTRL_Active_Component]
WHERE Close_Time_Stamp between #StartDate and #EndDate
GROUP BY CAST(Close_Time_Stamp AS DATE)
ORDER BY CAST(Close_Time_Stamp AS DATE) DESC
with period covering both date ranges.

Sql Server select datetime without seconds

I have datetime column value below
2015-01-04 20:37:00.000
I tried below
cast(cast(MyDateColumn as date) as datetime)+cast(datepart(hour,MyDateColumn ) as float)/24
as MyDateColumn
and
CAST(CONVERT(CHAR(16),MyDateColumn,113) AS datetime) as MyDateColumn
These are did not work for me
How can i get above datetime as 01-04.2015 20:37 ?
Since MS SQL 2012, you can use FORMAT,
SELECT FORMAT([MyDateColumn], 'dd-MM.yyyy HH:mm')
In MYSQL it will work
SELECT DATE_FORMAT(date, '%Y-%m-%d %H:%i') AS formated_date FROM table;
In MS SQL It will work
SELECT FORMAT(getdate(), 'dd-mm-yyyy HH:mm')
In SQL Server this will work:
DECLARE #now [datetime];
SET #now = GETDATE();
SELECT
CONVERT([varchar](10), #now, 105) + ' ' +
RIGHT('0' + CONVERT([varchar](2), DATEPART(HOUR, #now)), 2) + ':' +
RIGHT('0' + CONVERT([varchar](2), DATEPART(MINUTE, #now)), 2);
In SQL Server this should do the trick:
declare #dt datetime = '2015-01-04 20:37:00.000'
select right('0' + cast(DATEPART(MM, #dt) as varchar), 2) + '-'
+ right('0' +cast(DATEPART(DAY, #dt) as varchar), 2) + '.'
+ cast(DATEPART(YEAR, #dt) as varchar) + ' '
+ right('0' +cast(DATEPART(HOUR, #dt) as varchar), 2) + ':'
+ right('0' +cast(DATEPART(MINUTE, #dt) as varchar), 2)
Simply,
SELECT CAST(CONVERT(varchar, GETDATE(), 100) as datetime)
Here's another way and you get a datetime in return.
SELECT DATEADD(
MILLISECOND,
DATEPART(MILLISECOND, '2016-02-16 13:45:24.573') * -1,
DATEADD(SECOND, DATEPART(SECOND,'2016-02-16 13:45:24.573') * -1,
'2016-02-16 13:45:24.573')
)
this is the way i do it. I needed to get -2 minutes
select CONVERT(datetime, CONVERT(CHAR(18), DATEADD(minute, -2, getdate()) , 113) + '00')
Format(Cast(Convert(varchar(15),Cast(timeval as Time),100) as DateTime),'hh:mm tt') As newtime
This will remove seconds from time as well as add AM,PM with time.

Concatenating two TIME columns in SQL Server 2012

I want to concatenate two TIME columns and show as one column.
Example:
FromTime: 9:00
ToTime: 12:00
Result should be:
9:00-12:00
Generic SQL:
-- hh:mm:ss
SELECT 'result:' + CONVERT(CHAR(6), FromTime, 8) + '-' + CONVERT(CHAR(6), ToTime)
FROM yourTable
MySQL:
-- hh:mm
SELECT 'result:' + DATE_FORMAT(FromTime, '%H:%i') + '-' + DATE_FORMAT(ToTime, '%H:%i')
FROM yourTable
SQL Server:
-- hh:mm
SELECT 'result:' + convert(char(2), DATEPART(hh, FromTime)) + ':' +
CONVERT(CHAR(2), DATEPART(mm, FromTime)) + '-' +
CONVERT(CHAR(2), DATEPART(hh, ToTime)) + ':' +
CONVERT(CHAR(2), DATEPART(mm, ToTime))
FROM yourTable
declare #FromTime time
declare #ToTime time
set #FromTime='9:00'
set #ToTime='12:00'
select cast(#FromTime as varchar(10))+ '-' + cast(#ToTime as varchar(10)) as result
sql demo
You can use convert
select convert(VARCHAR(5),getdate(),108) + ' - ' + convert(VARCHAR(5),getdate()-1,108)