Different result in view and stored procedure - sql

My table structure like this
I am create view for this table
SELECT Entered FROM dbo.tbPatientReport
In the table data like this
If i execute view from sql query then it will give output like
select Entered from view_abc
Desired Output

You should format date as you want in result.
e.g.
SELECT FORMAT( Entered,'MM/dd/yyyy hh:mm:ss tt','en-US') AS 'US'
FORMAT is one of the new built-in String Function introduced as a Part of Sql Server 2012. It returns the value formatted in the specified format using the optional culture parameter value. It is not an Sql Server native function instead it is .NET CLR dependent function.
SYNTAX: FORMAT ( value, format [, culture ] )
Edit
Can you test this will be working.
SELECT CONVERT(VARCHAR(20), SYSDATETIME(), 22)
Format: MM/DD/YY HH:MI:SS AM
Output: 10/25/13 2:21:00 PM
Only problem is you cannot get FullYear.

Your [Entered] column [Datetime] in your table structure does not allow nulls, it's not ticked.
What version of SQL Management Studio are you using? And are you selecting the data in a plain select query or are you choosing to View the data?
In older versions "View" opens the edit window with your code on top.
While in the editing view the last row will always be NULL as it is expecting new data.

Related

SQLite - TZ format to Date time only using SQL queries

I have a SQLite database with a simple table in the format of:
ID,DateText,Name
1,2020-09-01T18:57:17Z,John
2,2022-12-01T04:00:09Z,Laurel
...
The DateText column is declared as TEXT on the "create table" statement. Using only SQL, I need to:
Create a new column with the DateText data.
Obtain the "oldest" date
Obtain the "newest" date
Note that I need to resolve this with a SQL query. I cannot read into a programming language, parse, and update table--I need to do everything on SQL. For example, SQL Server DateTime with timezone is the opposite, but they are using node.js, something I cannot do.
You can get the oldest and newest using min() and max():
SELECT ID, min(DateTime), Name FROM YourTable; -- Oldest
SELECT ID, max(DateTime), Name FROM YourTable; -- Newest
The nice thing about ISO-8601 date and time format strings is that they sort lexicographically without having to do anything special with them.
These queries would give an error on most SQL database engines because of the mix of non-grouped columns with an aggregate function, but SQLite explicitly will return the row that goes with the minimum or maximum column value. And of course if you don't want the other columns, just leave them out of the SELECT.

Error in MS SQl 11.0 given code: 'unix_timestamp' is not a recognized built-in function name

I got the SQL query from another developer and there was unix_timestamp function, because this query returns a list of objects from database in the exact period of time. Query doesn't work because of the SQL error 195: 'unix_timestamp' is not a recognized built-in function.
n.created>unix_timestamp('2019-02-28') and n.created<unix_timestamp('2019-09-01')
"unix_timestamp" is a built in function on MySQL - it isn't on SQLserver.
Here's the code to get the unix timestamp now:
SELECT DATEDIFF(SECOND,'1970-01-01', GETUTCDATE()) AS unix_timestamp
You can either create a user defined function to do this and use the query as-is or you can just modify the query like so:
n.created>(DATEDIFF(SECOND,'1970-01-01', '2019-02-28')) and n.created<(DATEDIFF(SECOND,'1970-01-01','2019-09-01'))
Unix timestamps are not particularly useful in SQL Server. You can add a computed column to convert this to a datetime data type:
alter table t add created_dt as (dateadd(second, created, '1970-01-01')) persisted;
create index t_created_dt on t(created_dt);
This then allows you to write:
n.created_dt > '2019-02-28' and
n.created_dt < '2019-09-01';
Otherwise, the solution by motobatsu is a good solution.

tSQL - convert nvarchar(8) to time only

I am using Microsoft SQL Server Management Studio to manipulate Public transport system database.
I have a table in database named 'dbo.tblStop_times', two columns of which should be of data type time. At the moment, they are both nvarchar and they have data stored in a pattern like this - "07:39:00" (without quotes)
I had same question with date columns as well, but found a solution for that on stackoverflow just few hours back.
Below works fine for conversion of nvarchar column to date column.
ALTER TABLE tblCalendar ALTER COLUMN [start_date] DATE NOT NULL;
I am not sure if what I want is achievable or not, because the above mention conversion works just fine, I assume it might be possible.
What I have atm is - nvarchar(8), what I want it to be is sql time data type, something like hh:mm:ss [and if possible - without trailing nnnnnn - nanoseconds component]
You should be able to do:
ALTER TABLE tblStop_times ALTER COLUMN start_time TIME NOT NULL;
Here is a rextester.
EDIT:
If you don't have valid time values, then you have a problem. You should first look for the values:
select col
from tblStop_times
where try_convert(time, col) is null;
This will show you the values that cannot be converted. If you like, you can NULL them out so the alter will work:
update tblStop_times
set col = NULL
where try_convert(time, col) is null;

Set default value for parameter in pentaho report designer

I am creating a report in pentaho report designer and need some help setting default values for a parameter that I've created.
One of the parameters labeled date fetches data from the date column of a table. While I am able to view all the dates in the drop down list, I am unable to find a way in which I can set the default value of this drop down to all (meaning all the dates together).
Is there a way in which I can set the 'all' value as default?
Assuming that you get the values for the filter from a JDBC connection in PRD, you can write a query like this. (I have used Postgresql).
This will load 'All' as the first values in the drop down and other distinct date values from your table. (Do the casting properly)
SELECT 1 AS sort,'All' AS date
UNION
SELECT DISTINCT 2 AS sort,
tablename.datecolumn::date AS date
FROM
tablename
ORDER BY sort
Then in your parameter that is shown to the user to select the date, enter All in Default Value field and select the query that we have written.
Next assuming that you use a KTR to retrieve data to your report, you can include the following query in a 'Table input' step,
(tablename.datecolumn IN (SELECT CASE WHEN('${date}' = 'All' OR '${date}' IS NULL) THEN tablename.datecolumn ELSE '${date}' END))
Hope this will help. If you have any further issues please comment below. Because this has worked perfectly for me.

Convert a string to a date in Access

I'm migrating data between tables in Access 2003. In the old table, the date was stored as a text field in the format YYYYMMDD.
I want to store the field as a datetime in the new table. I've tried using CDate() in my SQL statement, but it just displays as #Error in the results.
What am I doing wrong?
e.g. cdate(format("20091231", "####/##/##"))
So, in your case it will be
SELECT cdate(format(mystringFieldThatIsInYYYYMMDDFormat, "####/##/##"))
FROM myData