Add wrapper on date columns in SQL server - database

I have DB with tables having datetime columns.
Can I add wrapper over those dateTime columns which I can activate/deactivate as per my need, so that all date time converted to a specific Timezone before retrieval.
e.g. I have a Table ABC with column ExpDate having value '12-10-2019 2:30'
When I execute
select * from ExpDate
then it will result
'12-09-2019 19:30' (after converting it from UTC to PST(UTC-7))
I want this solution be generic so that it get implemented for all dataTime column in all tables.

Related

T-SQL MIN on Date type resulting in INT

I have a table with a column called ThisDayOfTherapy which is of datatype DATE in SQL Server.
Later, I have an aggregate query using Min(ThisDayOftherapy). This query populates a #temp table with the result of Min(ThisDayOfTherapy) as column name StartDate_Min
In a 3rd location I am finally doing something with StartDate_Min. When I hold my mouse over StartDate_Min, it says the datatype is INT.
Shouldn't it simply be DATE? Will it still work as if it were a date for all practical purposes?
I see the same behavior, but I think you will be fine. SQL Server would not execute any date functions if it was not a valid date. Adding one day to my MinDate column from my temp table is working as expected.

Convert unix date to utc [duplicate]

I have a problem in converting the Unix timestamp to sql server timestamp.
I have a data in excel sheet and I will import that data through a tool. So I am looking for a code or syntax which can convert that Epoch timestamp to sql server timestamp.
I have 3 different columns with the same format. How can I change the values in those columns.
For Example:
Epoch timestamp ---1291388960
sql server timestamp--- 2010-12-03 15:09:20.000
I have 3 different columns with the same format. How can I change the values in those columns.
To update 3 columns in a table, you can pair DATEADD seconds to the epoch (1 Jan 1970) with the column name, i.e.
update tbl set
datetimecol1 = dateadd(s, epochcol1, '19700101'),
datetimecol2 = dateadd(s, epochcol2, '19700101'),
datetimecol3 = dateadd(s, epochcol3, '19700101')
You can't update in place since a bigint column cannot also be a datetime column. You have to update them into 3 other columns.
Use the DATEADD function:
SELECT DATEADD(ss, 1291388960, '19700101')
...specifying a date of January 1st, 1970. In this example, it was provided in the YYYYMMDD format.
DATEADD will return a DATETIME data type, so if you have a table & column established -- you can use the function to INSERT/UPDATE depending on your needs. Provide details, and I'll clarify. Once you have a DATETIME to work with, you can use CAST or CONVERT to format the date in TSQL.

Power Query Calendar

I am trying to create a calendar using Power Query functions and for that I used below syntax in blank query:
Source= Duration.TotalDays(DateTime.LocalNow() - #datetime(2014,01,01,00,00,00)) * 24
Date= List.DateTimes(#datetime(2014,01,01,00,00,00), Source ,#duration(0,1,0,0))
Then I convert to a table and apply query.
Connect dimension date table to date column in fact table.
 
The error occurs when I’m trying to mark table as date table:
‘The date column can only gave one timestamp per day. The date column
can’t have gaps in dates’
 
What I have done wrong?
As the error message says:
The date column can only have one timestamp per day.
While you are trying to add 24, one for each hour. See the requirements for setting a table as a date table:
if it is a Date/Time data type, it has the same timestamp across each value
i.e. you can have only one value for each date, and if it is not a date, but datetime value, all time values should be the same.

SQL Server: How to query date in a certain format

I am pretty new to SQL and hope someone here can help me with the following:
I have a table in SQL Server with one column storing dates and I have a simple stored procedure to select data from this table.
Currently it fetches the date including hours and minutes.
How can I achieve that it fetches the date in format yyyy-mm-dd (without the hours and minutes) ?
My column is called "logDate" and my table is called "logTable".
Thanks for any help with this, Tim
If you only want to store the date, convert the column to a date type, not a datetime.
If you want to keep the date and time in the data, but just display the date
select convert(date, logDate) from logTable
If you want to return the date as a string, use convert
select convert(varchar(10), logDate, 120) from logTable

T-SQL - changing datetime to date data type?

I have a column BirthDate in a table that is using the datetime data type. Currently, the values resemble the format 1987-12-30 00:00:00.000.
I would like to update all the rows of this table, changing them to the following format with the date data type: 1987-12-30
I can run a SELECT...
SELECT CONVERT(date, BirthDate) FROM CUSTOMERLIST
But this is only affecting the display. I would like for it to update the entire column and also change the data type of that attribute as well. Any assistance would be greatly appreciated!
You will need to ALTER the table:
ALTER TABLE CUSTOMERLIST ALTER COLUMN BirthDate date not null
See Sql Fiddle with demo

Resources