sql-server-2005

How to convert datetime to date only (with time set to 00:00:00.000)

橙三吉。 提交于 2019-12-21 08:23:54
问题 I have a string '2009-06-24 09:52:43.000', which I need to insert to a DateTime column of a table. But I don't care about the time, just want to insert it as 2009-06-24 00:00:00.000 How can I do that in T-SQL? 回答1: For SQL Server 2005 and below: CONVERT(varchar(8), @ParamDate, 112) -- Supported way CAST(FLOOR(CAST(@ParamDate AS float)) AS DATETIME) -- Unsupported way For SQL Server 2008 and above: CAST(@ParamDate AS DATE) 回答2: declare @originalDate datetime select @originalDate = '2009-06-24

how to format getdate into YYYYMMDDHHmmSS

萝らか妹 提交于 2019-12-21 07:31:55
问题 In SQL Server how do I format getdate() output into YYYYMMDDHHmmSS where HH is 24 hour format? I've got the YYYYMMDD done with select CONVERT(varchar,GETDATE(),112) but that is as far as I got. Thanks. 回答1: select replace( replace( replace(convert(varchar(19), getdate(), 126), '-',''), 'T',''), ':','') 回答2: Just for anyone searching for this functionality that has SQL Server 2012 you can use the FORMAT function: SELECT FORMAT ( GETDATE(), 'yyyyMMddHHmmss') AS 'Custom DateTime' This allows any

How do I retain NULL values when using SSIS to import from flat file in SQL Server 2005

天大地大妈咪最大 提交于 2019-12-21 07:15:14
问题 I've exported records to a flat file delimited by "|" and it seems that when I import those records into a new database , SQL Server treats the NULL values as empty fields. IMy queries worked properly when the records/fields were NULL and so I want to either find a way to retain the NULL values in the data or convert the blank fields to NULL values. I'm assuming the former would be easier, but I don't know how to do that. Any help would be appreciated. 回答1: In your destination connection in

How do I script a password change for a SQL server login?

时光总嘲笑我的痴心妄想 提交于 2019-12-21 07:06:55
问题 Just what the title says, I need to change the password for an existing sql server login and I want to do it via sql script. 回答1: ALTER LOGIN http://msdn.microsoft.com/en-us/library/ms189828.aspx 回答2: If the user already has the ALTER ANY LOGIN permission (i.e. the user can change any password for any user) then this works: alter login mylogin with password = 'mylogin' Otherwise, if you don't want to make every user in your system a superuser, add a parameter of the old password for the same

ALTER current database without using its name

时间秒杀一切 提交于 2019-12-21 06:59:49
问题 I need to run update script on current database ( ALTER DATABASE... ), but can't use implicit its name. Is possible to use some function to get current db name and use inside ALTER DATABASE (Sql Server 2005 and above) ? I tried use db_name() , but doesn't work. select db_name(); works ALTER DATABASE db_name() ... doesn't work 回答1: You need to use something like declare @dbname varchar(100) set @dbname=quotename(db_name()) exec('alter database '+@dbname+' ...'); or.. even simpler set @sql=

Subtract minute from DateTime in SQL Server 2005

若如初见. 提交于 2019-12-21 06:45:26
问题 Suppose I have a datetime field whose value is 2000-01-01 08:30:00 and a duration field whose value is say 00:15 (meaning 15 minutes) If I subtract these two, I should get 2000-01-01 08:15:00 Also if I want to subtract 1:15 (means 1 hour 15 minutes), the output should be 2000-01-01 07:15:00 I am trying SELECT DATEDIFF(minute, '00:15','2000-01-01 08:30:00'); But the output is 52595055. How can i get the desired result? N.B.~ If I do SELECT dateadd(minute, -15,'2000-01-01 08:30:00'); , I will

SQL Server 2005 : split string into array and get array(x)?

混江龙づ霸主 提交于 2019-12-21 05:47:18
问题 I have strings in a database table like this: Peter/Parker/Spiderman/Marvel Bruce/Wayne/Batman/DC Is there a way in SQL to extract specific values from a string e.g. Name = MyColumn(0) SurName = MyColumn(1) Character = MyColumn(3) Company = MyColumn(4) Thanks 回答1: If you know you will have exactly 4 columns, then you can also use this nested CTE version: ;with s1 (name, extra) as ( select left(data, charindex('/', data)-1), substring(data, charindex('/', data) +1, len(data)) from yourtable ),

How to simulate heavy database usage with SQL server 2005

a 夏天 提交于 2019-12-21 05:36:18
问题 How can I simulate very heavy database usage on Microsoft SQL Server 2005? For test purposes I need to push the sql server to the max. The server is on a virtual machine and I don't care about network load, just CRUD operations, mainly inserts because I want to demonstrate how the database grows very quickly. 回答1: There are 2 free utilities from Microsoft called SQLIOSim (was SQLIOSTress) to spank the IO system and OSTRESS to replay trace files. 回答2: If you aren't worried about the network

First two words in the string - sql server

天大地大妈咪最大 提交于 2019-12-21 05:33:08
问题 I have string like this " This is a hello world example" Now I want first two words of the sentence as my output in SQL Server. i.e. This is . Another example: Original sentence : "Complete word exercise" Output: Complete word 回答1: Many solutions will break for any strings that have less than 2 words , which is increasingly likely for people hoping to parse the first n number of words. Let's first look at the query, and then how we can tell if it actually evaluated correctly. For that, we

User-Defined Functions SQL Server 2005 flagged incorrectly as non-deterministic?

 ̄綄美尐妖づ 提交于 2019-12-21 05:21:50
问题 Related to this question, I decided to check the UDFs in my data warehouse (which should largely have been deterministic), and I found several which aren't which should be. For instance: CREATE FUNCTION [udf_YearFromDataDtID] ( @DATA_DT_ID int ) RETURNS int AS BEGIN RETURN @DATA_DT_ID / 10000 END Shows up in this query: SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE IS_DETERMINISTIC = 'NO' AND ROUTINE_TYPE = 'FUNCTION' ORDER BY ROUTINE_NAME Why is this? 回答1: Yikes - apparently, it